home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
rtsi.com
/
2014.01.www.rtsi.com.tar
/
www.rtsi.com
/
OS9
/
OSK
/
EFFO
/
forum5.lzh
/
SPRACHEN
/
C
/
PASSWD
/
pwd.c
< prev
next >
Wrap
C/C++ Source or Header
|
1988-02-10
|
2KB
|
100 lines
#include <pwd.h>
#include <stdio.h>
FILE *pw=NULL;
char line[256],*lp;
struct passwd pw_ent;
int isclose=1;
setpwent()
{
if(isclose)
if ((pw=fopen("/dd/SYS/password","r")) == NULL)
puts("ERROR"); /* opens password file at first call */
else rewind(pw); /* else rewind to first line */
isclose=0;
return(pw);
}
endpwent()
{
isclose=1;
return(fclose(pw));
}
char *getpwword(p,c) /* returns pointer of first word until ':' */
char c,**p; /* sets p on char after ':' or NULL */
{
char *h,*hp;
hp=*p;
if(*p==NULL) return(NULL); /* NULL string return NULL */
if((h=(char *)index(*p,c))==NULL) *p=NULL; /* no char after ',' */
else {
*h='\0'; *p=++h; /* delete ',' set p on char after ',' */
}
return(hp);
}
struct passwd *getpwent()
{
char *hp;
int id;
if(isclose) setpwent(); /* if password file isn't open open it */
if(fgets(line,256,pw)==NULL) return(NULL); /* at eof */
lp=&line[0];
if((pw_ent.pw_name=getpwword(&lp,','))==NULL) return(NULL);
if((pw_ent.pw_passwd=getpwword(&lp,','))==NULL) return(NULL);
if((hp=getpwword(&lp,'.'))==NULL) return(NULL);
pw_ent.pw_gid=atoi(hp);
if((hp=getpwword(&lp,','))==NULL) return(NULL);
pw_ent.pw_uid=atoi(hp);
if((hp=getpwword(&lp,','))==NULL) return(NULL);
pw_ent.pw_prio=atoi(hp);
if((pw_ent.pw_xdir=getpwword(&lp,','))==NULL) return(NULL);
if((pw_ent.pw_dir=getpwword(&lp,','))==NULL) return(NULL);
if((pw_ent.pw_shell=getpwword(&lp,'\n'))==NULL) return(NULL);
return(&pw_ent);
}
struct passwd *getpwnam(name)
char *name;
{
struct passwd *p;
setpwent();
while((p=getpwent())!=NULL) {
if(strcmp(p->pw_name,name)==0) return(p);
}
return(NULL);
}
struct passwd *getpwuid(gid)
int gid;
{
struct passwd *p;
setpwent();
while((p=getpwent())!=NULL) {
if(p->pw_gid==gid) return(p);
}
return(NULL);
}
setpwfile(name)
char *name;
{
if(isclose) pw=fopen(name,"r"); /* opens password file at first call */
else rewind(pw); /* else rewind to first line */
isclose=0;
return(pw);
}