# HG changeset patch # User Rob Landley # Date 1327668568 21600 # Node ID 12add511705e4c6c3a4e28c6050aa6e0e3e6198d # Parent 2b521c791e4ee63b81b30aaabc1b10d0774e6086 Add id command from Tim Bird. diff -r 2b521c791e4e -r 12add511705e toys/id.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/toys/id.c Fri Jan 27 06:49:28 2012 -0600 @@ -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 + * + * 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); +}