Start New Discussion within our Software Development Community

This Program gives the UserDetails of the logged in User.
a) The RealName
b) Login Name ( Why do we need this?)
c) Home Directory
d) Default Login Shell

Additional Purpose of this code would be to invoke/ learn at man getpwuid and about struct passswd

/* Program to get the Details of the user like User's Real Name, Username and Shell */

#include <pwd.h>        /* getpwdid */
#include <sys/types.h>
#include <unistd.h> 
#include <stdio.h>

void user_details(void);

int main(void)
{
	user_details();
	
	return 0;
}

void user_details(void)
{
	struct passwd *passwd;           /* man getpwuid */
	passwd = getpwuid ( getuid());   /* Get the uid of the running processand use it to get a record from /etc/passwd */

	printf("\n The Real User Name is %s ", passwd->pw_gecos);
	printf("\n The Login Name is %s ", passwd->pw_name);
	printf("\n The Home Directory is %s", passwd->pw_dir);
	printf("\n The Login Shell is %s ", passwd->pw_shell);
	printf("\n The Passwd is %s ", getpwuid(getuid()) >pw_passwd);
	printf("\n The uid is %lu ", (unsigned long) getpwuid(getuid())->pw_uid);
	printf("\n The gid is %lu \n\n", (unsigned long) getpwuid(getuid())->pw_gid);
}

/* Program to get the Details of the user like User's Real Name, Username and Shell */

#include <pwd.h>        /* getpwdid */
#include <sys/types.h>
#include <unistd.h> 
#include <stdio.h>

void user_details(void);

int main(void)
{
	user_details();
	
	return 0;
}

void user_details(void)
{
	struct passwd *passwd;           /* man getpwuid */
	passwd = getpwuid ( getuid());   /* Get the uid of the running processand use it to get a record from /etc/passwd */

	printf("\n The Real User Name is %s ", passwd->pw_gecos);
	printf("\n The Login Name is %s ", passwd->pw_name);
	printf("\n The Home Directory is %s", passwd->pw_dir);
	printf("\n The Login Shell is %s ", passwd->pw_shell);
	printf("\n The Passwd is %s ", getpwuid(getuid())->pw_passwd);
	printf("\n The uid is %lu ", (unsigned long) getpwuid(getuid())->pw_uid);
	printf("\n The gid is %lu \n\n", (unsigned long) getpwuid(getuid())->pw_gid);
}