Featured Post

Monday, March 28, 2016

Retrieving User's Information using getlogin() and getpwnam() system calls


            /*getlogin() and getpwnam() system calls */
  

 Information about user is stored in the file /etc/passwd.. User information like login name(user name), User ID, Group ID, Home directory , shell program can be retrieved using getpwnam() system call.

getlogin() system call:          
     The function getlogin() returns a string holding the login name. It returns NULL on failure.

syntax:
                                    char *getlogin(void);


 /* UNIX program to retrieve the login name using getlogin() system call*/

#include<stdio.h>
void main()
{
     char *lname;
     lname=getlogin();    /* getlogin() returns a string holding login name */
     if(lname==NULL)
    {
      printf("cant find the login name");
      exit(1);
    }
    else
    {
       printf("\n Login name is %s\n",lname);
    }
}


Output:

 Login name is anil


Note:    getlogin() system call is used to retrieve only information like user name. Also getlogin() system call doest not work all the time. Better to user getpwnam() system call to retrieve all the information about user

getpwnam() system call:
     
          Information about users is stored in the file /etc/passwd. The function getpwnam() is used to retrieve all the information like user name, user ID, Group Id, Home directory etc

   passwd has the following structure to get information about user


struct passwd
{
      char *pw_name;                  /* Username*/
      char *pw_passwd;               /* Password*/
      char *pw_uid;                      /* User ID*/
      char *pw_gid;                      /* Group ID*/
      char *pw_dir;                      /* Home Directory*/
      char *pw_shell                    /*Shell program*/
};

struct passwd *getpwnam(char *name);


/* UNIX program to get user information using getpwnam() system call*/

#include<stdio.h>
#include<pwd.h>
void main()
{
   struct passwd *user;
   char *name="anil";
   if((pass=getpwnam(name))!=NULL)
   {
      printf("User name of %s is %s\n",name,user->pw_name);
               /* user name */
      printf("Home directory of %s is %s\n",user->pw_dir);                                /*user's Home Directory */
      printf("User ID os %s is %d",name,user->pw_uid);
                 /* user ID */
      printf("Group ID of %s is %s",name,user->pw_gid); 
                /* Group ID */
    }
    else
   {
     printf("\n cant find user information");
     exit(1);
    }
}


Ouput:
   User nameos anil is anil
   Home Directory of anil is /home/netsem2014
   User ID of anil is 1000
   Group ID  of anil is 1000


Unix operating system Linux operating system shell programming bourn shell kourne shell c shell difference between unix and linux operating system , history of UNIX, red hat operating system. UNIX OS architecture , kernel utility programs , file handling process handling inter process communication pipes shared memory message queues socket programming TCP/IP , UDP programming , One to One chat Application , Basic shell program to add two numbers , echo function , while loops clear gt lt le ge ne eq if fi while do done case array in unix strings , cut copy paste cat dir pwd getcwd chdir command read write stat lseek fstat flock mechanism.even or add program vi editor command ni vi editor command mode esc mode insert mode aa dd x :q :wq :w. User Identification , User Information using getlogin() and getpwnam() system call. getpwnam() structure getpwnam() uses, etc/passwd file. program to get user information using getlogin() and getpwnam() system call. getlogin() not working
       


No comments:

Post a Comment