comparison toys/id.c @ 413:12add511705e

Add id command from Tim Bird.
author Rob Landley <rob@landley.net>
date Fri, 27 Jan 2012 06:49:28 -0600
parents
children 74f152c5fa30
comparison
equal deleted inserted replaced
412:2b521c791e4e 413:12add511705e
1 /* vi: set sw=4 ts=4:
2 *
3 * id.c - print real and effective user and group IDs
4 *
5 * Copyright 2012 Sony Network Entertainment, Inc.
6 *
7 * by Tim Bird <tim.bird@am.sony.com>
8 *
9 * See http://www.opengroup.org/onlinepubs/009695399/utilities/id.html
10
11 USE_ID(NEWTOY(id, "gru", TOYFLAG_BIN))
12
13 config ID
14 bool "id"
15 default y
16 help
17 usage: id [-gru]
18
19 Print user and group ID.
20
21 -g Show only the effective group ID
22 -r Show real ID instead of effective ID
23 -u Show only the effective user ID
24 */
25
26 #include "toys.h"
27
28 #define FLAG_g (1<<2)
29 #define FLAG_r (1<<1)
30 #define FLAG_u 1
31
32 void id_main(void)
33 {
34 int flags = toys.optflags;
35
36 uid_t uid;
37 gid_t gid;
38
39 /* show effective, unless user specifies real */
40 uid = geteuid();
41 gid = getegid();
42
43 if (flags & FLAG_r) {
44 uid = getuid();
45 gid = getgid();
46 }
47 if (flags & FLAG_u) {
48 printf("%d\n", uid);
49 return;
50 }
51 if (flags & FLAG_g) {
52 printf("%d\n", gid);
53 return;
54 }
55 printf("%d %d\n", uid, gid);
56 }