Home | History | Annotate | Line # | Download | only in who
who.c revision 1.14
      1  1.14     peter /*	$NetBSD: who.c,v 1.14 2004/11/22 17:20:02 peter Exp $	*/
      2   1.3       jtc 
      3   1.1       cgd /*
      4   1.3       jtc  * Copyright (c) 1989, 1993
      5   1.3       jtc  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * This code is derived from software contributed to Berkeley by
      8   1.1       cgd  * Michael Fischbein.
      9   1.1       cgd  *
     10   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     11   1.1       cgd  * modification, are permitted provided that the following conditions
     12   1.1       cgd  * are met:
     13   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     14   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     15   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     17   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     18  1.12       agc  * 3. Neither the name of the University nor the names of its contributors
     19   1.1       cgd  *    may be used to endorse or promote products derived from this software
     20   1.1       cgd  *    without specific prior written permission.
     21   1.1       cgd  *
     22   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32   1.1       cgd  * SUCH DAMAGE.
     33   1.1       cgd  */
     34   1.1       cgd 
     35   1.5    kleink #include <sys/cdefs.h>
     36   1.1       cgd #ifndef lint
     37   1.5    kleink __COPYRIGHT(
     38   1.3       jtc "@(#) Copyright (c) 1989, 1993\n\
     39   1.5    kleink 	The Regents of the University of California.  All rights reserved.\n");
     40   1.1       cgd #endif /* not lint */
     41   1.1       cgd 
     42   1.1       cgd #ifndef lint
     43   1.3       jtc #if 0
     44   1.3       jtc static char sccsid[] = "@(#)who.c	8.1 (Berkeley) 6/6/93";
     45   1.3       jtc #endif
     46  1.14     peter __RCSID("$NetBSD: who.c,v 1.14 2004/11/22 17:20:02 peter Exp $");
     47   1.1       cgd #endif /* not lint */
     48   1.1       cgd 
     49   1.1       cgd #include <sys/types.h>
     50   1.4       jtc #include <sys/stat.h>
     51   1.5    kleink #include <err.h>
     52   1.5    kleink #include <locale.h>
     53   1.1       cgd #include <pwd.h>
     54   1.1       cgd #include <stdio.h>
     55   1.5    kleink #include <stdlib.h>
     56   1.4       jtc #include <string.h>
     57   1.5    kleink #include <time.h>
     58   1.4       jtc #include <unistd.h>
     59  1.10  christos #include "utmpentry.h"
     60   1.9  christos 
     61   1.9  christos static void output_labels(void);
     62   1.9  christos static void who_am_i(const char *, int);
     63   1.9  christos static void usage(void);
     64   1.9  christos static void process(const char *, int);
     65   1.9  christos static void print(const char *, const char *, time_t, const char *);
     66   1.5    kleink 
     67   1.9  christos int main(int, char **);
     68   1.1       cgd 
     69   1.9  christos static int show_term;			/* show term state */
     70   1.9  christos static int show_idle;			/* show idle time */
     71   1.9  christos 
     72  1.11  christos extern int maxname, maxline, maxhost;
     73   1.5    kleink 
     74   1.4       jtc int
     75   1.9  christos main(int argc, char **argv)
     76   1.1       cgd {
     77   1.5    kleink 	int c, only_current_term, show_labels;
     78   1.5    kleink 
     79   1.5    kleink 	setlocale(LC_ALL, "");
     80   1.4       jtc 
     81   1.5    kleink 	only_current_term = show_term = show_idle = show_labels = 0;
     82  1.14     peter 	while ((c = getopt(argc, argv, "HmTu")) != -1) {
     83   1.4       jtc 		switch (c) {
     84  1.14     peter 		case 'H':
     85  1.14     peter 			show_labels = 1;
     86  1.14     peter 			break;
     87   1.4       jtc 		case 'm':
     88   1.4       jtc 			only_current_term = 1;
     89   1.4       jtc 			break;
     90   1.4       jtc 		case 'T':
     91   1.4       jtc 			show_term = 1;
     92   1.4       jtc 			break;
     93   1.4       jtc 		case 'u':
     94   1.4       jtc 			show_idle = 1;
     95   1.4       jtc 			break;
     96   1.4       jtc 		default:
     97   1.4       jtc 			usage();
     98   1.4       jtc 			/* NOTREACHED */
     99   1.4       jtc 		}
    100   1.4       jtc 	}
    101   1.4       jtc 	argc -= optind;
    102   1.4       jtc 	argv += optind;
    103   1.4       jtc 
    104   1.4       jtc 	if (chdir("/dev")) {
    105   1.4       jtc 		err(1, "cannot change directory to /dev");
    106   1.4       jtc 		/* NOTREACHED */
    107   1.4       jtc 	}
    108   1.1       cgd 
    109   1.1       cgd 	switch (argc) {
    110   1.4       jtc 	case 0:					/* who */
    111   1.4       jtc 		if (only_current_term) {
    112   1.9  christos 			who_am_i(NULL, show_labels);
    113   1.4       jtc 		} else {
    114   1.9  christos 			process(NULL, show_labels);
    115   1.4       jtc 		}
    116   1.4       jtc 		break;
    117   1.4       jtc 	case 1:					/* who utmp_file */
    118   1.4       jtc 		if (only_current_term) {
    119   1.9  christos 			who_am_i(*argv, show_labels);
    120   1.4       jtc 		} else {
    121   1.9  christos 			process(*argv, show_labels);
    122   1.4       jtc 		}
    123   1.1       cgd 		break;
    124   1.4       jtc 	case 2:					/* who am i */
    125   1.9  christos 		who_am_i(NULL, show_labels);
    126   1.1       cgd 		break;
    127   1.1       cgd 	default:
    128   1.4       jtc 		usage();
    129   1.4       jtc 		/* NOTREACHED */
    130   1.1       cgd 	}
    131   1.1       cgd 	exit(0);
    132   1.1       cgd }
    133   1.1       cgd 
    134   1.9  christos static void
    135   1.9  christos who_am_i(const char *fname, int show_labels)
    136   1.4       jtc {
    137   1.4       jtc 	struct passwd *pw;
    138   1.6     lukem 	char *p;
    139   1.4       jtc 	char *t;
    140   1.9  christos 	time_t now;
    141  1.10  christos 	struct utmpentry *ehead, *ep;
    142   1.4       jtc 
    143   1.4       jtc 	/* search through the utmp and find an entry for this tty */
    144   1.9  christos 	if ((p = ttyname(STDIN_FILENO)) != NULL) {
    145   1.9  christos 
    146   1.4       jtc 		/* strip any directory component */
    147   1.6     lukem 		if ((t = strrchr(p, '/')) != NULL)
    148   1.4       jtc 			p = t + 1;
    149  1.10  christos 
    150  1.10  christos 		(void)getutentries(fname, &ehead);
    151  1.10  christos 		for (ep = ehead; ep; ep = ep->next)
    152  1.10  christos 			if (strcmp(ep->line, p) == 0) {
    153   1.9  christos 				if (show_labels)
    154   1.9  christos 					output_labels();
    155  1.10  christos 				print(ep->name, ep->line, (time_t)ep->tv.tv_sec,
    156  1.10  christos 				    ep->host);
    157   1.4       jtc 				return;
    158   1.4       jtc 			}
    159   1.4       jtc 	} else
    160   1.9  christos 		p = "tty??";
    161   1.4       jtc 
    162   1.9  christos 	(void)time(&now);
    163   1.4       jtc 	pw = getpwuid(getuid());
    164   1.9  christos 	if (show_labels)
    165   1.9  christos 		output_labels();
    166   1.9  christos 	print(pw ? pw->pw_name : "?", p, now, "");
    167   1.9  christos }
    168   1.9  christos 
    169   1.9  christos static void
    170   1.9  christos process(const char *fname, int show_labels)
    171   1.9  christos {
    172  1.10  christos 	struct utmpentry *ehead, *ep;
    173  1.10  christos 	(void)getutentries(fname, &ehead);
    174   1.9  christos 	if (show_labels)
    175   1.9  christos 		output_labels();
    176   1.9  christos 	for (ep = ehead; ep != NULL; ep = ep->next)
    177  1.10  christos 		print(ep->name, ep->line, (time_t)ep->tv.tv_sec, ep->host);
    178   1.9  christos }
    179   1.4       jtc 
    180   1.9  christos static void
    181   1.9  christos print(const char *name, const char *line, time_t t, const char *host)
    182   1.1       cgd {
    183   1.4       jtc 	struct stat sb;
    184   1.4       jtc 	char state;
    185   1.4       jtc 	static time_t now = 0;
    186   1.4       jtc 	time_t idle;
    187   1.4       jtc 
    188   1.5    kleink 	state = '?';
    189   1.5    kleink 	idle = 0;
    190   1.5    kleink 
    191   1.4       jtc 	if (show_term || show_idle) {
    192   1.4       jtc 		if (now == 0)
    193   1.4       jtc 			time(&now);
    194   1.4       jtc 
    195   1.4       jtc 		if (stat(line, &sb) == 0) {
    196   1.4       jtc 			state = (sb.st_mode & 020) ? '+' : '-';
    197   1.4       jtc 			idle = now - sb.st_atime;
    198   1.4       jtc 		}
    199   1.4       jtc 
    200   1.4       jtc 	}
    201   1.4       jtc 
    202   1.9  christos 	(void)printf("%-*.*s ", maxname, maxname, name);
    203   1.4       jtc 
    204   1.4       jtc 	if (show_term) {
    205   1.4       jtc 		(void)printf("%c ", state);
    206   1.4       jtc 	}
    207   1.4       jtc 
    208   1.9  christos 	(void)printf("%-*.*s ", maxline, maxline, line);
    209   1.9  christos 	(void)printf("%.12s ", ctime(&t) + 4);
    210   1.1       cgd 
    211   1.4       jtc 	if (show_idle) {
    212   1.4       jtc 		if (idle < 60)
    213   1.4       jtc 			(void)printf("  .   ");
    214   1.4       jtc 		else if (idle < (24 * 60 * 60))
    215   1.5    kleink 			(void)printf("%02ld:%02ld ",
    216   1.5    kleink 				     (long)(idle / (60 * 60)),
    217   1.5    kleink 				     (long)(idle % (60 * 60)) / 60);
    218   1.4       jtc 		else
    219   1.4       jtc 			(void)printf(" old  ");
    220   1.4       jtc 	}
    221   1.4       jtc 
    222   1.9  christos 	if (*host)
    223   1.9  christos 		printf("\t(%.*s)", maxhost, host);
    224   1.1       cgd 	(void)putchar('\n');
    225   1.1       cgd }
    226   1.1       cgd 
    227   1.9  christos static void
    228   1.5    kleink output_labels()
    229   1.5    kleink {
    230   1.9  christos 	(void)printf("%-*.*s ", maxname, maxname, "USER");
    231   1.5    kleink 
    232   1.5    kleink 	if (show_term)
    233   1.5    kleink 		(void)printf("S ");
    234   1.5    kleink 
    235   1.9  christos 	(void)printf("%-*.*s ", maxline, maxline, "LINE");
    236   1.5    kleink 	(void)printf("WHEN         ");
    237   1.5    kleink 
    238   1.5    kleink 	if (show_idle)
    239   1.5    kleink 		(void)printf("IDLE  ");
    240   1.5    kleink 
    241   1.9  christos 	(void)printf("\t%.*s", maxhost, "FROM");
    242   1.5    kleink 
    243   1.5    kleink 	(void)putchar('\n');
    244   1.5    kleink }
    245   1.5    kleink 
    246   1.9  christos static void
    247   1.4       jtc usage()
    248   1.4       jtc {
    249  1.14     peter 	(void)fprintf(stderr, "usage: %s [-HmTu] [file]\n       %s am i\n",
    250   1.9  christos 	    getprogname(), getprogname());
    251   1.4       jtc 	exit(1);
    252   1.1       cgd }
    253