diff options
author | Rob Landley <rob@landley.net> | 2012-01-27 06:49:28 -0600 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2012-01-27 06:49:28 -0600 |
commit | 95c5099f29e946da0c211b9f0d50a29cb8245887 (patch) | |
tree | 3aab12c79b2466135fad5756f667efce47934fe4 /toys/id.c | |
parent | 48522dfd8227e3de1e3556bb58d4261964ef84d8 (diff) | |
download | toybox-95c5099f29e946da0c211b9f0d50a29cb8245887.tar.gz |
Add id command from Tim Bird.
Diffstat (limited to 'toys/id.c')
-rw-r--r-- | toys/id.c | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/toys/id.c b/toys/id.c new file mode 100644 index 00000000..f051189b --- /dev/null +++ b/toys/id.c @@ -0,0 +1,56 @@ +/* vi: set sw=4 ts=4: + * + * id.c - print real and effective user and group IDs + * + * Copyright 2012 Sony Network Entertainment, Inc. + * + * by Tim Bird <tim.bird@am.sony.com> + * + * See http://www.opengroup.org/onlinepubs/009695399/utilities/id.html + +USE_ID(NEWTOY(id, "gru", TOYFLAG_BIN)) + +config ID + bool "id" + default y + help + usage: id [-gru] + + Print user and group ID. + + -g Show only the effective group ID + -r Show real ID instead of effective ID + -u Show only the effective user ID +*/ + +#include "toys.h" + +#define FLAG_g (1<<2) +#define FLAG_r (1<<1) +#define FLAG_u 1 + +void id_main(void) +{ + int flags = toys.optflags; + + uid_t uid; + gid_t gid; + + /* show effective, unless user specifies real */ + uid = geteuid(); + gid = getegid(); + + if (flags & FLAG_r) { + uid = getuid(); + gid = getgid(); + } + if (flags & FLAG_u) { + printf("%d\n", uid); + return; + } + if (flags & FLAG_g) { + printf("%d\n", gid); + return; + } + printf("%d %d\n", uid, gid); +} |