Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Analysis Using SQL And Excel

Authors: Gordon S Linoff

2nd Edition

111902143X, 9781119021438

More Books

Students also viewed these Databases questions

Question

What are the Five Phases of SDLC? Explain each briefly.

Answered: 1 week ago

Question

How can Change Control Procedures manage Project Creep?

Answered: 1 week ago