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 >
C/C++ Source or Header  |  1988-02-10  |  2KB  |  100 lines

  1. #include <pwd.h>
  2. #include <stdio.h>
  3.  
  4. FILE *pw=NULL;
  5. char line[256],*lp;
  6. struct passwd pw_ent;
  7. int isclose=1;
  8.  
  9. setpwent()
  10. {
  11.     if(isclose) 
  12.         if ((pw=fopen("/dd/SYS/password","r")) == NULL)
  13.             puts("ERROR");  /* opens password file at first call */
  14.     else rewind(pw);   /* else  rewind to first line */
  15.     isclose=0;
  16.     return(pw);
  17. }
  18.  
  19.  
  20. endpwent()
  21. {
  22.     isclose=1;
  23.     return(fclose(pw));
  24. }
  25.  
  26.  
  27. char *getpwword(p,c)    /* returns pointer of first word until ':' */
  28. char c,**p;             /* sets p on char after ':' or NULL */
  29.  
  30. {
  31.     char *h,*hp;
  32.     hp=*p;
  33.     if(*p==NULL) return(NULL);  /* NULL string return NULL */
  34.     if((h=(char *)index(*p,c))==NULL) *p=NULL;  /* no char after ',' */
  35.     else {
  36.         *h='\0'; *p=++h;   /* delete ',' set p on char after ',' */
  37.     }
  38.     return(hp);
  39. }
  40.  
  41.  
  42. struct passwd *getpwent()
  43. {
  44.     char *hp;
  45.     int id;
  46.     if(isclose) setpwent();  /* if password file isn't open open it */
  47.     if(fgets(line,256,pw)==NULL) return(NULL); /* at eof */
  48.     lp=&line[0];
  49.     if((pw_ent.pw_name=getpwword(&lp,','))==NULL) return(NULL);
  50.     if((pw_ent.pw_passwd=getpwword(&lp,','))==NULL) return(NULL);
  51.     if((hp=getpwword(&lp,'.'))==NULL) return(NULL);
  52.     pw_ent.pw_gid=atoi(hp);
  53.     if((hp=getpwword(&lp,','))==NULL) return(NULL);
  54.     pw_ent.pw_uid=atoi(hp);
  55.     if((hp=getpwword(&lp,','))==NULL) return(NULL);
  56.     pw_ent.pw_prio=atoi(hp);
  57.     if((pw_ent.pw_xdir=getpwword(&lp,','))==NULL) return(NULL);
  58.     if((pw_ent.pw_dir=getpwword(&lp,','))==NULL) return(NULL);
  59.     if((pw_ent.pw_shell=getpwword(&lp,'\n'))==NULL) return(NULL);
  60.     return(&pw_ent);
  61. }
  62.  
  63.  
  64. struct passwd *getpwnam(name)
  65. char *name;
  66. {
  67.     struct passwd *p;
  68.     setpwent();
  69.     while((p=getpwent())!=NULL) {
  70.         if(strcmp(p->pw_name,name)==0) return(p);
  71.     }
  72.     return(NULL);
  73. }
  74.  
  75.  
  76. struct passwd *getpwuid(gid)
  77. int gid;
  78. {
  79.     struct passwd *p;
  80.     setpwent();
  81.     while((p=getpwent())!=NULL) {
  82.         if(p->pw_gid==gid) return(p);
  83.     }
  84.     return(NULL);
  85. }
  86.  
  87.  
  88. setpwfile(name)
  89. char *name;
  90. {
  91.     if(isclose) pw=fopen(name,"r");  /* opens password file at first call */
  92.     else rewind(pw);   /* else  rewind to first line */
  93.     isclose=0;
  94.     return(pw);
  95. }
  96.  
  97.  
  98.  
  99.  
  100.