mikef@sarah.lerc.nasa.gov (Mike J. Fuller) (02/04/90)
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@sarah.lerc.nasa.gov |You'd be paranoid, | |----------------| mikef@zippysun.math.uakron.edu|too, if everyone | |/\/\/\/\/\/\/\/\|Bitnet: r3mjf1@akronvm |was out to get you!| \-----------------------------------------------------------------------------/