home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / dev / gcc / ixemulsrc.lha / ixemul / ixnet / getgrent.c < prev    next >
C/C++ Source or Header  |  1996-12-11  |  6KB  |  239 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.     UG_setgrent();
  150.     return 1;
  151.     }
  152.  
  153.     if (!start_gr())
  154.       return(0);
  155.     u.u_grp_stayopen = stayopen;
  156.     return 1;
  157. }
  158.  
  159. void
  160. endgrent(void)
  161. {
  162.     register struct ixnet *p = (struct ixnet *)u.u_ixnet;
  163.  
  164.     if (p->u_networkprotocol == IX_NETWORK_AMITCP) {
  165.     UG_endgrent();
  166.     }
  167.     if (u.u_grp_fp) {
  168.     (void)fclose(u.u_grp_fp);
  169.     u.u_grp_fp = NULL;
  170.     }
  171. }
  172.  
  173. static int
  174. grscan(int search, int gid, char *name)
  175. {
  176.     register char *cp, **m;
  177.     char *bp;
  178.     char *fgets(), *strsep(), *index();
  179.  
  180.     if (u.u_grp_line == NULL)
  181.       u.u_grp_line = malloc(MAXLINELENGTH + 1);
  182.     if (u.u_members == NULL)
  183.       u.u_members = malloc(MAXGRP * sizeof(char *));
  184.     if (u.u_grp_line == NULL || u.u_members == NULL)
  185.       {
  186.         errno = ENOMEM;
  187.         return 0;
  188.       }
  189.     for (;;) {
  190.     if (!fgets(u.u_grp_line, MAXLINELENGTH, u.u_grp_fp))
  191.         return(0);
  192.     bp = u.u_grp_line;
  193.     /* skip lines that are too big */
  194.     if (!index(u.u_grp_line, '\n')) {
  195.         int ch;
  196.  
  197.         while ((ch = getc(u.u_grp_fp)) != '\n' && ch != EOF)
  198.         ;
  199.  
  200.         continue;
  201.     }
  202.     u.u_group.gr_name = strsep(&bp, "|\n");
  203.     if (search && name && strcmp(u.u_group.gr_name, name))
  204.         continue;
  205.  
  206.     u.u_group.gr_passwd = strsep(&bp, "|\n");
  207.     if (!(cp = strsep(&bp, "|\n")))
  208.         continue;
  209.  
  210.     u.u_group.gr_gid = atoi(cp);
  211.     if (search && name == NULL && u.u_group.gr_gid != gid)
  212.         continue;
  213.  
  214.     for (m = u.u_group.gr_mem = u.u_members;; ++m) {
  215.         if (m == &u.u_members[MAXGRP - 1]) {
  216.         *m = NULL;
  217.         break;
  218.         }
  219.  
  220.         if ((*m = strsep(&bp, ", \n")) == NULL)
  221.         break;
  222.     }
  223.     return(1);
  224.     }
  225.     /* NOTREACHED */
  226. }
  227.  
  228.  
  229. int
  230. setgroups (int ngroups, const int *gidset)
  231. {
  232.   struct ixnet *p = u.u_ixnet;
  233.  
  234.   if (p->u_networkprotocol == IX_NETWORK_AMITCP)
  235.     return UG_setgroups(ngroups,gidset);
  236.   return (ngroups >= 1) ? 0 : -1;
  237. }
  238.  
  239.