[comp.sources.misc] v05i037: Provides access to utmp file from C

jeff@tekcsc.mkt.tek.com (Jeff Beadles) (11/09/88)

Posting-number: Volume 5, Issue 37
Submitted-by: "Jeff Beadles" <jeff@tekcsc.mkt.tek.com>
Archive-name: bsd.getut

[I take it this was redirected.  Note that it won't help you compile s5finger
on a non-System-V system:  getutline() isn't here.  ++bsa]

Here's something for comp.sources.unix.  It does have a copyright message,
but I think that it's more than reasonable.

Jeff Beadles  IDG Customer Support Center Tektronix, Inc.
jeff@tekcsc.MKT.TEK.COM -or-              POB 1000
 ..!tektronix!tekcsc.mkt.tek.com!jeff     Wilsonville, OR. 97070 MS 63-171


#--------------------------------CUT HERE-------------------------------------
#! /bin/sh
#
# This is a shell archive.  Save this into a file, edit it
# and delete all lines above this comment.  Then give this
# file to sh by executing the command "sh file".  The files
# will be extracted into the current directory owned by
# you with default permissions.
#
# The files contained herein are:
#
# -rw-r--r--  1 jeff         1462 Oct 31 17:36 myutmp.c
#
echo 'x - myutmp.c'
if test -f myutmp.c; then echo 'shar: not overwriting myutmp.c'; else
sed 's/^X//' << '________This_Is_The_END________' > myutmp.c
X/*
X *	myutmp.c  Copyright 1988, Jeff Beadles
X *	Permission is granted to this program where ever you desire as long
X *	as I am credited as the author.
X *
X *	(Does this exist somewhere?  I couldn't find a tool to do this
X *	function!)
X *
X *	This will make-up for the lack of {get,end,set}utent.  It is not a
X *	complete implimentation, but should get most people by.  I wrote it
X *	so I could compile and use "party", on a BSD system here.
X *
X *	No makefile is needed.  Just cc -c myutmp.c  This will NOT give an
X *	executable program.  It must be loaded into a program that requires
X *	getutent, endutent, setutent.
X *
X *	Question, comments, suggestions to jeff@tekcsc.MKT.TEK.COM.
X *	Flames to /dev/null.  (What do you expect for free?)
X *
X */
X
X#include <utmp.h>
X#include <stdio.h>
Xvoid setutent(), endutent();
Xstruct utmp *getutent();
Xint fp;
X
X/*
X *	Set-up utemp file for reading
X */
Xvoid setutent()
X	{
X	fp=open("/etc/utmp",0);
X	if ( fp == -1 )
X		{
X		perror("Error opening /etc/utmp");
X		exit(-1);
X		}
X	}
X/*
X *	Get an entry from the utmp file.  Return NULL if not available.
X */
Xstruct utmp *getutent()
X	{
X	int retval;
X	static struct utmp myline;
X
X	retval=read(fp, &myline, sizeof(struct utmp));
X	if ( retval != sizeof(struct utmp))
X		return( ((struct utmp *) NULL) );
X		else
X		if ( myline.ut_name[0] == '\0')
X			getutent();	/* call again, eats blank entries */
X		return(&myline);
X	}
X
X/*
X *	End use of utmp file.
X */
Xvoid endutent()
X	{
X	close(fp);
X	fp=0;
X	}
X
X
________This_Is_The_END________
if test `wc -l < myutmp.c` -ne 66; then
	echo 'shar: myutmp.c was damaged during transit (should have been 66 lines)'
fi
fi		; : end of overwriting check
exit 0