home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
rtsi.com
/
2014.01.www.rtsi.com.tar
/
www.rtsi.com
/
OS9
/
OSK
/
TELECOM
/
OS9_Unix.lzh
/
RSHSRC
/
getpw.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-09-10
|
2KB
|
101 lines
#include <stdio.h>
#include <pwd.h>
extern int errno;
extern char *strtok();
extern char *malloc();
#define PASSWORDFILE "/dd/sys/password"
static FILE *pwfile = NULL;
static struct passwd pwbuf;
static char pwline[512];
static char *pwfilename = NULL;
struct passwd *getpwent() {
int saveerr;
static char colon[] = ",.:\n";
register char *cp;
if(pwfile == NULL) {
pwfile = fopen(pwfilename == NULL ? PASSWORDFILE : pwfilename, "r");
if(pwfile == NULL) return NULL;
}
if(fgets(pwline, sizeof pwline, pwfile) == NULL) {
saveerr = errno;
fclose(pwfile);
pwfile = NULL;
errno = saveerr;
return NULL;
}
pwbuf.pw_name = strtok(pwline, colon);
pwbuf.pw_passwd = strtok((char *)NULL, colon);
cp = strtok((char *)NULL, colon);
pwbuf.pw_gid = (cp == NULL) ? -1 : atoi(cp);
cp = strtok((char *)NULL, colon);
pwbuf.pw_uid = (cp == NULL) ? -1 : atoi(cp);
cp = strtok((char *)NULL, colon);
pwbuf.pw_priority = (cp == NULL) ? 0 : atoi(cp);
/* pwbuf.pw_gecos = strtok((char *)NULL, colon); */
pwbuf.pw_gecos="No GECOS!";
pwbuf.pw_xdir = strtok((char *)NULL, colon);
pwbuf.pw_dir = strtok((char *)NULL, colon);
pwbuf.pw_shell = strtok((char *)NULL, "\n");
return &pwbuf;
}
setpwent() {
if(pwfile != NULL) {
return fseek(pwfile, 0, 0);
}
return 0;
}
endpwent() {
if(pwfile != NULL) {
fclose(pwfile);
pwfile = NULL;
}
return 0;
}
setpwfile(name)
register char *name;
{
if(pwfilename != NULL) free(pwfilename);
if(name != NULL) {
pwfilename = malloc(strlen(name) + 1);
if(pwfilename == NULL) return -1;
strcpy(pwfilename, name);
} else {
pwfilename = NULL;
}
return 0;
}
struct passwd *getpwuid(giduid)
int giduid;
{
register unsigned short gid, uid;
gid = giduid >> 16;
uid = giduid & 0xffff;
setpwent();
while(getpwent() != NULL) {
if(pwbuf.pw_uid == uid && pwbuf.pw_gid == gid) return &pwbuf;
}
return NULL;
}
struct passwd *getpwnam(name)
register char *name;
{
register struct passwd *pw;
setpwent();
while((pw = getpwent()) != NULL) {
if(strcmp(pw->pw_name, name) == 0) return pw;
}
return NULL;
}