Update copyright notices with scripts/update-copyrights
[glibc.git] / manual / examples / db.c
bloba8ee9004af7d0deefaebafb9a798c330b07c3e74
1 /* User and Group Database Example
2 Copyright (C) 1991-2014 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, if not, see <http://www.gnu.org/licenses/>.
18 #include <grp.h>
19 #include <pwd.h>
20 #include <sys/types.h>
21 #include <unistd.h>
22 #include <stdlib.h>
24 int
25 main (void)
27 uid_t me;
28 struct passwd *my_passwd;
29 struct group *my_group;
30 char **members;
32 /* Get information about the user ID. */
33 me = getuid ();
34 my_passwd = getpwuid (me);
35 if (!my_passwd)
37 printf ("Couldn't find out about user %d.\n", (int) me);
38 exit (EXIT_FAILURE);
41 /* Print the information. */
42 printf ("I am %s.\n", my_passwd->pw_gecos);
43 printf ("My login name is %s.\n", my_passwd->pw_name);
44 printf ("My uid is %d.\n", (int) (my_passwd->pw_uid));
45 printf ("My home directory is %s.\n", my_passwd->pw_dir);
46 printf ("My default shell is %s.\n", my_passwd->pw_shell);
48 /* Get information about the default group ID. */
49 my_group = getgrgid (my_passwd->pw_gid);
50 if (!my_group)
52 printf ("Couldn't find out about group %d.\n",
53 (int) my_passwd->pw_gid);
54 exit (EXIT_FAILURE);
57 /* Print the information. */
58 printf ("My default group is %s (%d).\n",
59 my_group->gr_name, (int) (my_passwd->pw_gid));
60 printf ("The members of this group are:\n");
61 members = my_group->gr_mem;
62 while (*members)
64 printf (" %s\n", *(members));
65 members++;
68 return EXIT_SUCCESS;