forget difference between big and small commands - obsolete with vm.
[minix.git] / commands / simple / id.c
blobb1b044f97c5abe3b6fcb80e91caf9f6729be80dc
1 /* id - return uid and gid Author: John J. Marco */
3 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
4 /* ----- id.c ----- */
5 /* Id - get real and effective user id and group id */
6 /* Author: John J. Marco */
7 /* pa1343@sdcc15.ucsd.edu */
8 /*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
10 #include <sys/types.h>
11 #include <pwd.h>
12 #include <grp.h>
13 #include <unistd.h>
14 #include <stdio.h>
15 #include <limits.h>
17 int main(int argc, char *argv[])
19 struct passwd *pwd;
20 struct group *grp;
21 uid_t ruid, euid, uid;
22 gid_t rgid, egid, gid;
23 #if __minix_vmd
24 uid_t suid;
25 gid_t sgid;
26 #else
27 # define suid ruid
28 # define sgid rgid
29 #endif
30 #if NGROUPS_MAX > 0
31 gid_t groups[NGROUPS_MAX];
32 int ngroups;
33 #else
34 # define groups (&rgid)
35 # define ngroups 0
36 #endif
37 int g;
38 int isug;
39 int c, uopt = 0, gopt = 0, nopt = 0, ropt = 0;
41 #if __minix_vmd
42 get6id(&ruid, &euid, &suid, &rgid, &egid, &sgid);
43 isug = issetugid();
44 #else
45 ruid = getuid();
46 euid = geteuid();
47 rgid = getgid();
48 egid = getegid();
49 isug = 0;
50 #endif
51 #if NGROUPS_MAX > 0
52 ngroups = getgroups(NGROUPS_MAX, groups);
53 #endif
55 while((c = getopt(argc, argv, "ugnr")) != EOF) {
56 switch(c) {
57 case 'u':
58 uopt = 1;
59 break;
60 case 'g':
61 gopt = 1;
62 break;
63 case 'n':
64 nopt = 1;
65 break;
66 case 'r':
67 ropt = 1;
68 break;
69 default:
70 fprintf(stderr, "%s: unrecognized option\n", argv[0]);
71 return(1);
75 if(uopt && gopt) {
76 fprintf(stderr, "%s: cannot combine -u and -g\n", argv[0]);
77 return 1;
80 if((nopt || ropt) && !uopt && !gopt) {
81 fprintf(stderr, "%s: cannot use -n or -r without -u or -g\n", argv[0]);
82 return 1;
85 if(uopt) {
86 uid = ropt ? ruid : euid;
87 if (!nopt || (pwd = getpwuid(uid)) == NULL)
88 printf("%u\n", uid);
89 else
90 printf("%s\n", pwd->pw_name);
91 return 0;
93 if(gopt) {
94 gid = ropt ? rgid : egid;
95 if (!nopt || (grp = getgrgid(gid)) == NULL)
96 printf("%u\n", gid);
97 else
98 printf("%s\n", grp->gr_name);
99 return 0;
102 if ((pwd = getpwuid(ruid)) == NULL)
103 printf("uid=%d", ruid);
104 else
105 printf("uid=%d(%s)", ruid, pwd->pw_name);
107 if ((grp = getgrgid(rgid)) == NULL)
108 printf(" gid=%d", rgid);
109 else
110 printf(" gid=%d(%s)", rgid, grp->gr_name);
112 if (euid != ruid)
113 if ((pwd = getpwuid(euid)) != NULL)
114 printf(" euid=%d(%s)", euid, pwd->pw_name);
115 else
116 printf(" euid=%d", euid);
118 if (egid != rgid)
119 if ((grp = getgrgid(egid)) != NULL)
120 printf(" egid=%d(%s)", egid, grp->gr_name);
121 else
122 printf(" egid=%d", egid);
124 if (suid != euid)
125 if ((pwd = getpwuid(suid)) != NULL)
126 printf(" suid=%d(%s)", suid, pwd->pw_name);
127 else
128 printf(" suid=%d", suid);
130 if (sgid != egid)
131 if ((grp = getgrgid(sgid)) != NULL)
132 printf(" sgid=%d(%s)", sgid, grp->gr_name);
133 else
134 printf(" sgid=%d", sgid);
136 if (isug) {
137 printf(" issetugid");
140 if (ngroups > 0) {
141 printf(" groups=");
142 for (g = 0; g < ngroups; g++) {
143 if (g > 0) fputc(',', stdout);
144 if ((grp = getgrgid(groups[g])) == NULL)
145 printf("%d", groups[g]);
146 else
147 printf("%d(%s)", groups[g], grp->gr_name);
151 printf("\n");
152 return(0);