xref: /original-bsd/usr.bin/users/users.c (revision 4d072710)
1 /*
2  * Copyright (c) 1980, 1987 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 char copyright[] =
20 "@(#) Copyright (c) 1980, 1987 Regents of the University of California.\n\
21  All rights reserved.\n";
22 #endif /* not lint */
23 
24 #ifndef lint
25 static char sccsid[] = "@(#)users.c	5.6 (Berkeley) 06/29/88";
26 #endif /* not lint */
27 
28 /*
29  * users
30  */
31 #include <sys/types.h>
32 #include <utmp.h>
33 #include <stdio.h>
34 
35 #define NMAX		sizeof(utmp.ut_name)
36 #define MAXUSERS	200
37 #define UTMP_FILE	"/etc/utmp"
38 
39 static struct utmp utmp;		/* read structure */
40 static int ncnt;			/* count of names */
41 static char *names[MAXUSERS];		/* names table */
42 
43 main()
44 {
45 	register FILE *fp;		/* file pointer */
46 
47 	if (!(fp = fopen(UTMP_FILE, "r"))) {
48 		perror(UTMP_FILE);
49 		exit(1);
50 	}
51 	while (fread((char *)&utmp, sizeof(utmp), 1, fp) == 1)
52 		if (*utmp.ut_name) {
53 			if (++ncnt > MAXUSERS) {
54 				ncnt = MAXUSERS;
55 				fputs("users: too many users.\n", stderr);
56 				break;
57 			}
58 			nsave();
59 		}
60 	summary();
61 	exit(0);
62 }
63 
64 nsave()
65 {
66 	static char **namp = names;	/* pointer to names table */
67 	char *calloc();
68 
69 	if (!(*namp = calloc((u_int)(NMAX + 1), sizeof(char)))) {
70 		fputs("users: malloc error.\n", stderr);
71 		exit(1);
72 	}
73 	bcopy(utmp.ut_name, *namp++, NMAX);
74 }
75 
76 summary()
77 {
78 	register char **p;
79 	int scmp();
80 
81 	if (!ncnt)
82 		return;
83 	qsort((char *)names, ncnt, sizeof(names[0]), scmp);
84 	fputs(names[0], stdout);
85 	for (p = &names[1]; --ncnt; ++p) {
86 		putchar(' ');
87 		fputs(*p, stdout);
88 	}
89 	putchar('\n');
90 }
91 
92 scmp(p, q)
93 	char **p, **q;
94 {
95 	return(strcmp(*p, *q));
96 }
97