1 /* id - return uid and gid Author: John J. Marco */
3 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
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>
17 int main(int argc
, char *argv
[])
21 uid_t ruid
, euid
, uid
;
22 gid_t rgid
, egid
, gid
;
31 gid_t groups
[NGROUPS_MAX
];
34 # define groups (&rgid)
39 int c
, uopt
= 0, gopt
= 0, nopt
= 0, ropt
= 0;
42 get6id(&ruid
, &euid
, &suid
, &rgid
, &egid
, &sgid
);
52 ngroups
= getgroups(NGROUPS_MAX
, groups
);
55 while((c
= getopt(argc
, argv
, "ugnr")) != EOF
) {
70 fprintf(stderr
, "%s: unrecognized option\n", argv
[0]);
76 fprintf(stderr
, "%s: cannot combine -u and -g\n", argv
[0]);
80 if((nopt
|| ropt
) && !uopt
&& !gopt
) {
81 fprintf(stderr
, "%s: cannot use -n or -r without -u or -g\n", argv
[0]);
86 uid
= ropt
? ruid
: euid
;
87 if (!nopt
|| (pwd
= getpwuid(uid
)) == NULL
)
90 printf("%s\n", pwd
->pw_name
);
94 gid
= ropt
? rgid
: egid
;
95 if (!nopt
|| (grp
= getgrgid(gid
)) == NULL
)
98 printf("%s\n", grp
->gr_name
);
102 if ((pwd
= getpwuid(ruid
)) == NULL
)
103 printf("uid=%d", ruid
);
105 printf("uid=%d(%s)", ruid
, pwd
->pw_name
);
107 if ((grp
= getgrgid(rgid
)) == NULL
)
108 printf(" gid=%d", rgid
);
110 printf(" gid=%d(%s)", rgid
, grp
->gr_name
);
113 if ((pwd
= getpwuid(euid
)) != NULL
)
114 printf(" euid=%d(%s)", euid
, pwd
->pw_name
);
116 printf(" euid=%d", euid
);
119 if ((grp
= getgrgid(egid
)) != NULL
)
120 printf(" egid=%d(%s)", egid
, grp
->gr_name
);
122 printf(" egid=%d", egid
);
125 if ((pwd
= getpwuid(suid
)) != NULL
)
126 printf(" suid=%d(%s)", suid
, pwd
->pw_name
);
128 printf(" suid=%d", suid
);
131 if ((grp
= getgrgid(sgid
)) != NULL
)
132 printf(" sgid=%d(%s)", sgid
, grp
->gr_name
);
134 printf(" sgid=%d", sgid
);
137 printf(" issetugid");
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
]);
147 printf("%d(%s)", groups
[g
], grp
->gr_name
);