[comp.sys.sgi] more REMOTEHOST

loki@NAZGUL.PHYSICS.MCGILL.CA (Loki Jorgenson) (06/22/91)

	Since we're getting in the mood.... I remembered somewhat 
belatedly that I also wrote a little utmp parser to find the remotehost
site.  It takes an argument (user name) or none (lists all remotely
connected users) and returns the user name, the tty, the remotehost
and the date of login (in time(2) formate).  It works on BSD systems
such as our date SUn OSv3.5 system which I wrote it for.

	It helpfully strips out the the domain from the remote host name
for local use.  This is not that helpful for truly remote sites but
its easy to hack the code.  Plus, most remote site names are too long
for utmp's host name limit anyway.

Enjoy,
 -------------- cut here -----------------
#include <stdio.h>
#include <fcntl.h>
#include <utmp.h>

struct utmp user;

main(argc,argv)
int argc;
char *argv[];
{
int uptr,notyet=1;

	if((uptr=open("/etc/utmp",O_RDONLY))<0)
	  fail("fERR >< cannot open /etc/utmp for read\n");
	if (argc>1)	do {
	  if(!(get_user(uptr))) break;
	  if(!(notyet=(strcmp(argv[1],user.ut_name))))
	     print_entry(&user);
			   } while(notyet);
	else do {
	  if(!(get_user(uptr))) break;
	  print_entry(&user);
		} while(notyet);
	close(uptr);
}

get_user(fptr)
int fptr;
{
int result;
	result = read(fptr,&user,sizeof(struct utmp));
	if(result!=sizeof(struct utmp)&&result)
		fail("rERR >< failed read on /etc/utmp\n");
	return(result);
}

#include <string.h>

print_entry(entry)
struct utmp *entry;
{
int ii,ch='.';
char *cptr;
	if(!strlen(entry->ut_name)) return;
	if((cptr=strchr(entry->ut_host,ch))!=NULL) *cptr = '\0';
	fprintf(stdout,"%.8s  ",entry->ut_name);
	fprintf(stdout,"%.8s  ",entry->ut_line);
	fprintf(stdout,"%.16s  ",entry->ut_host);
	fprintf(stdout,"%ld\n",entry->ut_time);
}


fail(string)
char *string;
{
  fprintf(stderr,"%s",string);
  exit();
}
------------ cute here -----------------

                             _ _         _ _
Loki Jorgenson              / / _ _ _ _ _ \ \  node:  loki@Physics.McGill.CA
Grad/Systems Manager       /_/_/_/_/ \_\_\_\_\ BITNET: PY29@MCGILLA
Physics, McGill University \ \ \_\_\_/_/_/ / / fax:   (514) 398-3733
Montreal Quebec CANADA      \_\_         _/_/  phone: (514) 398-7027

                      -*  Anatomically  correct  *-