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

anurag_pareek 0 Tallied Votes 1K 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);
}

Dani AI

Generated

Good starter from — the snippet shows how to read a passwd entry with getpwuid. Two practical points before diving in: always check that getpwuid did not return NULL, and avoid printing the pw_passwd field (modern systems put encrypted passwords in the shadow file). Also, do not call getpwuid repeatedly — save the pointer and reuse it — and prefer the reentrant getpwuid_r in threaded programs. (man7.org)

Concrete, safe fixes to apply: use uid_t uid = getuid() (or geteuid() for effective UID in setuid programs), call getpwuid(uid) once and check for NULL, extract the "real name" from pw_gecos up to the first comma (the GECOS field often contains comma-separated items), and do not attempt to read real passwords from pw_passwd — those belong in /etc/shadow. When printing IDs cast to unsigned long for %lu in portable printf use. (man7.org)

Example pattern (safe, short):

uid_t uid = getuid();               /* or geteuid() */
struct passwd *pw = getpwuid(uid);
if (!pw) { perror("getpwuid"); return; }
char name[256]; strncpy(name, pw->pw_gecos, sizeof(name)-1);
if (char *c = strchr(name, ',')) *c = '\0';   /* keep only full name */
printf("user=%s full=\"%s\" home=%s shell=%s uid=%lu gid=%lu\n",
       pw->pw_name, name, pw->pw_dir, pw->pw_shell,
       (unsigned long)pw->pw_uid, (unsigned long)pw->pw_gid);

For details and edge cases consult the man pages: getpwuid(3p), passwd(5) and shadow(5). (man7.org)

(Note: was right to notice the duplicate post contents — the fixes above apply once; no need to post the same code twice.)

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.