xref: /original-bsd/usr.bin/users/users.c (revision 8477994b)
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.11 (Berkeley) 08/21/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(argc, argv)
26 	int argc;
27 	char **argv;
28 {
29 	extern int optind;
30 	register int cnt, ncnt;
31 	struct utmp utmp;
32 	char names[MAXUSERS][UT_NAMESIZE];
33 	int ch, scmp();
34 
35 	while ((ch = getopt(argc, argv, "")) != EOF)
36 		switch(ch) {
37 		case '?':
38 		default:
39 			(void)fprintf(stderr, "usage: users\n");
40 			exit(1);
41 		}
42 	argc -= optind;
43 	argv += optind;
44 
45 	if (!freopen(_PATH_UTMP, "r", stdin)) {
46 		(void)fprintf(stderr, "users: can't open %s.\n", _PATH_UTMP);
47 		exit(1);
48 	}
49 	for (ncnt = 0;
50 	    fread((char *)&utmp, sizeof(utmp), 1, stdin) == 1;)
51 		if (*utmp.ut_name) {
52 			if (ncnt == MAXUSERS) {
53 				(void)fprintf(stderr,
54 				    "users: too many users.\n");
55 				break;
56 			}
57 			(void)strncpy(names[ncnt], utmp.ut_name, UT_NAMESIZE);
58 			++ncnt;
59 		}
60 
61 	if (ncnt) {
62 		qsort(names, ncnt, UT_NAMESIZE, scmp);
63 		(void)printf("%s", names[0]);
64 		for (cnt = 1; cnt < ncnt; ++cnt)
65 			if (strncmp(names[cnt], names[cnt - 1], UT_NAMESIZE))
66 				(void)printf(" %.*s", UT_NAMESIZE, names[cnt]);
67 		(void)printf("\n");
68 	}
69 	exit(0);
70 }
71 
72 scmp(p, q)
73 	char *p, *q;
74 {
75 	return(strcmp(p, q));
76 }
77