Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!usc!ucsd!ames!amelia!eagle!news From: mikef@sarah.lerc.nasa.gov (Mike J. Fuller) Newsgroups: comp.unix.wizards Subject: Determining remote host name on SysV Message-ID: <1990Feb3.200135.19372@eagle.lerc.nasa.gov> Date: 3 Feb 90 20:01:35 GMT Organization: NASA Lewis Research Center, Cleveland, OH Lines: 42 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 #include #include #include #include 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!| \-----------------------------------------------------------------------------/