Hi,
I am not sure if this is an appropriate posting , but I need help with a C
program. I've received help here before so I thought I'd give it a try.
IF there is a more appropriate group, pardon my post and please let me know.
I am running DU 4.0B on a AlphaServer 1000A. I have a program which reads
a dbm file and then does a getpwnam call. Before the getpwnam call, the
data from the dbm file is fine and can be printed. After the call, the
data is lost. A printf prints null strings! What happened to my data?
My program:
#include <stdio.h>
#include <ndbm.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <time.h>
#include <pwd.h>
#include <string.h>
char *EmployeeDBM = "employee";
/* employeedbm record */
#define EMPLOYEEDBM_RECORD_SIZE 111
#define SIZE_EMPLOYEEDBM_FNAME 14
#define SIZE_EMPLOYEEDBM_MINIT 1
#define SIZE_EMPLOYEEDBM_LNAME 15
#define SIZE_EMPLOYEEDBM_ML 4
#define SIZE_EMPLOYEEDBM_PHONE 10
#define SIZE_EMPLOYEEDBM_STATUS 1
#define SIZE_EMPLOYEEDBM_DEPT 30
#define SIZE_EMPLOYEEDBM_COLLEGE 3
#define SIZE_EMPLOYEEDBM_TITLE 25
#define SIZE_EMPLOYEEDBM_BMONTH 2
#define SIZE_EMPLOYEEDBM_BDAY 2
struct employeedbm_record
{
char fname[14],
minit[1],
lname[15],
ml[4],
phone[10],
status[1],
dept[30],
college[3],
title[25],
bmonth[2],
bday[2],
filler[2],
stopper;
};
main() {
struct employeedbm_record *EmployeeDBMRecord;
struct passwd *pwptr;
char *lower_uid = "daviser";
char *ssnumber = "xxxxxxxxx";
int i;
char social[9];
datum key;
datum content;
DBM *dbm;
strcpy(social,"xxxxxxxxx");
dbm = dbm_open(EmployeeDBM, O_RDONLY , S_IRUSR | S_IWUSR);
if (dbm == NULL){
printf("Cannot open employee dbm file\n");
exit(1);
}
key.dptr = social;
key.dsize = 10;
content = dbm_fetch(dbm,key);
dbm_close(dbm);
if (content.dptr == NULL) {
return(0);
}
else {
EmployeeDBMRecord = (struct employeedbm_record *) content.dptr;
}
printf("before %s\n",(EmployeeDBMRecord)); /* works */
pwptr = getpwnam(lower_uid);
printf("after %s\n",(EmployeeDBMRecord)); /* does not work - prints nulls */
printf("username %s\n",pwptr->pw_name); /* prints name correctly */
}
Ellen Davis
Received on Fri Jun 06 1997 - 21:40:47 NZST