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 #include #include #include #include #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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!