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