Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!sdd.hp.com!hp-pcd!hpfcso!hpfcbig!jww From: jww@hpfcbig.SDE.HP.COM (Jim Wichelman) Newsgroups: comp.sys.hp Subject: Re: determining original host Message-ID: <7540066@hpfcbig.SDE.HP.COM> Date: 1 Apr 91 18:09:10 GMT References: <1970@news.tcs.com> Organization: HP SESD, Fort Collins, CO Lines: 103 The following simple program should work ... Jim Wichelman Hewlett Packard jww@hpfclp.sde.hp.com #------------------------------------------------------------- # This is a shell archive. Remove anything before this line, # then unpack it by saving it in a file and typing "sh file". # # Wrapped by Jim Wichelman on Mon Apr 1 11:07:24 1991 # # This archive contains: # fromhost.c # LANG=""; export LANG PATH=/bin:/usr/bin:$PATH; export PATH echo x - fromhost.c cat >fromhost.c <<'@EOF' #include #include #ifdef hpux #include #endif #ifdef apollo #include #endif #include #define UTMP_FILE "/etc/utmp" main () { char *tty; char *p; FILE *fd; struct utmp info; char host[100]; extern char *ttyname(); tty = ttyname( fileno(stderr) ); if (!tty) tty = ttyname( fileno(stdout) ); if (!tty) tty = ttyname( fileno(stdin) ); tty += strlen("/dev/"); if (!(fd = fopen(UTMP_FILE, "r"))) { printf("unknown-host, can't open %s\n", UTMP_FILE); close(fd); exit(1); } while ( fread( (char *)&info, sizeof(info), 1, fd) ) if (strcmp(tty, info.ut_line) == 0) { p = info.ut_host; while (*p == ' ') p++; while (*p) { if ( ( *p == '.') || (*p == ':') ) *p = 0; else p++; } p = info.ut_host; if ( (strlen(p) == 0) || (strcmp("unix", p) == 0)) { host[0] = 0; if (gethostname(host, sizeof(host)) == 0) p = host; else p = ""; } printf("%s\n",p); close(fd); exit(0); } printf("unknown-host, no entry in %s\n", UTMP_FILE); close(fd); exit(1); } @EOF chmod 664 fromhost.c exit 0