119910Sdist /* 219910Sdist * Copyright (c) 1980 Regents of the University of California. 319910Sdist * All rights reserved. The Berkeley software License Agreement 419910Sdist * specifies the terms and conditions for redistribution. 519910Sdist */ 619910Sdist 712686Ssam #ifndef lint 819910Sdist char copyright[] = 919910Sdist "@(#) Copyright (c) 1980 Regents of the University of California.\n\ 1019910Sdist All rights reserved.\n"; 1119910Sdist #endif not lint 1219910Sdist 1319910Sdist #ifndef lint 14*35429Sbostic static char sccsid[] = "@(#)who.c 5.5 (Berkeley) 08/29/88"; 1519910Sdist #endif not lint 1619910Sdist 171165Sbill /* 181165Sbill * who 191165Sbill */ 201165Sbill 2130813Skarels #include <stdio.h> 221165Sbill #include <utmp.h> 231165Sbill #include <pwd.h> 241729Sbill #include <ctype.h> 251165Sbill 2630813Skarels #define NMAX sizeof(utmp.ut_name) 2730813Skarels #define LMAX sizeof(utmp.ut_line) 2830813Skarels #define HMAX sizeof(utmp.ut_host) 291165Sbill 3030813Skarels struct utmp utmp; 3130813Skarels struct passwd *pw; 3230813Skarels struct passwd *getpwuid(); 331165Sbill 3430813Skarels char *ttyname(), *rindex(), *ctime(), *strcpy(); 3530813Skarels 3630813Skarels main(argc, argv) 3730813Skarels int argc; 3830813Skarels char **argv; 391165Sbill { 4030813Skarels register char *tp, *s; 4130813Skarels register FILE *fi; 421165Sbill 4330813Skarels s = "/etc/utmp"; 4430813Skarels if(argc == 2) 4530813Skarels s = argv[1]; 4630813Skarels if (argc == 3) { 4730813Skarels tp = ttyname(0); 4830813Skarels if (tp) 4930813Skarels tp = rindex(tp, '/') + 1; 5030813Skarels else { /* no tty - use best guess from passwd file */ 5134026Sbostic (void)strcpy(utmp.ut_line, "tty??"); 5230813Skarels guess(); 5330813Skarels exit(0); 5430813Skarels } 551165Sbill } 5634026Sbostic if (!(fi = fopen(s, "r"))) { 5734026Sbostic fprintf(stderr, "who: cannot read %s.\n", s); 581165Sbill exit(1); 591165Sbill } 6030813Skarels while (fread((char *)&utmp, sizeof(utmp), 1, fi) == 1) { 6112686Ssam if (argc == 3) { 6230813Skarels if (strcmp(utmp.ut_line, tp)) 6330813Skarels continue; 64*35429Sbostic if (!utmp.ut_name[0]) 65*35429Sbostic guess(); 66*35429Sbostic else 67*35429Sbostic putline(); 6830813Skarels exit(0); 6930282Sbostic } 7030813Skarels if (utmp.ut_name[0] == '\0' && argc == 1) 7130813Skarels continue; 7230813Skarels putline(); 7330813Skarels } 7430813Skarels if (argc == 3) { 7530813Skarels strncpy(utmp.ut_line, tp, sizeof(utmp.ut_line)); 7630813Skarels guess(); 7730813Skarels } 7830813Skarels exit(0); 791165Sbill } 801165Sbill 811165Sbill putline() 821165Sbill { 8330813Skarels register char *cbuf; 841165Sbill 8530813Skarels printf("%-*.*s %-*.*s", 8630813Skarels NMAX, NMAX, utmp.ut_name, 8730813Skarels LMAX, LMAX, utmp.ut_line); 8830813Skarels cbuf = ctime(&utmp.ut_time); 8930813Skarels printf("%.12s", cbuf+4); 9030813Skarels if (utmp.ut_host[0]) 9130813Skarels printf("\t(%.*s)", HMAX, utmp.ut_host); 9212686Ssam putchar('\n'); 931165Sbill } 9430813Skarels 9530813Skarels guess() 9630813Skarels { 9730813Skarels pw = getpwuid(getuid()); 9830813Skarels strncpy(utmp.ut_name, pw ? pw->pw_name : "?", NMAX); 9930813Skarels time(&utmp.ut_time); 10030813Skarels putline(); 10130813Skarels } 102