1/* Cut down version of getent which only returns passwd and group database 2 entries and seems to compile on most systems without too much fuss. 3 Original copyright notice below. */ 4 5/* Copyright (c) 1998, 1999, 2000 Free Software Foundation, Inc. 6 This file is part of the GNU C Library. 7 Contributed by Thorsten Kukuk <kukuk@suse.de>, 1998. 8 9 The GNU C Library is free software; you can redistribute it and/or 10 modify it under the terms of the GNU Library General Public License as 11 published by the Free Software Foundation; either version 2 of the 12 License, or (at your option) any later version. 13 14 The GNU C Library is distributed in the hope that it will be useful, 15 but WITHOUT ANY WARRANTY; without even the implied warranty of 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 Library General Public License for more details. 18 19 You should have received a copy of the GNU Library General Public 20 License along with the GNU C Library; see the file COPYING.LIB. If not, 21 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 22 Boston, MA 02111-1307, USA. */ 23 24#include <stdio.h> 25#include <pwd.h> 26#include <grp.h> 27 28group_keys (int number, char *key[]) 29{ 30 int result = 0; 31 int i; 32 33 for (i = 0; i < number; ++i) 34 { 35 struct group *grp; 36 37 if (isdigit (key[i][0])) 38 grp = getgrgid (atol (key[i])); 39 else 40 grp = getgrnam (key[i]); 41 42 if (grp == NULL) 43 result = 2; 44 else 45 print_group (grp); 46 } 47 48 return result; 49} 50 51passwd_keys (int number, char *key[]) 52{ 53 int result = 0; 54 int i; 55 56 for (i = 0; i < number; ++i) 57 { 58 struct passwd *pwd; 59 60 if (isdigit (key[i][0])) 61 pwd = getpwuid (atol (key[i])); 62 else 63 pwd = getpwnam (key[i]); 64 65 if (pwd == NULL) 66 result = 2; 67 else 68 print_passwd (pwd); 69 } 70 71 return result; 72} 73 74print_group (struct group *grp) 75{ 76 unsigned int i = 0; 77 78 printf ("%s:%s:%ld:", grp->gr_name ? grp->gr_name : "", 79 grp->gr_passwd ? grp->gr_passwd : "", 80 (unsigned long)grp->gr_gid); 81 82 while (grp->gr_mem[i] != NULL) 83 { 84 fputs (grp->gr_mem[i], stdout); 85 ++i; 86 if (grp->gr_mem[i] != NULL) 87 fputs (",", stdout); 88 } 89 fputs ("\n", stdout); 90} 91 92print_passwd (struct passwd *pwd) 93{ 94 printf ("%s:%s:%ld:%ld:%s:%s:%s\n", 95 pwd->pw_name ? pwd->pw_name : "", 96 pwd->pw_passwd ? pwd->pw_passwd : "", 97 (unsigned long)pwd->pw_uid, 98 (unsigned long)pwd->pw_gid, 99 pwd->pw_gecos ? pwd->pw_gecos : "", 100 pwd->pw_dir ? pwd->pw_dir : "", 101 pwd->pw_shell ? pwd->pw_shell : ""); 102} 103 104int main(int argc, char **argv) 105{ 106 switch(argv[1][0]) 107 { 108 case 'g': /* group */ 109 if (strcmp (argv[1], "group") == 0) 110 { 111 if (argc == 2) 112 { 113 struct group *grp; 114 115 setgrent (); 116 while ((grp = getgrent()) != NULL) 117 print_group (grp); 118 endgrent (); 119 } 120 else 121 return group_keys (argc - 2, &argv[2]); 122 } 123 else 124 goto error; 125 break; 126 127 case 'p': /* passwd, protocols */ 128 if (strcmp (argv[1], "passwd") == 0) 129 { 130 if (argc == 2) 131 { 132 struct passwd *pwd; 133 134 setpwent (); 135 while ((pwd = getpwent()) != NULL) 136 print_passwd (pwd); 137 endpwent (); 138 } 139 else 140 return passwd_keys (argc - 2, &argv[2]); 141 } 142 else 143 goto error; 144 break; 145 default: 146 error: 147 fprintf (stderr, "Unknown database: %s\n", argv[1]); 148 return 1; 149 } 150 return 0; 151} 152