This adds "id -c" to display a user's login class.  If no user is
specified, it looks up the passwd entry based on the real uid and
displays the corresponding login class.

This is similar to "id -c" in FreeBSD (but they keep the login class
in the kernel).

 - todd

Index: usr.bin/id/id.1
===================================================================
RCS file: /cvs/src/usr.bin/id/id.1,v
retrieving revision 1.17
diff -u -p -u -r1.17 id.1
--- usr.bin/id/id.1     3 Sep 2010 11:09:29 -0000       1.17
+++ usr.bin/id/id.1     18 May 2015 16:47:46 -0000
@@ -43,6 +43,9 @@
 .Nm id
 .Op Ar user
 .Nm id
+.Fl c
+.Op Ar user
+.Nm id
 .Fl G Op Fl n
 .Op Ar user
 .Nm id
@@ -70,6 +73,9 @@ In this case, the real and effective IDs
 .Pp
 The options are as follows:
 .Bl -tag -width Ds
+.It Fl c
+Display the login class of the real user ID or the specified
+.Ar user .
 .It Fl G
 Display the different group IDs (effective, real and supplementary)
 as whitespace separated numbers, in no particular order.
Index: usr.bin/id/id.c
===================================================================
RCS file: /cvs/src/usr.bin/id/id.c,v
retrieving revision 1.22
diff -u -p -u -r1.22 id.c
--- usr.bin/id/id.c     16 Jan 2015 06:40:08 -0000      1.22
+++ usr.bin/id/id.c     18 May 2015 21:10:13 -0000
@@ -38,6 +38,7 @@
 #include <string.h>
 #include <unistd.h>
 #include <limits.h>
+#include <login_cap.h>
 
 void   current(void);
 void   pretty(struct passwd *);
@@ -52,12 +53,12 @@ main(int argc, char *argv[])
 {
        struct group *gr;
        struct passwd *pw;
-       int Gflag, ch, gflag, nflag, pflag, rflag, uflag;
+       int ch, cflag, Gflag, gflag, nflag, pflag, rflag, uflag;
        uid_t uid;
        gid_t gid;
        const char *opts;
 
-       Gflag = gflag = nflag = pflag = rflag = uflag = 0;
+       cflag = Gflag = gflag = nflag = pflag = rflag = uflag = 0;
 
        if (strcmp(getprogname(), "groups") == 0) {
                Gflag = 1;
@@ -72,10 +73,13 @@ main(int argc, char *argv[])
                if (argc > 1)
                        usage();
        } else
-               opts = "Ggnpru";
+               opts = "cGgnpru";
 
        while ((ch = getopt(argc, argv, opts)) != -1)
                switch(ch) {
+               case 'c':
+                       cflag = 1;
+                       break;
                case 'G':
                        Gflag = 1;
                        break;
@@ -101,7 +105,7 @@ main(int argc, char *argv[])
        argc -= optind;
        argv += optind;
 
-       switch (Gflag + gflag + pflag + uflag) {
+       switch (cflag + Gflag + gflag + pflag + uflag) {
        case 1:
                break;
        case 0:
@@ -117,6 +121,16 @@ main(int argc, char *argv[])
 
        pw = *argv ? who(*argv) : NULL;
 
+       if (cflag) {
+               if (pw == NULL)
+                       pw = getpwuid(getuid());
+               if (pw != NULL && pw->pw_class != NULL && *pw->pw_class != '\0')
+                       (void)printf("%s\n", pw->pw_class);
+               else
+                       (void)printf("%s\n", LOGIN_DEFCLASS);
+               exit(0);
+       }
+
        if (gflag) {
                gid = pw ? pw->pw_gid : rflag ? getgid() : getegid();
                if (nflag && (gr = getgrgid(gid)))
@@ -325,6 +339,7 @@ usage(void)
                (void)fprintf(stderr, "usage: whoami\n");
        } else {
                (void)fprintf(stderr, "usage: id [user]\n");
+               (void)fprintf(stderr, "       id -c [user]\n");
                (void)fprintf(stderr, "       id -G [-n] [user]\n");
                (void)fprintf(stderr, "       id -g [-nr] [user]\n");
                (void)fprintf(stderr, "       id -p [user]\n");

Reply via email to