Home | History | Annotate | Line # | Download | only in who
who.c revision 1.9
      1  1.9  christos /*	$NetBSD: who.c,v 1.9 2002/07/28 21:46:34 christos 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.1       cgd  * 3. All advertising materials mentioning features or use of this software
     19  1.1       cgd  *    must display the following acknowledgement:
     20  1.1       cgd  *	This product includes software developed by the University of
     21  1.1       cgd  *	California, Berkeley and its contributors.
     22  1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     23  1.1       cgd  *    may be used to endorse or promote products derived from this software
     24  1.1       cgd  *    without specific prior written permission.
     25  1.1       cgd  *
     26  1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  1.1       cgd  * SUCH DAMAGE.
     37  1.1       cgd  */
     38  1.1       cgd 
     39  1.5    kleink #include <sys/cdefs.h>
     40  1.1       cgd #ifndef lint
     41  1.5    kleink __COPYRIGHT(
     42  1.3       jtc "@(#) Copyright (c) 1989, 1993\n\
     43  1.5    kleink 	The Regents of the University of California.  All rights reserved.\n");
     44  1.1       cgd #endif /* not lint */
     45  1.1       cgd 
     46  1.1       cgd #ifndef lint
     47  1.3       jtc #if 0
     48  1.3       jtc static char sccsid[] = "@(#)who.c	8.1 (Berkeley) 6/6/93";
     49  1.3       jtc #endif
     50  1.9  christos __RCSID("$NetBSD: who.c,v 1.9 2002/07/28 21:46:34 christos Exp $");
     51  1.1       cgd #endif /* not lint */
     52  1.1       cgd 
     53  1.1       cgd #include <sys/types.h>
     54  1.4       jtc #include <sys/stat.h>
     55  1.5    kleink #include <err.h>
     56  1.5    kleink #include <locale.h>
     57  1.1       cgd #include <pwd.h>
     58  1.1       cgd #include <stdio.h>
     59  1.5    kleink #include <stdlib.h>
     60  1.4       jtc #include <string.h>
     61  1.5    kleink #include <time.h>
     62  1.4       jtc #include <unistd.h>
     63  1.9  christos #ifdef SUPPORT_UTMP
     64  1.5    kleink #include <utmp.h>
     65  1.9  christos #endif
     66  1.9  christos #ifdef SUPPORT_UTMPX
     67  1.9  christos #include <utmpx.h>
     68  1.9  christos #endif
     69  1.4       jtc 
     70  1.9  christos struct entry {
     71  1.9  christos 	char name[65];
     72  1.9  christos 	char line[65];
     73  1.9  christos 	char host[257];
     74  1.9  christos 	time_t time;
     75  1.9  christos 	struct entry *next;
     76  1.9  christos };
     77  1.9  christos 
     78  1.9  christos static void output_labels(void);
     79  1.9  christos static void who_am_i(const char *, int);
     80  1.9  christos static void usage(void);
     81  1.9  christos static void process(const char *, int);
     82  1.9  christos #ifdef SUPPORT_UTMP
     83  1.9  christos static void getentry(struct entry *, struct utmp *);
     84  1.9  christos #endif
     85  1.9  christos #ifdef SUPPORT_UTMPX
     86  1.9  christos static void getentryx(struct entry *, struct utmpx *);
     87  1.9  christos #endif
     88  1.9  christos #if defined(SUPPORT_UTMPX) && defined(SUPPORT_UTMP)
     89  1.9  christos static int setup(const char *);
     90  1.9  christos static void adjust_size(struct entry *e);
     91  1.9  christos #endif
     92  1.9  christos static void print(const char *, const char *, time_t, const char *);
     93  1.5    kleink 
     94  1.9  christos int main(int, char **);
     95  1.1       cgd 
     96  1.9  christos static int show_term;			/* show term state */
     97  1.9  christos static int show_idle;			/* show idle time */
     98  1.9  christos 
     99  1.9  christos static int maxname = 8, maxline = 8, maxhost = 16;
    100  1.5    kleink 
    101  1.4       jtc int
    102  1.9  christos main(int argc, char **argv)
    103  1.1       cgd {
    104  1.5    kleink 	int c, only_current_term, show_labels;
    105  1.5    kleink 
    106  1.5    kleink 	setlocale(LC_ALL, "");
    107  1.4       jtc 
    108  1.5    kleink 	only_current_term = show_term = show_idle = show_labels = 0;
    109  1.5    kleink 	while ((c = getopt(argc, argv, "mTuH")) != -1) {
    110  1.4       jtc 		switch (c) {
    111  1.4       jtc 		case 'm':
    112  1.4       jtc 			only_current_term = 1;
    113  1.4       jtc 			break;
    114  1.4       jtc 		case 'T':
    115  1.4       jtc 			show_term = 1;
    116  1.4       jtc 			break;
    117  1.4       jtc 		case 'u':
    118  1.4       jtc 			show_idle = 1;
    119  1.4       jtc 			break;
    120  1.5    kleink 		case 'H':
    121  1.5    kleink 			show_labels = 1;
    122  1.5    kleink 			break;
    123  1.4       jtc 		default:
    124  1.4       jtc 			usage();
    125  1.4       jtc 			/* NOTREACHED */
    126  1.4       jtc 		}
    127  1.4       jtc 	}
    128  1.4       jtc 	argc -= optind;
    129  1.4       jtc 	argv += optind;
    130  1.4       jtc 
    131  1.4       jtc 	if (chdir("/dev")) {
    132  1.4       jtc 		err(1, "cannot change directory to /dev");
    133  1.4       jtc 		/* NOTREACHED */
    134  1.4       jtc 	}
    135  1.1       cgd 
    136  1.1       cgd 	switch (argc) {
    137  1.4       jtc 	case 0:					/* who */
    138  1.4       jtc 		if (only_current_term) {
    139  1.9  christos 			who_am_i(NULL, show_labels);
    140  1.4       jtc 		} else {
    141  1.9  christos 			process(NULL, show_labels);
    142  1.4       jtc 		}
    143  1.4       jtc 		break;
    144  1.4       jtc 	case 1:					/* who utmp_file */
    145  1.4       jtc 		if (only_current_term) {
    146  1.9  christos 			who_am_i(*argv, show_labels);
    147  1.4       jtc 		} else {
    148  1.9  christos 			process(*argv, show_labels);
    149  1.4       jtc 		}
    150  1.1       cgd 		break;
    151  1.4       jtc 	case 2:					/* who am i */
    152  1.9  christos 		who_am_i(NULL, show_labels);
    153  1.1       cgd 		break;
    154  1.1       cgd 	default:
    155  1.4       jtc 		usage();
    156  1.4       jtc 		/* NOTREACHED */
    157  1.1       cgd 	}
    158  1.1       cgd 	exit(0);
    159  1.1       cgd }
    160  1.1       cgd 
    161  1.9  christos #if defined(SUPPORT_UTMPX) && defined(SUPPORT_UTMP)
    162  1.9  christos static void
    163  1.9  christos adjust_size(struct entry *e)
    164  1.9  christos {
    165  1.9  christos 	int max;
    166  1.9  christos 
    167  1.9  christos 	if ((max = strlen(e->name)) > maxname)
    168  1.9  christos 		maxname = max;
    169  1.9  christos 	if ((max = strlen(e->line)) > maxline)
    170  1.9  christos 		maxline = max;
    171  1.9  christos 	if ((max = strlen(e->host)) > maxhost)
    172  1.9  christos 		maxhost = max;
    173  1.9  christos }
    174  1.9  christos 
    175  1.9  christos static int
    176  1.9  christos setup(const char *fname)
    177  1.9  christos {
    178  1.9  christos 	int what = 3;
    179  1.9  christos 
    180  1.9  christos 	if (fname == NULL) {
    181  1.9  christos #ifdef SUPPORT_UTMPX
    182  1.9  christos 		setutent();
    183  1.9  christos #endif
    184  1.9  christos #ifdef SUPPORT_UTMP
    185  1.9  christos 		setutxent();
    186  1.9  christos #endif
    187  1.9  christos 	} else {
    188  1.9  christos 		size_t len = strlen(fname);
    189  1.9  christos 		if (len == 0)
    190  1.9  christos 			errx(1, "Filename cannot be 0 length.");
    191  1.9  christos 		what = fname[len - 1] == 'x' ? 1 : 2;
    192  1.9  christos 		if (what == 1) {
    193  1.9  christos #ifdef SUPPORT_UTMPX
    194  1.9  christos 			if (utmpxname(fname) == 0)
    195  1.9  christos 				err(1, "Cannot open `%s'", fname);
    196  1.9  christos #else
    197  1.9  christos 			errx(1, "utmpx support not compiled in");
    198  1.9  christos #endif
    199  1.9  christos 		} else {
    200  1.9  christos #ifdef SUPPORT_UTMPX
    201  1.9  christos 			if (utmpname(fname) == 0)
    202  1.9  christos 				err(1, "Cannot open `%s'", fname);
    203  1.9  christos #else
    204  1.9  christos 			errx(1, "utmp support not compiled in");
    205  1.9  christos #endif
    206  1.9  christos 		}
    207  1.9  christos 	}
    208  1.9  christos 	return what;
    209  1.9  christos }
    210  1.9  christos #endif
    211  1.9  christos 
    212  1.9  christos static void
    213  1.9  christos who_am_i(const char *fname, int show_labels)
    214  1.4       jtc {
    215  1.9  christos #ifdef SUPPORT_UTMPX
    216  1.9  christos 	struct utmpx *utx;
    217  1.9  christos #endif
    218  1.9  christos #ifdef SUPPORT_UTMP
    219  1.9  christos 	struct utmp *ut;
    220  1.9  christos #endif
    221  1.4       jtc 	struct passwd *pw;
    222  1.6     lukem 	char *p;
    223  1.4       jtc 	char *t;
    224  1.9  christos 	time_t now;
    225  1.9  christos #if defined(SUPPORT_UTMP) && defined(SUPPORT_UTMPX)
    226  1.9  christos 	int what = setup(fname);
    227  1.9  christos #endif
    228  1.4       jtc 
    229  1.4       jtc 	/* search through the utmp and find an entry for this tty */
    230  1.9  christos 	if ((p = ttyname(STDIN_FILENO)) != NULL) {
    231  1.9  christos 
    232  1.4       jtc 		/* strip any directory component */
    233  1.6     lukem 		if ((t = strrchr(p, '/')) != NULL)
    234  1.4       jtc 			p = t + 1;
    235  1.9  christos #ifdef SUPPORT_UTMPX
    236  1.9  christos 		while ((what & 1) && (utx = getutxent()) != NULL)
    237  1.9  christos 			if (utx->ut_type == USER_PROCESS &&
    238  1.9  christos 			    !strcmp(utx->ut_line, p)) {
    239  1.9  christos 				struct entry e;
    240  1.9  christos 				getentryx(&e, utx);
    241  1.9  christos 				if (show_labels)
    242  1.9  christos 					output_labels();
    243  1.9  christos 				print(e.name, e.line, e.time, e.host);
    244  1.4       jtc 				return;
    245  1.4       jtc 			}
    246  1.9  christos #endif
    247  1.9  christos #ifdef SUPPORT_UTMP
    248  1.9  christos 		while ((what & 2) && (ut = getutent()) != NULL)
    249  1.9  christos 			if (!strcmp(ut->ut_line, p)) {
    250  1.9  christos 				struct entry e;
    251  1.9  christos 				getentry(&e, ut);
    252  1.9  christos 				if (show_labels)
    253  1.9  christos 					output_labels();
    254  1.9  christos 				print(e.name, e.line, e.time, e.host);
    255  1.9  christos 				return;
    256  1.9  christos 			}
    257  1.9  christos #endif
    258  1.4       jtc 	} else
    259  1.9  christos 		p = "tty??";
    260  1.4       jtc 
    261  1.9  christos 	(void)time(&now);
    262  1.4       jtc 	pw = getpwuid(getuid());
    263  1.9  christos 	if (show_labels)
    264  1.9  christos 		output_labels();
    265  1.9  christos 	print(pw ? pw->pw_name : "?", p, now, "");
    266  1.9  christos }
    267  1.9  christos 
    268  1.9  christos static void
    269  1.9  christos process(const char *fname, int show_labels)
    270  1.9  christos {
    271  1.9  christos #ifdef SUPPORT_UTMPX
    272  1.9  christos 	struct utmpx *utx;
    273  1.9  christos #endif
    274  1.9  christos #ifdef SUPPORT_UTMP
    275  1.9  christos 	struct utmp *ut;
    276  1.9  christos #endif
    277  1.9  christos 	struct entry *ep, *ehead = NULL;
    278  1.9  christos #if defined(SUPPORT_UTMP) && defined(SUPPORT_UTMPX)
    279  1.9  christos 	int what = setup(fname);
    280  1.9  christos 	struct entry **nextp = &ehead;
    281  1.9  christos #endif
    282  1.9  christos 
    283  1.9  christos #ifdef SUPPORT_UTMPX
    284  1.9  christos 	while ((what & 1) && (utx = getutxent()) != NULL) {
    285  1.9  christos 		if (fname == NULL && utx->ut_type != USER_PROCESS)
    286  1.9  christos 			continue;
    287  1.9  christos 		if ((ep = calloc(1, sizeof(struct entry))) == NULL)
    288  1.9  christos 			err(1, NULL);
    289  1.9  christos 		getentryx(ep, utx);
    290  1.9  christos 		*nextp = ep;
    291  1.9  christos 		nextp = &(ep->next);
    292  1.9  christos 	}
    293  1.9  christos #endif
    294  1.9  christos 
    295  1.9  christos #ifdef SUPPORT_UTMP
    296  1.9  christos 	while ((what & 2) && (ut = getutent()) != NULL) {
    297  1.9  christos 		if (fname == NULL && (*ut->ut_name == '\0' ||
    298  1.9  christos 		    *ut->ut_line == '\0'))
    299  1.9  christos 			continue;
    300  1.9  christos 		/* Don't process entries that we have utmpx for */
    301  1.9  christos 		for (ep = ehead; ep != NULL; ep = ep->next) {
    302  1.9  christos 			if (strncmp(ep->line, ut->ut_line,
    303  1.9  christos 			    sizeof(ut->ut_line)) == 0)
    304  1.9  christos 				break;
    305  1.9  christos 		}
    306  1.9  christos 		if (ep != NULL)
    307  1.9  christos 			continue;
    308  1.9  christos 		if ((ep = calloc(1, sizeof(struct entry))) == NULL)
    309  1.9  christos 			err(1, NULL);
    310  1.9  christos 		getentry(ep, ut);
    311  1.9  christos 		*nextp = ep;
    312  1.9  christos 		nextp = &(ep->next);
    313  1.9  christos 	}
    314  1.9  christos #endif
    315  1.9  christos #if defined(SUPPORT_UTMP) && defined(SUPPORT_UTMPX)
    316  1.9  christos 	if (ehead != NULL) {
    317  1.9  christos 		struct entry *from = ehead, *save;
    318  1.9  christos 
    319  1.9  christos 		ehead = NULL;
    320  1.9  christos 		while (from != NULL) {
    321  1.9  christos 			for (nextp = &ehead;
    322  1.9  christos 			    (*nextp) && strcmp(from->line, (*nextp)->line) > 0;
    323  1.9  christos 			    nextp = &(*nextp)->next)
    324  1.9  christos 				continue;
    325  1.9  christos 			save = from;
    326  1.9  christos 			from = from->next;
    327  1.9  christos 			save->next = *nextp;
    328  1.9  christos 			*nextp = save;
    329  1.9  christos 		}
    330  1.9  christos 	}
    331  1.9  christos #endif
    332  1.9  christos 	if (show_labels)
    333  1.9  christos 		output_labels();
    334  1.9  christos 	for (ep = ehead; ep != NULL; ep = ep->next)
    335  1.9  christos 		print(ep->name, ep->line, ep->time, ep->host);
    336  1.9  christos }
    337  1.9  christos 
    338  1.9  christos #ifdef SUPPORT_UTMP
    339  1.9  christos static void
    340  1.9  christos getentry(struct entry *e, struct utmp *up)
    341  1.9  christos {
    342  1.9  christos 	(void)strncpy(e->name, up->ut_name, sizeof(up->ut_name));
    343  1.9  christos 	e->name[sizeof(e->name) - 1] = '\0';
    344  1.9  christos 	(void)strncpy(e->line, up->ut_line, sizeof(up->ut_line));
    345  1.9  christos 	e->line[sizeof(e->line) - 1] = '\0';
    346  1.9  christos 	(void)strncpy(e->host, up->ut_host, sizeof(up->ut_host));
    347  1.9  christos 	e->name[sizeof(e->host) - 1] = '\0';
    348  1.9  christos 	e->time = up->ut_time;
    349  1.9  christos 	adjust_size(e);
    350  1.9  christos }
    351  1.9  christos #endif
    352  1.9  christos 
    353  1.9  christos #ifdef SUPPORT_UTMPX
    354  1.9  christos static void
    355  1.9  christos getentryx(struct entry *e, struct utmpx *up)
    356  1.9  christos {
    357  1.9  christos 	(void)strncpy(e->name, up->ut_name, sizeof(up->ut_name));
    358  1.9  christos 	e->name[sizeof(e->name) - 1] = '\0';
    359  1.9  christos 	(void)strncpy(e->line, up->ut_line, sizeof(up->ut_line));
    360  1.9  christos 	e->line[sizeof(e->line) - 1] = '\0';
    361  1.9  christos 	(void)strncpy(e->host, up->ut_host, sizeof(up->ut_host));
    362  1.9  christos 	e->name[sizeof(e->host) - 1] = '\0';
    363  1.9  christos 	e->time = (time_t)up->ut_tv.tv_sec;
    364  1.9  christos 	adjust_size(e);
    365  1.4       jtc }
    366  1.9  christos #endif
    367  1.9  christos 
    368  1.4       jtc 
    369  1.9  christos static void
    370  1.9  christos print(const char *name, const char *line, time_t t, const char *host)
    371  1.1       cgd {
    372  1.4       jtc 	struct stat sb;
    373  1.4       jtc 	char state;
    374  1.4       jtc 	static time_t now = 0;
    375  1.4       jtc 	time_t idle;
    376  1.4       jtc 
    377  1.5    kleink 	state = '?';
    378  1.5    kleink 	idle = 0;
    379  1.5    kleink 
    380  1.4       jtc 	if (show_term || show_idle) {
    381  1.4       jtc 		if (now == 0)
    382  1.4       jtc 			time(&now);
    383  1.4       jtc 
    384  1.4       jtc 		if (stat(line, &sb) == 0) {
    385  1.4       jtc 			state = (sb.st_mode & 020) ? '+' : '-';
    386  1.4       jtc 			idle = now - sb.st_atime;
    387  1.4       jtc 		}
    388  1.4       jtc 
    389  1.4       jtc 	}
    390  1.4       jtc 
    391  1.9  christos 	(void)printf("%-*.*s ", maxname, maxname, name);
    392  1.4       jtc 
    393  1.4       jtc 	if (show_term) {
    394  1.4       jtc 		(void)printf("%c ", state);
    395  1.4       jtc 	}
    396  1.4       jtc 
    397  1.9  christos 	(void)printf("%-*.*s ", maxline, maxline, line);
    398  1.9  christos 	(void)printf("%.12s ", ctime(&t) + 4);
    399  1.1       cgd 
    400  1.4       jtc 	if (show_idle) {
    401  1.4       jtc 		if (idle < 60)
    402  1.4       jtc 			(void)printf("  .   ");
    403  1.4       jtc 		else if (idle < (24 * 60 * 60))
    404  1.5    kleink 			(void)printf("%02ld:%02ld ",
    405  1.5    kleink 				     (long)(idle / (60 * 60)),
    406  1.5    kleink 				     (long)(idle % (60 * 60)) / 60);
    407  1.4       jtc 		else
    408  1.4       jtc 			(void)printf(" old  ");
    409  1.4       jtc 	}
    410  1.4       jtc 
    411  1.9  christos 	if (*host)
    412  1.9  christos 		printf("\t(%.*s)", maxhost, host);
    413  1.1       cgd 	(void)putchar('\n');
    414  1.1       cgd }
    415  1.1       cgd 
    416  1.9  christos static void
    417  1.5    kleink output_labels()
    418  1.5    kleink {
    419  1.9  christos 	(void)printf("%-*.*s ", maxname, maxname, "USER");
    420  1.5    kleink 
    421  1.5    kleink 	if (show_term)
    422  1.5    kleink 		(void)printf("S ");
    423  1.5    kleink 
    424  1.9  christos 	(void)printf("%-*.*s ", maxline, maxline, "LINE");
    425  1.5    kleink 	(void)printf("WHEN         ");
    426  1.5    kleink 
    427  1.5    kleink 	if (show_idle)
    428  1.5    kleink 		(void)printf("IDLE  ");
    429  1.5    kleink 
    430  1.9  christos 	(void)printf("\t%.*s", maxhost, "FROM");
    431  1.5    kleink 
    432  1.5    kleink 	(void)putchar('\n');
    433  1.5    kleink }
    434  1.5    kleink 
    435  1.9  christos static void
    436  1.4       jtc usage()
    437  1.4       jtc {
    438  1.9  christos 	(void)fprintf(stderr, "Usage: %s [-mTuH] [ file ]\n       %s am i\n",
    439  1.9  christos 	    getprogname(), getprogname());
    440  1.4       jtc 	exit(1);
    441  1.1       cgd }
    442