xref: /original-bsd/usr.bin/users/users.c (revision c3e32dec)
1 /*
2  * Copyright (c) 1980, 1987, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char copyright[] =
10 "@(#) Copyright (c) 1980, 1987, 1993\n\
11 	The Regents of the University of California.  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)users.c	8.1 (Berkeley) 06/06/93";
16 #endif /* not lint */
17 
18 #include <sys/types.h>
19 #include <utmp.h>
20 #include <stdio.h>
21 
22 #define	MAXUSERS	200
23 
24 main(argc, argv)
25 	int argc;
26 	char **argv;
27 {
28 	extern int optind;
29 	register int cnt, ncnt;
30 	struct utmp utmp;
31 	char names[MAXUSERS][UT_NAMESIZE];
32 	int ch, scmp();
33 
34 	while ((ch = getopt(argc, argv, "")) != EOF)
35 		switch(ch) {
36 		case '?':
37 		default:
38 			(void)fprintf(stderr, "usage: users\n");
39 			exit(1);
40 		}
41 	argc -= optind;
42 	argv += optind;
43 
44 	if (!freopen(_PATH_UTMP, "r", stdin)) {
45 		(void)fprintf(stderr, "users: can't open %s.\n", _PATH_UTMP);
46 		exit(1);
47 	}
48 	for (ncnt = 0;
49 	    fread((char *)&utmp, sizeof(utmp), 1, stdin) == 1;)
50 		if (*utmp.ut_name) {
51 			if (ncnt == MAXUSERS) {
52 				(void)fprintf(stderr,
53 				    "users: too many users.\n");
54 				break;
55 			}
56 			(void)strncpy(names[ncnt], utmp.ut_name, UT_NAMESIZE);
57 			++ncnt;
58 		}
59 
60 	if (ncnt) {
61 		qsort(names, ncnt, UT_NAMESIZE, scmp);
62 		(void)printf("%.*s", UT_NAMESIZE, names[0]);
63 		for (cnt = 1; cnt < ncnt; ++cnt)
64 			if (strncmp(names[cnt], names[cnt - 1], UT_NAMESIZE))
65 				(void)printf(" %.*s", UT_NAMESIZE, names[cnt]);
66 		(void)printf("\n");
67 	}
68 	exit(0);
69 }
70 
71 scmp(p, q)
72 	char *p, *q;
73 {
74 	return(strncmp(p, q, UT_NAMESIZE));
75 }
76