home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / TELECOM / stg_v4.lzh / grp.c < prev    next >
C/C++ Source or Header  |  1994-11-11  |  2KB  |  122 lines

  1. /*
  2.  * group file routines
  3.  *
  4.  * 92/10/27 StG initial version for semi unix compatible
  5. */
  6.  
  7. #include "stglib.h"
  8.  
  9. /* unix already has these functions */
  10. #ifndef _UNIX
  11.  
  12. #include "grp.h"
  13. #include "stglib.h"
  14.  
  15. extern int errno;
  16.  
  17. #define GRPBUF 256
  18.  
  19. int hGRP=0;   /* file number */
  20. struct group _grp;
  21. char grp_buf[GRPBUF];
  22.  
  23. setgrent()
  24. {
  25.     int uid;
  26.  
  27.     if (hGRP)
  28.     {
  29.         lseek(hGRP,0L,0);
  30.         return(0);
  31.     }
  32.  
  33.     uid=getuid();
  34.     setuid(0);
  35.  
  36.     hGRP=open(GRP_FILE,O_RDONLY);
  37.     if (hGRP==ERR)
  38.     {
  39.         if (errno!=216)
  40.             syslog(LOG_CRIT,"setgrent: cant access %s %m",GRP_FILE);
  41.         hGRP=0;
  42.         setuid(uid);
  43.         return(ERR);
  44.     }
  45.     setuid(uid);
  46.     return(0);
  47. }
  48.  
  49. endgrent()
  50. {
  51.     if (hGRP)
  52.     {
  53.         close(hGRP);
  54.         hGRP=0;
  55.     }
  56.     return(0);
  57. }
  58.  
  59. struct group *
  60. getgrent()
  61. {
  62.     char *p;
  63.     int n;
  64.  
  65.     if (!hGRP)
  66.         if (setgrent()==ERR)
  67.             return(0);
  68.  
  69. nextline:
  70.     n=readln(hGRP,grp_buf,GRPBUF);
  71.  
  72.     if (n<=0)
  73.         return(0);
  74.  
  75.     grp_buf[n-1]=0;
  76.  
  77.     if (!*grp_buf || *grp_buf=='*')
  78.         goto nextline;
  79.  
  80.     _grp.gr_mem=0;
  81.  
  82.     _grp.gr_name=grp_buf;
  83.     p=strcut(grp_buf,',');
  84.     if (p)
  85.     {
  86.         p=strcut(p,',');
  87.         if (p)
  88.         {
  89.             p=strcut(p,',');
  90.             _grp.gr_mem=arglist(p,':');
  91.         }
  92.     }
  93.     return(&_grp);
  94. }
  95.  
  96. struct group *
  97. getgrnam(name)
  98. char *name;
  99. {
  100.     struct group *gr;
  101.  
  102.     if (_grp.gr_name && !stricmp(_grp.gr_name,name))
  103.         return(&_grp);
  104.  
  105.     setgrent();
  106.     do
  107.     {
  108.         gr=getgrent();
  109.         if (!gr)
  110.         {
  111. /*            endgrent(); */
  112.             return(0);
  113.         }
  114.     }
  115.     while (stricmp(gr->gr_name,name));
  116.  
  117. /*    endgrent(); */
  118.     return(gr);
  119. }
  120.  
  121. #endif
  122.