diff options
Diffstat (limited to 'usr.bin/who/who.c')
-rw-r--r-- | usr.bin/who/who.c | 327 |
1 files changed, 220 insertions, 107 deletions
diff --git a/usr.bin/who/who.c b/usr.bin/who/who.c index 02b5e51..6268095 100644 --- a/usr.bin/who/who.c +++ b/usr.bin/who/who.c @@ -1,9 +1,6 @@ -/* - * Copyright (c) 1989, 1993 - * The Regents of the University of California. All rights reserved. - * - * This code is derived from software contributed to Berkeley by - * Michael Fischbein. +/*- + * Copyright (c) 2002 Tim J. Robbins. + * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -13,18 +10,11 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. - * 4. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) @@ -35,25 +25,16 @@ */ #include <sys/cdefs.h> - __FBSDID("$FreeBSD$"); -#ifndef lint -static const char copyright[] = -"@(#) Copyright (c) 1989, 1993\n\ - The Regents of the University of California. All rights reserved.\n"; -#endif - -#ifndef lint -static const char sccsid[] = "@(#)who.c 8.1 (Berkeley) 6/6/93"; -#endif - #include <sys/types.h> -#include <sys/file.h> +#include <sys/ioctl.h> +#include <sys/stat.h> #include <err.h> #include <langinfo.h> #include <locale.h> +#include <paths.h> #include <pwd.h> #include <stdio.h> #include <stdlib.h> @@ -62,107 +43,239 @@ static const char sccsid[] = "@(#)who.c 8.1 (Berkeley) 6/6/93"; #include <unistd.h> #include <utmp.h> -static void usage(void); -static void output(struct utmp *); -static FILE *file(const char *); +static void heading(void); +static void process_utmp(FILE *); +static void quick(FILE *); +static void row(struct utmp *); +static int ttywidth(void); +static void usage(void); +static void whoami(FILE *); + +static int Hflag; /* Write column headings */ +static int mflag; /* Show info about current terminal */ +static int qflag; /* "Quick" mode */ +static int sflag; /* Show name, line, time */ +static int Tflag; /* Show terminal state */ +static int uflag; /* Show idle time */ int -main(argc, argv) - int argc; - char **argv; +main(int argc, char *argv[]) { - char *p; - struct utmp usr; - struct passwd *pw; - FILE *ufp; - char *t; - - (void) setlocale(LC_TIME, ""); - - switch (argc) { - case 1: /* who */ - ufp = file(_PATH_UTMP); - /* only entries with both name and line fields */ - while (fread((char *)&usr, sizeof(usr), 1, ufp) == 1) - if (*usr.ut_name && *usr.ut_line) - output(&usr); - break; - case 2: /* who utmp_file */ - ufp = file(argv[1]); - /* all entries */ - while (fread((char *)&usr, sizeof(usr), 1, ufp) == 1) - output(&usr); - break; - case 3: /* who am i */ - if (strcmp(argv[1], "am") - || (strcmp(argv[2], "I") && strcmp(argv[2], "i"))) - usage(); - - ufp = file(_PATH_UTMP); - - /* search through the utmp and find an entry for this tty */ - if ((p = ttyname(0))) { - /* strip any directory component */ - if ((t = rindex(p, '/'))) - p = t + 1; - while (fread((char *)&usr, sizeof(usr), 1, ufp) == 1) - if (*usr.ut_name && !strcmp(usr.ut_line, p)) { - output(&usr); - exit(0); - } - /* well, at least we know what the tty is */ - (void)strncpy(usr.ut_line, p, UT_LINESIZE); - } else - (void)strcpy(usr.ut_line, "tty??"); - pw = getpwuid(getuid()); - (void)strncpy(usr.ut_name, pw ? pw->pw_name : "?", UT_NAMESIZE); - (void)time(&usr.ut_time); - *usr.ut_host = '\0'; - output(&usr); - break; - default: + int ch; + const char *file; + FILE *fp; + + setlocale(LC_TIME, ""); + + while ((ch = getopt(argc, argv, "HTabdlmpqrstu")) != -1) { + switch (ch) { + case 'H': /* Write column headings */ + Hflag = 1; + break; + case 'T': /* Show terminal state */ + Tflag = 1; + break; + case 'm': /* Show info about current terminal */ + mflag = 1; + break; + case 'q': /* "Quick" mode */ + qflag = 1; + break; + case 's': /* Show name, line, time */ + sflag = 1; + break; + case 'u': /* Show idle time */ + uflag = 1; + break; + default: + usage(); + /*NOTREACHED*/ + } + } + argc -= optind; + argv += optind; + + if (argc >= 2 && strcmp(argv[0], "am") == 0 && + (strcmp(argv[1], "i") == 0 || strcmp(argv[1], "I") == 0)) { + /* "who am i" or "who am I", equivalent to -m */ + mflag = 1; + argc -= 2; + argv += 2; + } + if (argc > 1) usage(); + + if (*argv != NULL) + file = *argv; + else + file = _PATH_UTMP; + if ((fp = fopen(file, "r")) == NULL) + err(1, "%s", file); + + if (qflag) + quick(fp); + else { + if (sflag) + Tflag = uflag = 0; + if (Hflag) + heading(); + if (mflag) + whoami(fp); + else + process_utmp(fp); } + + fclose(fp); + exit(0); } -static void -usage() +void +usage(void) { - (void)fprintf(stderr, "%s\n%s\n", - "usage: who [file]", - " who am i"); + + fprintf(stderr, "usage: who [-HmqsTu] [am I] [file]\n"); exit(1); } void -output(up) - struct utmp *up; +heading(void) { - char buf[80]; + + printf("%-*s ", UT_NAMESIZE, "NAME"); + if (Tflag) + printf("S "); + printf("%-*s ", UT_LINESIZE, "LINE"); + printf("%-*s ", 12, "TIME"); + if (uflag) + printf("IDLE "); + putchar('\n'); +} + +void +row(struct utmp *ut) +{ + char buf[80], tty[sizeof(_PATH_DEV) + UT_LINESIZE]; + struct stat sb; + time_t idle; static int d_first = -1; + struct tm *tm; + char state; if (d_first < 0) d_first = (*nl_langinfo(D_MD_ORDER) == 'd'); - (void)printf("%-*.*s %-*.*s", UT_NAMESIZE, UT_NAMESIZE, up->ut_name, - UT_LINESIZE, UT_LINESIZE, up->ut_line); - (void)strftime(buf, sizeof(buf), - d_first ? "%e %b %R" : "%b %e %R", - localtime(&up->ut_time)); - (void)printf("%s", buf); - if (*up->ut_host) - printf("\t(%.*s)", UT_HOSTSIZE, up->ut_host); - (void)putchar('\n'); + if (Tflag || uflag) { + snprintf(tty, sizeof(tty), "%s%.*s", _PATH_DEV, + UT_LINESIZE, ut->ut_line); + state = '?'; + idle = 0; + if (stat(tty, &sb) == 0) { + state = sb.st_mode & (S_IWOTH|S_IWGRP) ? + '+' : '-'; + idle = time(NULL) - sb.st_mtime; + } + } + + printf("%-*.*s ", UT_NAMESIZE, UT_NAMESIZE, ut->ut_name); + if (Tflag) + printf("%c ", state); + printf("%-*.*s ", UT_LINESIZE, UT_LINESIZE, ut->ut_line); + tm = localtime(&ut->ut_time); + strftime(buf, sizeof(buf), d_first ? "%e %b %R" : "%b %e %R", tm); + printf("%-*s ", 12, buf); + if (uflag) { + if (idle < 60) + printf(" . "); + else if (idle < 24 * 60 * 60) + printf("%02d:%02d ", (int)(idle / 60 / 60), + (int)(idle / 60 % 60)); + else + printf(" old "); + } + if (*ut->ut_host != '\0') + printf("(%.*s)", UT_HOSTSIZE, ut->ut_host); + putchar('\n'); +} + +void +process_utmp(FILE *fp) +{ + struct utmp ut; + + while (fread(&ut, sizeof(ut), 1, fp) == 1) + if (*ut.ut_name != '\0') + row(&ut); +} + +void +quick(FILE *fp) +{ + struct utmp ut; + int col, ncols, num; + + ncols = ttywidth(); + col = num = 0; + while (fread(&ut, sizeof(ut), 1, fp) == 1) { + if (*ut.ut_name == '\0') + continue; + printf("%-*.*s", UT_NAMESIZE, UT_NAMESIZE, ut.ut_name); + if (++col < ncols / (UT_NAMESIZE + 1)) + putchar(' '); + else { + col = 0; + putchar('\n'); + } + num++; + } + if (col != 0) + putchar('\n'); + + printf("# users = %d\n", num); } -static FILE * -file(name) - const char *name; +void +whoami(FILE *fp) { - FILE *ufp; + struct utmp ut; + struct passwd *pwd; + const char *name, *p, *tty; + + if ((tty = ttyname(STDIN_FILENO)) == NULL) + tty = "tty??"; + else if ((p = strrchr(tty, '/')) != NULL) + tty = p + 1; + + /* Search utmp for our tty, dump first matching record. */ + while (fread(&ut, sizeof(ut), 1, fp) == 1) + if (*ut.ut_name != '\0' && strncmp(ut.ut_line, tty, + UT_LINESIZE) == 0) { + row(&ut); + return; + } + + /* Not found; fill the utmp structure with the information we have. */ + memset(&ut, 0, sizeof(ut)); + if ((pwd = getpwuid(getuid())) != NULL) + name = pwd->pw_name; + else + name = "?"; + strncpy(ut.ut_name, name, UT_NAMESIZE); + strncpy(ut.ut_line, tty, UT_LINESIZE); + time(&ut.ut_time); + row(&ut); +} + +int +ttywidth(void) +{ + struct winsize ws; + int width; + + if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) != -1) + width = ws.ws_col; + else + width = 80; - if (!(ufp = fopen(name, "r"))) - err(1, "%s", name); - return(ufp); + return (width); } |