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 / getgrgid.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-11  |  3.1 KB  |  118 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 <sys/types.h>
  23. #include <grp.h>
  24.  
  25. #ifdef YP
  26. #include <rpcsvc/yp_prot.h>
  27. #include <rpcsvc/ypclnt.h>
  28. extern int __yp_check(char **) ;
  29. extern struct group * __nis_parsegroupdata(char *, void *);
  30. extern struct group * __nis_getgrnam(const char *, char **, void *);
  31. static struct group * __nis_getgrgid(gid_t, void *);
  32. #endif
  33.  
  34. /* Search for an entry with a matching group ID.  */
  35. struct group *
  36. DEFUN(getgrgid, (gid), register gid_t gid)
  37. {
  38.   static PTR info = NULL;
  39.   register FILE *stream;
  40.   register struct group *g;
  41.   
  42.   if (info == NULL)
  43.     {
  44.       info = __grpalloc();
  45.       if (info == NULL)
  46.         return NULL;
  47.     }
  48.   
  49.   stream = __grpopen();
  50.   if (stream == NULL)
  51.     return NULL;
  52.   
  53.   while ((g = __grpread(stream, info)) != NULL)
  54.     {
  55.       if (g->gr_gid == (gid_t) gid)
  56.         break;
  57. #ifdef YP
  58.           /*
  59.            * Handle +group/-group entries.
  60.            */
  61.       if ('-' == g->gr_name[0] && '\0' != g->gr_name[1])
  62.         {
  63.           g = __nis_getgrnam(g->gr_name + 1, NULL, info);
  64.           if (g->gr_gid == gid)
  65.             return NULL;
  66.           continue;
  67.         }
  68.       if ('+' == g->gr_name[0] && '\0' != g->gr_name[1])
  69.         {
  70.           struct group *grp = g;
  71.           g = __nis_getgrnam(g->gr_name + 1, g->gr_mem, info);
  72.           if (NULL != g && g->gr_gid == gid)
  73.             {
  74.               if (NULL != grp->gr_passwd && '\0' != grp->gr_passwd[0])
  75.                 g->gr_passwd = grp->gr_passwd;
  76.               break;
  77.             }
  78.           continue;
  79.         }
  80.       if (0 == strcmp(g->gr_name, "+"))
  81.         {
  82.           g = __nis_getgrgid(gid, info);
  83.           break;
  84.         }
  85. #endif
  86.     }
  87.   (void) fclose(stream);
  88.   return g;
  89. }
  90.  
  91. #ifdef YP
  92. static struct group *
  93. __nis_getgrgid(gid_t gid, void *info)
  94. {
  95.   static char *nisdomain = NULL;
  96.   char *outkey;
  97.   int status, outkeylen ;
  98.   struct group *gptr = NULL;
  99.   
  100.   if (1 == __yp_check(NULL))
  101.     {
  102.       char gidbuf[20] ;
  103.       if (NULL == nisdomain)
  104.         yp_get_default_domain(&nisdomain);
  105.       sprintf(gidbuf, "%d", gid) ;
  106.       status = yp_match(nisdomain, "group.bygid",
  107.                         gidbuf, strlen(gidbuf),
  108.                         &outkey, &outkeylen) ;
  109.       if (0 != status)
  110.         return NULL;
  111.       gptr = __nis_parsegroupdata(outkey, info) ;
  112.       free (outkey);
  113.       return (gptr);
  114.     }
  115.   return NULL;
  116. }
  117. #endif
  118.