gitweb/indextext_readonly.html: include sample readonly file
[girocco.git] / src / getent.c
blob73b9f834240b31ca67d0f49b5e64acd52e59e46a
1 /*
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.
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <sys/types.h>
36 #include <grp.h>
37 #include <pwd.h>
39 int main(int argc, char *argv[])
41 const char *db;
42 const char *key;
43 int found = 0;
45 if (argc != 3)
46 return 1;
48 db = argv[1];
49 key = argv[2];
50 if (!db || !*db || !key || !*key)
51 return 1;
53 if (!strcmp(db, "passwd")) {
54 struct passwd *pwent = getpwnam(key);
55 if (pwent) {
56 found = 1;
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,
59 pwent->pw_shell);
61 } else if (!strcmp(db, "group")) {
62 struct group *grent = getgrnam(key);
63 if (grent) {
64 char **memb = grent->gr_mem;
65 found = 1;
66 printf("%s:%s:%d:", grent->gr_name, grent->gr_passwd, grent->gr_gid);
67 if (memb) {
68 int first = 1;
69 for (; *memb; ++memb) {
70 if (first)
71 first = 0;
72 else
73 putchar(',');
74 fputs(*memb, stdout);
77 putchar('\n');
79 } else {
80 return 1;
82 return found ? 0 : 2;