home *** CD-ROM | disk | FTP | other *** search
/ Serving the Web / ServingTheWeb1995.disc1of1.iso / linux / slacksrce / d / libc / libc-4.6 / libc-4 / libc-linux / grp / getgrent.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-17  |  4.4 KB  |  189 lines

  1. /* Copyright (C) 1991 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. #include <ansidecl.h>
  20. #include <stddef.h>
  21. #include <stdio.h>
  22. #include <grp.h>
  23.  
  24. #ifdef YP
  25. #include <rpcsvc/yp_prot.h>
  26. #include <rpcsvc/ypclnt.h>
  27. extern int __yp_check(char **);
  28. extern struct group * __nis_parsegroupdata(char *, void *);
  29. extern struct group * __nis_getgrnam(const char *, char **, void *);
  30. extern struct group * __nis_getgrent(int, void *);
  31. static int ypmode = 0;
  32. static int inkeylen;
  33. static char *inkey = NULL;
  34. #endif
  35.  
  36. static FILE *stream = NULL;
  37.  
  38. /* Rewind the stream.  */
  39. void
  40. DEFUN_VOID(setgrent)
  41. {
  42.   if (NULL != stream)
  43.     rewind(stream);
  44. #ifdef YP
  45.   ypmode = 0;
  46.   if(NULL != inkey)
  47.     {
  48.       free (inkey);
  49.       inkey = NULL;
  50.     }
  51. #endif
  52. }
  53.  
  54.  
  55. /* Close the stream.  */
  56. void
  57. DEFUN_VOID(endgrent)
  58. {
  59.   if (NULL != stream)
  60.     {
  61.       (void) fclose(stream);
  62.       stream = NULL;
  63.     }
  64. #ifdef YP
  65.   ypmode = 0;
  66.   if(NULL != inkey)
  67.     {
  68.       free (inkey);
  69.       inkey = NULL;
  70.     }
  71. #endif
  72. }
  73.  
  74.  
  75. /* Read an entry from the stream.  */
  76. struct group *
  77. DEFUN_VOID(getgrent)
  78. {
  79. #ifdef YP
  80.   struct group* grtmp;
  81. #endif
  82.   static PTR info = NULL;
  83.   if (NULL == info)
  84.     {
  85.       info = __grpalloc();
  86.       if (NULL == info)
  87.         return(NULL);
  88.     }
  89.   
  90.   if (NULL == stream)
  91.     {
  92.       stream = __grpopen();
  93.       if (NULL == stream)
  94.         return(NULL);
  95.     }
  96.   
  97. #ifdef YP
  98.   if (0 == ypmode) 
  99.     {
  100.       again:
  101.           /* disable -group entries */
  102.       do
  103.         {
  104.           grtmp = __grpread(stream, info);
  105.         }
  106.       while (NULL != grtmp && '-' == grtmp->gr_name[0]);
  107.       if (NULL == grtmp)
  108.         return NULL;
  109.           /*
  110.            * Handle +group entries.
  111.            */
  112.       if ('+' == grtmp->gr_name[0] && '\0' != grtmp->gr_name[1])
  113.         {
  114.           struct group *grp = grtmp;
  115.           grtmp = __nis_getgrnam(grtmp->gr_name + 1, grp->gr_mem, info);
  116.           if (NULL == grtmp)
  117.             goto again;
  118.           if (NULL != grp->gr_passwd && '\0' != grp->gr_passwd[0])
  119.             grtmp->gr_passwd = grp->gr_passwd;
  120.         }
  121.       
  122.       if (0 == strcmp(grtmp->gr_name, "+"))
  123.         {
  124.           ypmode = 1;
  125.           grtmp = __nis_getgrent(1, info);
  126.         }
  127.     }
  128.   else
  129.     {
  130.       grtmp = __nis_getgrent(0, info);
  131.     }
  132.   return(grtmp);
  133. #else
  134.   return(__grpread(stream, info));
  135. #endif
  136. }
  137.  
  138. #ifdef YP
  139. struct group *
  140. __nis_getgrent(int first, void *info)
  141. {
  142.   static char *nisdomain = NULL;
  143.   char *outkey, *outval;
  144.   int outkeylen, outvallen, status;
  145.   struct group *grptr;
  146.   
  147.   if (1 == __yp_check(NULL))
  148.     {
  149.       if (NULL == nisdomain)
  150.         yp_get_default_domain(&nisdomain);
  151.       if (1 == first)
  152.         {
  153.           status = yp_first(nisdomain, "group.byname",
  154.                             &outkey, &outkeylen,
  155.                             &outval, &outvallen);
  156.           if (0 != status)
  157.             return NULL;
  158.           inkey = outkey;
  159.           inkeylen = outkeylen;
  160.               /* Assumption: No comment lines in NIS maps */
  161.           grptr = __nis_parsegroupdata(outval, info);
  162.           free (outval);
  163.           return (grptr);
  164.         }
  165.       else
  166.         {
  167.           status = yp_next(nisdomain, "group.byname",
  168.                            inkey, inkeylen,
  169.                            &outkey, &outkeylen,
  170.                            &outval, &outvallen);
  171.           if (0 != status)
  172.             {
  173.               free (inkey);
  174.               inkey = NULL;
  175.               return NULL;
  176.             }
  177.           free (inkey);
  178.           inkey = outkey;
  179.           inkeylen = outkeylen;
  180.               /* Assumption: No comment lines in NIS maps */
  181.           grptr = __nis_parsegroupdata(outval, info);
  182.           free (outval);
  183.           return (grptr);
  184.         }
  185.     }
  186.   return NULL;
  187. }
  188. #endif
  189.