1
0
mirror of https://sourceware.org/git/glibc.git synced 2025-10-27 12:15:39 +03:00
Files
glibc/manual/examples/db.c
Collin Funk a86118d661 manual: Fix missing include in group and user database example.
Previously this file would fail to compile with the following error:

    $ gcc manual/examples/db.c
    db.c: In function ‘main’:
    db.c:37:7: error: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]
       37 |       printf ("Couldn't find out about user %d.\n", (int) me);
          |       ^~~~~~
    db.c:23:1: note: include ‘<stdio.h>’ or provide a declaration of ‘printf’
       22 | #include <stdlib.h>
      +++ |+#include <stdio.h>
       23 |
    db.c:37:7: warning: incompatible implicit declaration of built-in function ‘printf’ [-Wbuiltin-declaration-mismatch]
       37 |       printf ("Couldn't find out about user %d.\n", (int) me);
          |       ^~~~~~
    db.c:37:7: note: include ‘<stdio.h>’ or provide a declaration of ‘printf’
    db.c:42:3: warning: incompatible implicit declaration of built-in function ‘printf’ [-Wbuiltin-declaration-mismatch]
       42 |   printf ("I am %s.\n", my_passwd->pw_gecos);
          |   ^~~~~~
    db.c:42:3: note: include ‘<stdio.h>’ or provide a declaration of ‘printf’

Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
2025-09-29 17:01:54 -07:00

71 lines
2.0 KiB
C

/* User and Group Database Example
Copyright (C) 1991-2025 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <https://www.gnu.org/licenses/>.
*/
#include <grp.h>
#include <pwd.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
int
main (void)
{
uid_t me;
struct passwd *my_passwd;
struct group *my_group;
char **members;
/* Get information about the user ID. */
me = getuid ();
my_passwd = getpwuid (me);
if (!my_passwd)
{
printf ("Couldn't find out about user %d.\n", (int) me);
exit (EXIT_FAILURE);
}
/* Print the information. */
printf ("I am %s.\n", my_passwd->pw_gecos);
printf ("My login name is %s.\n", my_passwd->pw_name);
printf ("My uid is %d.\n", (int) (my_passwd->pw_uid));
printf ("My home directory is %s.\n", my_passwd->pw_dir);
printf ("My default shell is %s.\n", my_passwd->pw_shell);
/* Get information about the default group ID. */
my_group = getgrgid (my_passwd->pw_gid);
if (!my_group)
{
printf ("Couldn't find out about group %d.\n",
(int) my_passwd->pw_gid);
exit (EXIT_FAILURE);
}
/* Print the information. */
printf ("My default group is %s (%d).\n",
my_group->gr_name, (int) (my_passwd->pw_gid));
printf ("The members of this group are:\n");
members = my_group->gr_mem;
while (*members)
{
printf (" %s\n", *(members));
members++;
}
return EXIT_SUCCESS;
}