1 /* `id.c` - return user identity
2 Copyright (c) 2022, Alan Potteiger
3 See `LICENSE` for copyright and license details */
5 #define _POSIX_C_SOURCE 200809L
13 static const char *usage
= {
16 "id -g [-nr] [user]\n"
17 "id -u [-nr] [user]\n"
21 main(int argc
, char *argv
[])
24 int all
, Gflag
, gflag
, nflag
, rflag
, uflag
;
34 all
= Gflag
= gflag
= uflag
= nflag
= rflag
= 0;
40 while ((ch
= getopt(argc
, argv
, "Ggunr")) != -1) {
64 /* some options should be mutually exclusive */
65 if ((Gflag
&& gflag
) || (Gflag
&& uflag
) || (gflag
&& uflag
)) {
69 /* -n and -r are useless without any of the main flags */
70 if (Gflag
== 0 && gflag
== 0 && uflag
== 0) {
71 if (nflag
== 1 || rflag
== 1) {
75 if (nflag
== 0 && rflag
== 0)
82 /* obtain passwd struct if user name provided */
91 fprintf(stderr
, "id: user %s does not exist\n",
100 fputs("id: cannot obtain login information\n", stderr
);
104 /* TODO: no flag functionality */
113 /* TODO: Gflag functionality */
128 printf("%s\n", grp
->gr_name
);
138 printf("%s\n", user
);