BSD 4_4_Lite2 release
[unix-history] / usr / src / usr.bin / id / id.c
index 18fa862..bf9218e 100644 (file)
@@ -1,28 +1,55 @@
 /*-
 /*-
- * Copyright (c) 1991 The Regents of the University of California.
- * All rights reserved.
+ * Copyright (c) 1991, 1993
+ *     The Regents of the University of California.  All rights reserved.
  *
  *
- * %sccs.include.redist.c%
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-char copyright[] =
-"@(#) Copyright (c) 1991 The Regents of the University of California.\n\
- All rights reserved.\n";
+static char copyright[] =
+"@(#) Copyright (c) 1991, 1993\n\
      The Regents of the University of California.  All rights reserved.\n";
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)id.c       5.5 (Berkeley) %G%";
+static char sccsid[] = "@(#)id.c       8.3 (Berkeley) 4/28/95";
 #endif /* not lint */
 
 #include <sys/param.h>
 #endif /* not lint */
 
 #include <sys/param.h>
-#include <pwd.h>
-#include <grp.h>
+
 #include <errno.h>
 #include <errno.h>
-#include <unistd.h>
-#include <stdlib.h>
+#include <grp.h>
+#include <pwd.h>
 #include <stdio.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 #include <string.h>
+#include <unistd.h>
 
 void   current __P((void));
 void   err __P((const char *, ...));
 
 void   current __P((void));
 void   err __P((const char *, ...));
@@ -162,7 +189,8 @@ current()
 {
        struct group *gr;
        struct passwd *pw;
 {
        struct group *gr;
        struct passwd *pw;
-       int id, eid, lastid, ngroups, groups[NGROUPS];
+       int cnt, id, eid, lastid, ngroups;
+       gid_t groups[NGROUPS];
        char *fmt;
 
        id = getuid();
        char *fmt;
 
        id = getuid();
@@ -184,9 +212,9 @@ current()
                        (void)printf("(%s)", gr->gr_name);
        }
        if (ngroups = getgroups(NGROUPS, groups)) {
                        (void)printf("(%s)", gr->gr_name);
        }
        if (ngroups = getgroups(NGROUPS, groups)) {
-               for (fmt = " groups=%u", lastid = -1; --ngroups >= 0;
+               for (fmt = " groups=%u", lastid = -1, cnt = 0; cnt < ngroups;
                    fmt = ", %u", lastid = id) {
                    fmt = ", %u", lastid = id) {
-                       id = groups[ngroups];
+                       id = groups[cnt++];
                        if (lastid == id)
                                continue;
                        (void)printf(fmt, id);
                        if (lastid == id)
                                continue;
                        (void)printf(fmt, id);
@@ -202,24 +230,25 @@ user(pw)
        register struct passwd *pw;
 {
        register struct group *gr;
        register struct passwd *pw;
 {
        register struct group *gr;
-       register int id, lastid;
        register char *fmt, **p;
        register char *fmt, **p;
+       int cnt, id, lastid, ngroups, groups[NGROUPS + 1];
 
        id = pw->pw_uid;
        (void)printf("uid=%u(%s)", id, pw->pw_name);
        (void)printf(" gid=%u", pw->pw_gid);
 
        id = pw->pw_uid;
        (void)printf("uid=%u(%s)", id, pw->pw_name);
        (void)printf(" gid=%u", pw->pw_gid);
-       if (gr = getgrgid(id))
+       if (gr = getgrgid(pw->pw_gid))
                (void)printf("(%s)", gr->gr_name);
                (void)printf("(%s)", gr->gr_name);
-       for (fmt = " groups=%u(%s)", lastid = -1; gr = getgrent();
-           lastid = id) {
-               if (pw->pw_gid == gr->gr_gid)
+       ngroups = NGROUPS + 1;
+       (void) getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
+       fmt = " groups=%u";
+       for (lastid = -1, cnt = 0; cnt < ngroups; ++cnt) {
+               if (lastid == (id = groups[cnt]))
                        continue;
                        continue;
-               for (p = gr->gr_mem; p && *p; p++)
-                       if (!strcmp(*p, pw->pw_name)) {
-                               (void)printf(fmt, gr->gr_gid, gr->gr_name);
-                               fmt = ", %u(%s)";
-                               break;
-                       }
+               (void)printf(fmt, id);
+               fmt = " %u";
+               if (gr = getgrgid(id))
+                       (void)printf("(%s)", gr->gr_name);
+               lastid = id;
        }
        (void)printf("\n");
 }
        }
        (void)printf("\n");
 }
@@ -230,44 +259,33 @@ group(pw, nflag)
        int nflag;
 {
        struct group *gr;
        int nflag;
 {
        struct group *gr;
-       int cnt, id, lastid, ngroups, groups[NGROUPS + 1];
-       char *fmt, *name, **p;
+       int cnt, id, lastid, ngroups;
+       gid_t groups[NGROUPS + 1];
+       char *fmt;
 
 
-       fmt = nflag ? "%s" : "%u";
        if (pw) {
        if (pw) {
-               name = pw->pw_name;
-               while (gr = getgrent()) {
-                       for (p = gr->gr_mem; p && *p; p++)
-                               if (!strcmp(*p, name)) {
-                                       if (nflag) {
-                                               (void)printf(fmt, gr->gr_name);
-                                               fmt = " %s";
-                                       } else {
-                                               (void)printf(fmt, gr->gr_gid);
-                                               fmt = " %u";
-                                       }
-                                       break;
-                               }
-               }
+               ngroups = NGROUPS + 1;
+               (void) getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
        } else {
                groups[0] = getgid();
                ngroups = getgroups(NGROUPS, groups + 1) + 1;
        } else {
                groups[0] = getgid();
                ngroups = getgroups(NGROUPS, groups + 1) + 1;
-               for (lastid = -1, cnt = 0; cnt < ngroups; ++cnt) {
-                       if (lastid == (id = groups[cnt]))
-                               continue;
-                       if (nflag) {
-                               if (gr = getgrgid(id))
-                                       (void)printf(fmt, gr->gr_name);
-                               else
-                                       (void)printf(*fmt == ' ' ? " %u" : "%u",
-                                           id);
-                               fmt = " %s";
-                       } else {
-                               (void)printf(fmt, id);
-                               fmt = " %u";
-                       }
-                       lastid = id;
+       }
+       fmt = nflag ? "%s" : "%u";
+       for (lastid = -1, cnt = 0; cnt < ngroups; ++cnt) {
+               if (lastid == (id = groups[cnt]))
+                       continue;
+               if (nflag) {
+                       if (gr = getgrgid(id))
+                               (void)printf(fmt, gr->gr_name);
+                       else
+                               (void)printf(*fmt == ' ' ? " %u" : "%u",
+                                   id);
+                       fmt = " %s";
+               } else {
+                       (void)printf(fmt, id);
+                       fmt = " %u";
                }
                }
+               lastid = id;
        }
        (void)printf("\n");
 }
        }
        (void)printf("\n");
 }