[comp.sys.hp] GETUTENT /ETC/UTMP NOT WORKING

tundra@ux1.cso.uiuc.edu (John Kemp) (09/18/90)

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.

Any suggestions?
(Test program and output listed below)

--------  john kemp            (  (  )_  internet - kemp@uiatma.atmos.uiuc.edu
  -----                       (  (   __)   decnet - uiatmb::kemp
   ---    univ of illinois   (_ (   __)    bitnet - {uunet,convex}
   --     dept of atmos sci  .(____).               !uiucuxc!uiatma!kemp
   -      105 s gregory ave    ...          phone - (217) 333-6881
    -     urbana, il 61801    ...             fax - (217) 444-4393

********************
SAMPLE PROGRAM
********************

/*
 * Print out /etc/utmp records.
 */
#include <stdio.h>
#include <sys/types.h>
#include <utmp.h>
struct utmp *ut;
main()
{
        utmpname("/etc/utmp");
        setutent();
        while ( (ut = getutent()) != NULL ) {
                fprintf(stderr,"user: %8c \n",ut->ut_user);
                fprintf(stderr,"id:   %4c \n",ut->ut_id);
                fprintf(stderr,"line: %12c\n",ut->ut_line);
                fprintf(stderr,"pid:  %d  \n",ut->ut_pid);
                fprintf(stderr,"type: %d  \n",ut->ut_type);
                fprintf(stderr,"host: %16c\n",ut->ut_host);
        }
}

********************
OUTPUT:
********************
user:        8
id:      @
line:            D
pid:  0
type: 2
host:                `
user:        8
id:      @
line:            D
pid:  0
type: 1
host:                `
Add Nauseum with PID's incresing and so on...

jad@hpcndnm.hp-sdd (John A Dilley) (09/18/90)

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 <stdio.h>
#include <sys/types.h>
#include <utmp.h>
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      --