Back to index

glibc  2.9
testgrp.c
Go to the documentation of this file.
00001 #include <grp.h>
00002 #include <pwd.h>
00003 #include <sys/types.h>
00004 #include <unistd.h>
00005 #include <stdlib.h>
00006 #include <stdio.h>
00007 
00008 int
00009 main (int argc, char *argv[])
00010 {
00011   uid_t me;
00012   struct passwd *my_passwd;
00013   struct group *my_group = NULL;
00014   char **members;
00015 
00016   me = getuid ();
00017   my_passwd = getpwuid (me);
00018   if (my_passwd == NULL)
00019     printf ("Cannot find user entry for UID %d\n", me);
00020   else
00021     {
00022       printf ("My login name is %s.\n", my_passwd->pw_name);
00023       printf ("My uid is %d.\n", (int)(my_passwd->pw_uid));
00024       printf ("My home directory is %s.\n", my_passwd->pw_dir);
00025       printf ("My default shell is %s.\n", my_passwd->pw_shell);
00026 
00027       my_group = getgrgid (my_passwd->pw_gid);
00028       if (my_group == NULL)
00029        printf ("No data for group %d found\n", my_passwd->pw_gid);
00030       else
00031        {
00032          printf ("My default group is %s (%d).\n",
00033                 my_group->gr_name, (int)(my_passwd->pw_gid));
00034          printf ("The members of this group are:\n");
00035          for (members = my_group->gr_mem; *members != NULL; ++members)
00036            printf ("  %s\n", *members);
00037        }
00038     }
00039 
00040   return my_passwd && my_group ? EXIT_SUCCESS : EXIT_FAILURE;
00041 }