3 getent.c -- getent utility simulation
4 Copyright (c) 2013 Kyle J. McKay. All rights reserved.
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 A very simple emulation of getent passwd and getent group. No other databases
24 are supported. Only a single key is supported.
26 USAGE: getent passwd|group key
27 Exit code 0 for success (and the /etc/passwd|/etc/group format line is written
28 to stdout), 1 for bad command, 2 for key not found.
29 Wrong number of or bad arguments exits with 1.
35 #include <sys/types.h>
39 int main(int argc
, char *argv
[])
50 if (!db
|| !*db
|| !key
|| !*key
)
53 if (!strcmp(db
, "passwd")) {
54 struct passwd
*pwent
= getpwnam(key
);
57 printf("%s:%s:%d:%d:%s:%s:%s\n", pwent
->pw_name
, pwent
->pw_passwd
,
58 (int)pwent
->pw_uid
, (int)pwent
->pw_gid
, pwent
->pw_gecos
, pwent
->pw_dir
,
61 } else if (!strcmp(db
, "group")) {
62 struct group
*grent
= getgrnam(key
);
64 char **memb
= grent
->gr_mem
;
66 printf("%s:%s:%d:", grent
->gr_name
, grent
->gr_passwd
, grent
->gr_gid
);
69 for (; *memb
; ++memb
) {