2 ** Copyright (c) 2004 OBOS
3 ** Permission is hereby granted, free of charge, to any person obtaining a copy
4 ** of this software and associated documentation files (the "Software"), to deal
5 ** in the Software without restriction, including without limitation the rights
6 ** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 ** copies of the Software, and to permit persons to whom the Software is
8 ** furnished to do so, subject to the following conditions:
10 ** The above copyright notice and this permission notice shall be included in all
11 ** copies or substantial portions of the Software.
13 ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 ** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 ** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 ** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 ** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 ** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 static void print_user_info(int userID
, char *suffix
);
34 static void print_group_info(int groupID
, char *suffix
);
35 static void print_group_list(int listID
);
36 static void print_combined_info(void);
37 static void suggest_help(void);
38 static void usage(void);
39 static void version(void);
42 static int gFlag
, glFlag
, nFlag
, rFlag
, uFlag
;
43 struct passwd
*euidName
;
44 struct passwd
*ruidName
;
45 struct group
*egidName
;
46 struct group
*rgidName
;
56 print_user_info(int userID
, char *suffix
) {
57 struct stat statBuffer
;
58 struct passwd
*userIDName
;
59 if ((userIDName
= getpwuid(userID
)) != NULL
) {
61 fprintf(stdout
, "%s%s", userIDName
->pw_name
, suffix
);
63 fprintf(stdout
, "%u%s", eUID
, suffix
);
65 fprintf(stdout
, "%-8d%s", statBuffer
.st_uid
, suffix
);
70 print_group_info(int groupID
, char *suffix
) {
71 struct stat statBuffer
;
72 struct group
*groupIDName
;
73 if ((groupIDName
= getgrgid(groupID
)) != NULL
) {
75 fprintf(stdout
, "%s%s", groupIDName
->gr_name
, suffix
);
77 fprintf(stdout
, "%u%s", groupID
, suffix
);
79 fprintf(stdout
, " %-8d%s", statBuffer
.st_gid
, suffix
);
83 print_group_list(int groupID
) {
84 int cnt
, id
, lastID
, nGroups
;
88 ngroups_max
= sysconf(NGROUPS_MAX
) + 1;
89 groups
= (gid_t
*)malloc(ngroups_max
*sizeof(gid_t
));
91 nGroups
= getgroups(ngroups_max
, groups
);
94 print_group_info(groupID
, suffix
);
95 for (lastID
= -1, cnt
= 0; cnt
< ngroups_max
; ++cnt
) {
96 if (lastID
== (id
= groups
[cnt
]))
99 print_group_info(id
, suffix
);
102 fprintf(stdout
, "\n");
107 print_combined_info() {
108 if ( eUID
!= rUID
) {
111 fprintf(stdout
, "uid=");
112 print_user_info(rUID
, suffix
);
113 fprintf(stdout
, "(");
115 print_user_info(rUID
, suffix
);
116 fprintf(stdout
, ") gid=");
117 rFlag
= 1; nFlag
= 0;
118 print_group_info(rGID
, suffix
);
119 fprintf(stdout
, "(");
120 rFlag
= 0; nFlag
= 1;
121 print_group_info(eGID
, suffix
);
122 fprintf(stdout
, ") euid=");
123 rFlag
= 0; nFlag
= 0;
124 print_user_info(eUID
, suffix
);
125 fprintf(stdout
, "(");
126 rFlag
= 0; nFlag
= 1;
127 print_user_info(eUID
, suffix
);
128 fprintf(stdout
, ")\n");
132 fprintf(stdout
, "uid=");
133 print_user_info(rUID
, suffix
);
134 fprintf(stdout
, "(");
136 print_user_info(rUID
, suffix
);
137 fprintf(stdout
, ") gid=");
138 rFlag
= 1; nFlag
= 0;
139 print_group_info(rGID
, suffix
);
140 fprintf(stdout
, "(");
141 rFlag
= 1; nFlag
= 1;
142 print_group_info(rGID
, suffix
);
143 fprintf(stdout
, ")\n");
151 (void)fprintf(stdout
, "Try `%s --help' for more information.\n", progName
);
159 "%s OBOS (http://www.openbeos.org/)
161 Usage: %s [OPTION]... [USERNAME]
163 -g, --group print only the group ID
164 -G, --groups print only the supplementary groups
165 -n, --name print a name instead of a number, for -ugG
166 -r, --real print the real ID instead of effective ID, for -ugG
167 -u, --user print only the user ID
168 --help display this help and exit
169 --version output version information and exit
171 Print information for USERNAME, or the current user.
173 ", progName
, progName
);
180 fprintf(stdout
, "%s OBOS (http://www.openbeos.org/)\n", progName
);
185 main(int argc
, char *argv
[])
189 char * const * optargv
= argv
;
191 struct option groupOption
= { "group", no_argument
, 0, 1 } ;
192 struct option groupsOption
= { "groups", no_argument
, 0, 2 } ;
193 struct option nameOption
= { "name", no_argument
, 0, 3 } ;
194 struct option realOption
= { "real", no_argument
, 0, 4 } ;
195 struct option userOption
= { "user", no_argument
, 0, 5 } ;
196 struct option helpOption
= { "help", no_argument
, 0, 6 } ;
197 struct option versionOption
= { "version", no_argument
, 0, 7 } ;
199 struct option options
[] = {
200 groupOption
, groupsOption
, nameOption
, realOption
, userOption
, helpOption
, versionOption
, {0}
203 struct passwd
*suppliedName
;
205 gFlag
= glFlag
= nFlag
= rFlag
= uFlag
= 0;
206 progName
= argv
[0]; // don't put this before or between structs! werrry bad things happen. ;)
208 while ((argOption
= getopt_long(argc
, optargv
, "gGnru", options
, &indexptr
)) != -1) {
227 switch (options
[indexptr
].val
) {
242 if (argc
- optind
> 1)
245 if (argc
- optind
== 1) {
246 suppliedName
= getpwnam(argv
[optind
]);
248 if (suppliedName
== NULL
) {
249 fprintf(stderr
, "%s: %s: No such user\n", progName
, argv
[optind
]);
253 rUID
= eUID
= suppliedName
->pw_uid
;
254 rGID
= eGID
= suppliedName
->pw_gid
;
261 euidName
= getpwuid(eUID
);
262 ruidName
= getpwuid(rUID
);
263 egidName
= getgrgid(eGID
);
264 rgidName
= getgrgid(rGID
);
267 if ( gFlag
+ glFlag
+ uFlag
> 1 ) {
268 fprintf(stderr
, "%s: cannot print only user and only group\n", progName
);
273 if ( gFlag
+ glFlag
+ uFlag
== 0 && (rFlag
|| nFlag
)) {
274 fprintf(stderr
, "%s: cannot print only names or real IDs in default format\n", progName
);
283 print_group_info(rUID
, suffix
);
285 print_group_info(eUID
, suffix
);
292 print_group_list(rUID
);
294 print_group_list(eUID
);
302 print_user_info(rUID
, suffix
);
304 print_user_info(eUID
, suffix
);
308 // no arguments? print combined info.
309 print_combined_info();