Question
Required to write a Unix command in C that returns the same information as Unix command whoami. Basically, I think I need to use the
Required to write a Unix command in C that returns the same information as Unix command whoami. Basically, I think I need to use the getuid() sys call and compare that to utmp to return the current user, like you'd get with the whoami function. However, I'm stuck in my code and have only made it this far:
#include
void show_info(struct utmp *);
int main() { struct utmp utbuf; /* read info into here */ int utmpfd; /* read from this descriptor */
if ( (utmpfd = open(UTMP_FILE, O_RDONLY)) == -1 ){ perror(UTMP_FILE); exit(1); }
while( read(utmpfd, &utbuf, sizeof(utbuf)) == sizeof(utbuf) ) show_info( &utbuf ); close(utmpfd); return 0; } /* * show info() * displays the contents of the utmp struct * in human readable form * * displays nothing if record has no user name */ void show_info( struct utmp *utbufp ) { if ( utbufp->ut_type != USER_PROCESS ) return;
printf("%-8.8s", utbufp->ut_name); //the logname
printf(" "); /* newline */ }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started