home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / ixemul-45.0-src.tgz / tar.out / contrib / ixemul / ixnet / getgrent.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  6KB  |  236 lines

  1. /*
  2.  * Copyright (c) 1989 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #if defined(LIBC_SCCS) && !defined(lint)
  35. static char sccsid[] = "@(#)getgrent.c  5.9 (Berkeley) 4/1/91";
  36. #endif /* LIBC_SCCS and not lint */
  37.  
  38. #define _KERNEL
  39. #include "ixnet.h"
  40. #include <sys/types.h>
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <grp.h>
  44.  
  45. /* for AS225 */
  46. #undef _PATH_GROUP
  47. #define _PATH_GROUP "/inet/db/group"
  48.  
  49. static int grscan(), start_gr();
  50.  
  51. #define MAXGRP        200
  52. #define MAXLINELENGTH    1024
  53.  
  54. struct group *
  55. getgrent(void)
  56. {
  57.     register struct ixnet *p = (struct ixnet *)u.u_ixnet;
  58.     if (p->u_networkprotocol == IX_NETWORK_AMITCP) {
  59.     return UG_getgrent();
  60.     }
  61.     if ((!u.u_grp_fp && !start_gr()) || !grscan(0, 0, NULL))
  62.     return(NULL);
  63.     return(&u.u_group);
  64. }
  65.  
  66. struct group *
  67. getgrnam(const char *name)
  68. {
  69.     register struct ixnet *p = (struct ixnet *)u.u_ixnet;
  70.  
  71.     if (p->u_networkprotocol == IX_NETWORK_AMITCP) {
  72.     struct group *err = UG_getgrnam(name);
  73.  
  74.     if (!err) {
  75.         errno = ug_GetErr();
  76.     }
  77.     return err;
  78.     }
  79.     else {
  80.     int rval;
  81.  
  82.     if (!start_gr())
  83.         return(NULL);
  84.  
  85.     rval = grscan(1, 0, name);
  86.     if (!u.u_grp_stayopen)
  87.         endgrent();
  88.  
  89.     return(rval ? &u.u_group : NULL);
  90.     }
  91. }
  92.  
  93. struct group *
  94. getgrgid(gid_t gid)
  95. {
  96.     register struct ixnet *p = (struct ixnet *)u.u_ixnet;
  97.  
  98.     if (p->u_networkprotocol == IX_NETWORK_AMITCP) {
  99.     struct group *err;
  100.     err = UG_getgrgid(gid);
  101.  
  102.     if (!err) {
  103.         errno = ug_GetErr();
  104.     }
  105.     return err;
  106.     }
  107.     else {
  108.     int rval;
  109.  
  110.     if (!start_gr())
  111.         return(NULL);
  112.  
  113.     rval = grscan(1, gid, NULL);
  114.     if (!u.u_grp_stayopen)
  115.         endgrent();
  116.  
  117.     return(rval ? &u.u_group : NULL);
  118.     }
  119. }
  120.  
  121. static int
  122. start_gr(void)
  123. {
  124.     if (u.u_grp_fp) {
  125.     rewind(u.u_grp_fp);
  126.     return(1);
  127.     }
  128.     return((u.u_grp_fp = fopen(_PATH_GROUP, "r")) ? 1 : 0);
  129. }
  130.  
  131. int
  132. setgrent(void)
  133. {
  134.     register struct ixnet *p = (struct ixnet *)u.u_ixnet;
  135.  
  136.     if (p->u_networkprotocol == IX_NETWORK_AMITCP) {
  137.     UG_setgrent();
  138.     return 1;
  139.     }
  140.     return setgroupent(0);
  141. }
  142.  
  143. int
  144. setgroupent(int stayopen)
  145. {
  146.     register struct ixnet *p = (struct ixnet *)u.u_ixnet;
  147.  
  148.     if (p->u_networkprotocol != IX_NETWORK_AMITCP) {
  149.     if (!start_gr())
  150.         return(0);
  151.     u.u_grp_stayopen = stayopen;
  152.     }
  153.     return 1;
  154. }
  155.  
  156. void
  157. endgrent(void)
  158. {
  159.     register struct ixnet *p = (struct ixnet *)u.u_ixnet;
  160.  
  161.     if (p->u_networkprotocol == IX_NETWORK_AMITCP) {
  162.     UG_endgrent();
  163.     }
  164.     if (u.u_grp_fp) {
  165.     (void)fclose(u.u_grp_fp);
  166.     u.u_grp_fp = NULL;
  167.     }
  168. }
  169.  
  170. static int
  171. grscan(int search, int gid, char *name)
  172. {
  173.     register char *cp, **m;
  174.     char *bp;
  175.     char *fgets(), *strsep(), *index();
  176.  
  177.     if (u.u_grp_line == NULL)
  178.       u.u_grp_line = malloc(MAXLINELENGTH + 1);
  179.     if (u.u_members == NULL)
  180.       u.u_members = malloc(MAXGRP * sizeof(char *));
  181.     if (u.u_grp_line == NULL || u.u_members == NULL)
  182.       {
  183.         errno = ENOMEM;
  184.         return 0;
  185.       }
  186.     for (;;) {
  187.     if (!fgets(u.u_grp_line, MAXLINELENGTH, u.u_grp_fp))
  188.         return(0);
  189.     bp = u.u_grp_line;
  190.     /* skip lines that are too big */
  191.     if (!index(u.u_grp_line, '\n')) {
  192.         int ch;
  193.  
  194.         while ((ch = getc(u.u_grp_fp)) != '\n' && ch != EOF)
  195.         ;
  196.  
  197.         continue;
  198.     }
  199.     u.u_group.gr_name = strsep(&bp, "|\n");
  200.     if (search && name && strcmp(u.u_group.gr_name, name))
  201.         continue;
  202.  
  203.     u.u_group.gr_passwd = strsep(&bp, "|\n");
  204.     if (!(cp = strsep(&bp, "|\n")))
  205.         continue;
  206.  
  207.     u.u_group.gr_gid = atoi(cp);
  208.     if (search && name == NULL && u.u_group.gr_gid != gid)
  209.         continue;
  210.  
  211.     for (m = u.u_group.gr_mem = u.u_members;; ++m) {
  212.         if (m == &u.u_members[MAXGRP - 1]) {
  213.         *m = NULL;
  214.         break;
  215.         }
  216.  
  217.         if ((*m = strsep(&bp, ", \n")) == NULL)
  218.         break;
  219.     }
  220.     return(1);
  221.     }
  222.     /* NOTREACHED */
  223. }
  224.  
  225.  
  226. int
  227. setgroups (int ngroups, const int *gidset)
  228. {
  229.   struct ixnet *p = u.u_ixnet;
  230.  
  231.   if (p->u_networkprotocol == IX_NETWORK_AMITCP)
  232.     return UG_setgroups(ngroups,gidset);
  233.   return (ngroups >= 1) ? 0 : -1;
  234. }
  235.  
  236.