Path: utzoo!attcan!uunet!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!sdd.hp.com!hp-sdd!jad From: jad@hpcndnm.hp-sdd (John A Dilley) Newsgroups: comp.sys.hp Subject: Re: GETUTENT /ETC/UTMP NOT WORKING(?) Message-ID: Date: 17 Sep 90 19:27:35 GMT References: <1990Sep17.172858.29044@ux1.cso.uiuc.edu> Sender: news@sdd.hp.com (Usenet News) Organization: Hewlett Packard, Colorado Networks Division Lines: 61 In-Reply-To: tundra@ux1.cso.uiuc.edu's message of Mon, 17 Sep 90 17:28:58 GMT Nntp-Posting-Host: hpcndnm.cnd.hp.com In article <1990Sep17.172858.29044@ux1.cso.uiuc.edu> tundra@ux1.cso.uiuc.edu (John Kemp) writes: Is this a bug or am I doing something stupid? How does one go about scanning the list of currently logged in users? I thought "getutent" as the way to do it. Unfortunately, this call produces garbage in the "ut_user" and "ut_line" fields. Yes, the getutent() library call is the way to programmatically read your /etc/utmp file. The problem with your program was that you were printing the contents of character string fields as a character (e.g., using format string "%8c") instead of as a string (as in "%8s"). With minor change the program dumps the contents as expected. Following is your program with a slight modification to not print several fields valid only for user processes unless the ut_type == USER_PROCESS. It's not terribly sophisticated (for "pretty" formatting of /etc/utmp, use the commands "last", "who", "finger", etc...). /* * Print out /etc/utmp records. */ #include #include #include struct utmp *ut, *getutent(); main() { utmpname("/etc/utmp"); setutent(); while ( (ut = getutent()) != NULL ) { ut->ut_host[15]= '\0'; fprintf(stderr,"type:\t%d\n",ut->ut_type); fprintf(stderr,"line:\t%12s\n",ut->ut_line); if (ut->ut_type == USER_PROCESS) { fprintf(stderr,"user:\t%8s\n",ut->ut_user); fprintf(stderr,"id:\t%4s\n",ut->ut_id); fprintf(stderr,"pid:\t%d\n",ut->ut_pid); fprintf(stderr,"host:\t%16s\n",ut->ut_host); } fprintf(stderr, "\n"); } exit(0); } Regards, -- jad -- John DILLEY Hewlett-Packard Colorado Networks Division UX-mail: jad@cnd.hp.com Phone: (303) 229-2787 -- This is not an official statement of Hewlett-Packard Corp, and does not necessarily reflect the views of HP. The information above is provided completely without warranty of any kind. -- jad --