Determining remote host name on SysV

Mike J. Fuller mikef at sarah.lerc.nasa.gov
Sun Feb 4 07:01:35 AEST 1990


I hacked together the following program which will print the name of
the host you are logged in from (if any) on a BSD machine by looking
in /etc/utmp.  It handles non-null terminated (ie., 16 character)
hostnames and stops at a ":" for xterm sessions (ie., it prints
"hostname" not "hostname:0.0").  However, the remote host name is not
stored in /etc/utmp on SysV machines.  In fact, I can't seem to find
it anywhere.  So, how do you find out the name of the host your are
logged in from on a SysV machine?

#include <stdio.h>
#include <sys/types.h>
#include <sys/file.h>
#include <utmp.h>
#include <strings.h>

main()
{
    int fd, slot;
    struct utmp entry;
    char *colon, host[17];

    if((fd = open("/etc/utmp", O_RDONLY)) == -1) {
	perror("/etc/utmp");
	exit(1);
    }

    if((slot = ttyslot()) == 0) exit(1);
    lseek(fd, slot * sizeof(struct utmp), L_SET);
    read(fd, &entry, sizeof(struct utmp));

    if(*entry.ut_host == '\0') exit(1);
    strncpy(host,entry.ut_host,16);
    host[16] = '\0';
    if((colon = (index(host, ':'))) != (char *) 0) *colon = '\0';
    printf("%s\n", host);
}

/-----------------------------------------------------------------------------\
| Mike J. Fuller |Internet: mikef at sarah.lerc.nasa.gov     |You'd be paranoid, |
|----------------|          mikef at zippysun.math.uakron.edu|too, if everyone   |
|/\/\/\/\/\/\/\/\|Bitnet:   r3mjf1 at akronvm                |was out to get you!|
\-----------------------------------------------------------------------------/



More information about the Comp.unix.wizards mailing list