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