Blame manual/examples/db.c

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