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 / initgroups.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-17  |  2.8 KB  |  108 lines

  1. /* Copyright (C) 1989, 1991, 1993 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 <unistd.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <grp.h>
  24. #include <limits.h>
  25. #include <sys/types.h>
  26.  
  27. #ifdef YP
  28. extern struct group * __nis_getgrnam(const char *, char **, void *);
  29. extern struct group * __nis_getgrent(int, void *);
  30. #endif
  31.  
  32. /* Initialize the group set for the current user
  33.    by reading the group database and using all groups
  34.    of which USER is a member.  Also include GROUP.  */
  35. int
  36. DEFUN(initgroups, (user, group),
  37.       CONST char *user AND gid_t group)
  38. {
  39. #ifdef NGROUPS_MAX
  40. #if NGROUPS_MAX == 0
  41.   return 0;
  42. #else
  43.   static PTR info = NULL;
  44.   register FILE *stream;
  45.   register struct group *g;
  46.   gid_t groups[NGROUPS_MAX];
  47.   register size_t n;
  48.   int ypmode = 0;
  49.   
  50.   if (info == NULL)
  51.     {
  52.       info = __grpalloc();
  53.       if (info == NULL)
  54.         return -1;
  55.     }
  56.   
  57.   stream = __grpopen();
  58.   if (stream == NULL)
  59.     return -1;
  60.   
  61.   n = 0;
  62.   groups[n++] = group;
  63.   
  64. #ifdef YP
  65.   while (n < NGROUPS_MAX &&
  66.          (g = (0 == ypmode ? __grpread(stream, info) : __nis_getgrent(0, info))) != NULL)
  67. #else
  68.   while (n < NGROUPS_MAX && (g = __grpread(stream, info)) != NULL)
  69. #endif
  70.     {
  71. #ifdef YP
  72.       if ('-' == g->gr_name[0] && '\0' != g->gr_name[1])
  73.           /* FIXME: must remember this group, it must not show up
  74.              in grouplist! */
  75.         continue;
  76.  
  77.       if ('+' == g->gr_name[0] && '\0' != g->gr_name[1])
  78.         {
  79.           g = __nis_getgrnam(g->gr_name + 1, g->gr_mem, info);
  80.           if (NULL == g)
  81.             continue;
  82.         }
  83.       else if (0 == strcmp(g->gr_name, "+"))
  84.         {
  85.           ypmode = 1;
  86.           g = __nis_getgrent(1, info);
  87.           if (NULL == g)
  88.             break;
  89.         }
  90. #endif /* YP */
  91.       if (g->gr_gid != group)
  92.         {
  93.           register char **m;
  94.           
  95.           for (m = g->gr_mem; *m != NULL; ++m)
  96.             if (!strcmp(*m, user))
  97.               groups[n++] = g->gr_gid;
  98.         }
  99.     }
  100.   (void) fclose (stream);
  101.   return setgroups(n, groups);
  102. #endif
  103. #else
  104.   return 0;
  105. #endif
  106. }
  107.   
  108.