|
|
1.1 ! root 1: /* ! 2: * Copyright (c) 1989 The Regents of the University of California. ! 3: * All rights reserved. ! 4: * ! 5: * This code is derived from software contributed to Berkeley by ! 6: * Michael Fischbein. ! 7: * ! 8: * Redistribution and use in source and binary forms are permitted provided ! 9: * that: (1) source distributions retain this entire copyright notice and ! 10: * comment, and (2) distributions including binaries display the following ! 11: * acknowledgement: ``This product includes software developed by the ! 12: * University of California, Berkeley and its contributors'' in the ! 13: * documentation or other materials provided with the distribution and in ! 14: * all advertising materials mentioning features or use of this software. ! 15: * Neither the name of the University nor the names of its contributors may ! 16: * be used to endorse or promote products derived from this software without ! 17: * specific prior written permission. ! 18: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED ! 19: * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF ! 20: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. ! 21: */ ! 22: ! 23: #ifndef lint ! 24: char copyright[] = ! 25: "@(#) Copyright (c) 1989 The Regents of the University of California.\n\ ! 26: All rights reserved.\n"; ! 27: #endif /* not lint */ ! 28: ! 29: #ifndef lint ! 30: static char sccsid[] = "@(#)who.c 5.11 (Berkeley) 6/1/90"; ! 31: #endif /* not lint */ ! 32: ! 33: #include <sys/types.h> ! 34: #include <sys/file.h> ! 35: #include <sys/time.h> ! 36: #include <pwd.h> ! 37: #include <utmp.h> ! 38: #include <stdio.h> ! 39: ! 40: main(argc, argv) ! 41: int argc; ! 42: char **argv; ! 43: { ! 44: register char *p; ! 45: struct utmp usr; ! 46: struct passwd *pw; ! 47: FILE *ufp, *file(); ! 48: char *t, *rindex(), *strcpy(), *strncpy(), *ttyname(); ! 49: time_t time(); ! 50: ! 51: switch (argc) { ! 52: case 1: /* who */ ! 53: ufp = file(_PATH_UTMP); ! 54: /* only entries with both name and line fields */ ! 55: while (fread((char *)&usr, sizeof(usr), 1, ufp) == 1) ! 56: if (*usr.ut_name && *usr.ut_line) ! 57: output(&usr); ! 58: break; ! 59: case 2: /* who utmp_file */ ! 60: ufp = file(argv[1]); ! 61: /* all entries */ ! 62: while (fread((char *)&usr, sizeof(usr), 1, ufp) == 1) ! 63: output(&usr); ! 64: break; ! 65: case 3: /* who am i */ ! 66: ufp = file(_PATH_UTMP); ! 67: ! 68: /* search through the utmp and find an entry for this tty */ ! 69: if (p = ttyname(0)) { ! 70: /* strip any directory component */ ! 71: if (t = rindex(p, '/')) ! 72: p = t + 1; ! 73: while (fread((char *)&usr, sizeof(usr), 1, ufp) == 1) ! 74: if (usr.ut_name && !strcmp(usr.ut_line, p)) { ! 75: output(&usr); ! 76: exit(0); ! 77: } ! 78: /* well, at least we know what the tty is */ ! 79: (void)strncpy(usr.ut_line, p, UT_LINESIZE); ! 80: } else ! 81: (void)strcpy(usr.ut_line, "tty??"); ! 82: pw = getpwuid(getuid()); ! 83: (void)strncpy(usr.ut_name, pw ? pw->pw_name : "?", UT_NAMESIZE); ! 84: (void)time(&usr.ut_time); ! 85: *usr.ut_host = '\0'; ! 86: output(&usr); ! 87: break; ! 88: default: ! 89: (void)fprintf(stderr, "usage: who [ file ]\n who am i\n"); ! 90: exit(1); ! 91: } ! 92: exit(0); ! 93: } ! 94: ! 95: output(up) ! 96: struct utmp *up; ! 97: { ! 98: char *ctime(); ! 99: ! 100: (void)printf("%-*.*s %-*.*s", UT_NAMESIZE, UT_NAMESIZE, up->ut_name, ! 101: UT_LINESIZE, UT_LINESIZE, up->ut_line); ! 102: (void)printf("%.12s", ctime(&up->ut_time) + 4); ! 103: if (*up->ut_host) ! 104: printf("\t(%.*s)", UT_HOSTSIZE, up->ut_host); ! 105: (void)putchar('\n'); ! 106: } ! 107: ! 108: FILE * ! 109: file(name) ! 110: char *name; ! 111: { ! 112: extern int errno; ! 113: FILE *ufp; ! 114: char *strerror(); ! 115: ! 116: if (!(ufp = fopen(name, "r"))) { ! 117: (void)fprintf(stderr, "who: %s: %s.\n", name, strerror(errno)); ! 118: exit(1); ! 119: } ! 120: return(ufp); ! 121: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.