119910Sdist /* 2*38370Sbostic * Copyright (c) 1989 The Regents of the University of California. 3*38370Sbostic * All rights reserved. 4*38370Sbostic * 5*38370Sbostic * This code is derived from software contributed to Berkeley by 6*38370Sbostic * Michael Fischbein. 7*38370Sbostic * 8*38370Sbostic * Redistribution and use in source and binary forms are permitted 9*38370Sbostic * provided that the above copyright notice and this paragraph are 10*38370Sbostic * duplicated in all such forms and that any documentation, 11*38370Sbostic * advertising materials, and other materials related to such 12*38370Sbostic * distribution and use acknowledge that the software was developed 13*38370Sbostic * by the University of California, Berkeley. The name of the 14*38370Sbostic * University may not be used to endorse or promote products derived 15*38370Sbostic * from this software without specific prior written permission. 16*38370Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 17*38370Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 18*38370Sbostic * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1919910Sdist */ 2019910Sdist 2112686Ssam #ifndef lint 2219910Sdist char copyright[] = 23*38370Sbostic "@(#) Copyright (c) 1989 The Regents of the University of California.\n\ 2419910Sdist All rights reserved.\n"; 25*38370Sbostic #endif /* not lint */ 2619910Sdist 2719910Sdist #ifndef lint 28*38370Sbostic static char sccsid[] = "@(#)who.c 5.10 (Berkeley) 06/29/89"; 29*38370Sbostic #endif /* not lint */ 3019910Sdist 3136955Sbostic #include <sys/types.h> 32*38370Sbostic #include <sys/file.h> 33*38370Sbostic #include <sys/time.h> 34*38370Sbostic #include <pwd.h> 351165Sbill #include <utmp.h> 3636955Sbostic #include <stdio.h> 371165Sbill 3830813Skarels main(argc, argv) 3930813Skarels int argc; 4030813Skarels char **argv; 411165Sbill { 42*38370Sbostic register char *p; 43*38370Sbostic struct utmp usr; 44*38370Sbostic struct passwd *pw; 45*38370Sbostic FILE *ufp, *file(); 46*38370Sbostic char *t, *rindex(), *strcpy(), *strncpy(), *ttyname(); 47*38370Sbostic time_t time(); 481165Sbill 49*38370Sbostic switch (argc) { 50*38370Sbostic case 1: /* who */ 51*38370Sbostic ufp = file(_PATH_UTMP); 52*38370Sbostic /* only entries with both name and line fields */ 53*38370Sbostic while (fread((char *)&usr, sizeof(usr), 1, ufp) == 1) 54*38370Sbostic if (*usr.ut_name && *usr.ut_line) 55*38370Sbostic output(&usr); 56*38370Sbostic break; 57*38370Sbostic case 2: /* who utmp_file */ 58*38370Sbostic ufp = file(argv[1]); 59*38370Sbostic /* all entries */ 60*38370Sbostic while (fread((char *)&usr, sizeof(usr), 1, ufp) == 1) 61*38370Sbostic output(&usr); 62*38370Sbostic break; 63*38370Sbostic case 3: /* who am i */ 64*38370Sbostic ufp = file(_PATH_UTMP); 65*38370Sbostic 66*38370Sbostic /* search through the utmp and find an entry for this tty */ 67*38370Sbostic if (p = ttyname(0)) { 68*38370Sbostic /* strip any directory component */ 69*38370Sbostic if (t = rindex(p, '/')) 70*38370Sbostic p = t + 1; 71*38370Sbostic while (fread((char *)&usr, sizeof(usr), 1, ufp) == 1) 72*38370Sbostic if (usr.ut_name && !strcmp(usr.ut_line, p)) { 73*38370Sbostic output(&usr); 74*38370Sbostic exit(0); 75*38370Sbostic } 76*38370Sbostic /* well, at least we know what the tty is */ 77*38370Sbostic (void)strncpy(usr.ut_line, p, UT_LINESIZE); 78*38370Sbostic } else 79*38370Sbostic (void)strcpy(usr.ut_line, "tty??"); 80*38370Sbostic pw = getpwuid(getuid()); 81*38370Sbostic (void)strncpy(usr.ut_name, pw ? pw->pw_name : "?", UT_NAMESIZE); 82*38370Sbostic (void)time(&usr.ut_time); 83*38370Sbostic *usr.ut_host = '\0'; 84*38370Sbostic output(&usr); 85*38370Sbostic break; 86*38370Sbostic default: 87*38370Sbostic (void)fprintf(stderr, "usage: who [ file ]\n who am i\n"); 881165Sbill exit(1); 891165Sbill } 9030813Skarels exit(0); 911165Sbill } 921165Sbill 93*38370Sbostic output(up) 94*38370Sbostic struct utmp *up; 951165Sbill { 96*38370Sbostic char *ctime(); 971165Sbill 98*38370Sbostic (void)printf("%-*.*s %-*.*s", UT_NAMESIZE, UT_NAMESIZE, up->ut_name, 99*38370Sbostic UT_LINESIZE, UT_LINESIZE, up->ut_line); 100*38370Sbostic (void)printf("%.12s", ctime(&up->ut_time) + 4); 101*38370Sbostic if (*up->ut_host) 102*38370Sbostic printf("\t(%.*s)", UT_HOSTSIZE, up->ut_host); 103*38370Sbostic (void)putchar('\n'); 1041165Sbill } 10530813Skarels 106*38370Sbostic FILE * 107*38370Sbostic file(name) 108*38370Sbostic char *name; 10930813Skarels { 110*38370Sbostic extern int errno; 111*38370Sbostic FILE *ufp; 112*38370Sbostic char *strerror(); 113*38370Sbostic 114*38370Sbostic if (!(ufp = fopen(name, "r"))) { 115*38370Sbostic (void)fprintf(stderr, "who: %s: %s.\n", name, strerror(errno)); 116*38370Sbostic exit(1); 117*38370Sbostic } 118*38370Sbostic return(ufp); 11930813Skarels } 120