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 / getgrnam.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-11  |  3.7 KB  |  131 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 <string.h>
  23. #include <grp.h>
  24.  
  25. #ifdef YP
  26. #include <rpc/rpc.h>
  27. #include <rpcsvc/yp_prot.h>
  28. #include <rpcsvc/ypclnt.h>
  29. extern int __yp_check(char **) ;
  30. extern struct group * __nis_parsegroupdata(char *, void *);
  31. extern struct group * __nis_getgrnam(const char *, char **, void *);
  32. #endif
  33.  
  34. /* Search for an entry with a matching name.  */
  35. struct group *
  36. DEFUN(getgrnam, (name), register CONST char *name)
  37. {
  38.   static PTR info = NULL;
  39.   register FILE *stream;
  40.   register struct group *g;
  41.   
  42.   if (NULL == info)
  43.     {
  44.       info = __grpalloc();
  45.       if (NULL == info)
  46.         return NULL;
  47.     }
  48.   
  49.   stream = __grpopen();
  50.   if (NULL == stream)
  51.     return NULL;
  52.   
  53.   while ((g = __grpread(stream, info)) != NULL)
  54.     {
  55.       if (!strcmp(g->gr_name, name))
  56.         break;
  57. #ifdef YP
  58.           /*
  59.            * Handle +group/-group entries.
  60.            * Bug: gets full entry from NIS, overwriting of some fields
  61.            * not supported (yet). --Swen
  62.            */
  63.       if (('-' == g->gr_name[0]) && (0 == strcmp(g->gr_name + 1, name)))
  64.         return NULL;
  65.       if ('+' == g->gr_name[0] && 0 == strcmp(g->gr_name + 1, name))
  66.         {
  67.           struct group *grp = g;
  68.           g = __nis_getgrnam(g->gr_name + 1, grp->gr_mem, info);
  69.           if (NULL != g && NULL != grp->gr_passwd && '\0' != grp->gr_passwd[0])
  70.             g->gr_passwd = grp->gr_passwd;
  71.           break;
  72.         }
  73.       if (0 == strcmp(g->gr_name, "+"))
  74.         {
  75.           g = __nis_getgrnam(name, NULL, info);
  76.           break;
  77.         }
  78. #endif;
  79.     }
  80.   (void) fclose(stream);
  81.   return(g);
  82. }
  83.  
  84. #ifdef YP
  85. struct group *
  86. __nis_getgrnam(const char *name, char **more_members, void *info)
  87. {
  88.   static char *nisdomain = NULL;
  89.   char *outkey, *outtmp, *c;
  90.   int status, outkeylen ;
  91.   struct group *gptr = NULL;
  92.   int i;
  93.     
  94.   if (1 == __yp_check(NULL))
  95.     {
  96.       if (NULL == nisdomain)
  97.         yp_get_default_domain(&nisdomain);
  98.       status = yp_match(nisdomain, "group.byname",
  99.                         name, strlen(name),
  100.                         &outkey, &outkeylen) ;
  101.       if (0 != status)
  102.         return NULL;
  103.       if (NULL != more_members)
  104.         {
  105.           i = strlen(outkey);
  106.           outtmp = malloc(i+1);
  107.           strcpy(outtmp, outkey);
  108.           free(outkey);
  109.           if ('\n' == outtmp[i-1])
  110.             outtmp[i-1] = '\0';
  111.           i = 0;
  112.           c = strrchr(outtmp, ':');
  113.           *(c+1) = '\0';
  114.           while (more_members[i] != NULL)
  115.             {
  116.               outtmp = realloc(outtmp, strlen(outtmp) + strlen(more_members[i]) + 3);
  117.               strcat(outtmp, more_members[i]);
  118.               strcat(outtmp, ",");
  119.               i++;
  120.             }
  121.           *(strrchr(outtmp, ',')) = '\0';
  122.           strcat(outtmp, "\n");
  123.           outkey = outtmp;
  124.         }
  125.       gptr = __nis_parsegroupdata(outkey, info) ;
  126.       free (outkey);
  127.     }
  128.   return (gptr);
  129. }
  130. #endif
  131.