A C based Program to get the Details of the user in Unix

anurag_pareek 0 Tallied Votes 1,000 Views Share

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);
}
bumsfeld 413 Nearly a Posting Virtuoso

Twice is better? You must be shiddin us!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.