define new library function getgrouplist
[unix-history] / usr / src / lib / libc / gen / initgroups.c
index ba7716f..2288c53 100644 (file)
@@ -1,38 +1,35 @@
-/*     initgroups.c    4.2     83/02/15        */
+/*
+ * Copyright (c) 1983 Regents of the University of California.
+ * All rights reserved.
+ *
+ * %sccs.include.redist.c%
+ */
+
+#if defined(LIBC_SCCS) && !defined(lint)
+static char sccsid[] = "@(#)initgroups.c       5.8 (Berkeley) %G%";
+#endif /* LIBC_SCCS and not lint */
 
 /*
  * initgroups
  */
 
 /*
  * initgroups
  */
-#include <stdio.h>
 #include <sys/param.h>
 #include <sys/param.h>
-#include <grp.h>
-
-struct group *getgrent();
+#include <stdio.h>
 
 
+int
 initgroups(uname, agroup)
 initgroups(uname, agroup)
-       char *uname;
+       const char *uname;
        int agroup;
 {
        int agroup;
 {
-       int groups[NGROUPS], ngroups = 0;
-       register struct group *grp;
-       register int i;
+       int groups[NGROUPS], ngroups;
 
 
-       if (agroup >= 0)
-               groups[ngroups++] = agroup;
-       setgrent();
-       while (grp = getgrent())
-               for (i = 0; grp->gr_mem[i]; i++)
-                       if (!strcmp(grp->gr_mem[i], uname)) {
-                               if (ngroups == NGROUPS) {
-fprintf(stderr, "inigrp: %s is in too many groups\n", uname);
-                                       goto toomany;
-                               }
-                               groups[ngroups++] = grp->gr_gid;
-                       }
-toomany:
+       ngroups = NGROUPS;
+       if (getgrouplist(uname, agroup, groups, &ngroups) < 0)
+               fprintf(stderr,
+                   "initgroups: %s is in too many groups, using first %d\n",
+                   uname, ngroups);
        if (setgroups(ngroups, groups) < 0) {
        if (setgroups(ngroups, groups) < 0) {
-               perror("setgrp");
-               return (1);
+               perror("setgroups");
+               return (-1);
        }
        return (0);
 }
        }
        return (0);
 }