xref: /titanic_53/usr/src/ucbcmd/users/users.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
2*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
3*7c478bd9Sstevel@tonic-gate 
4*7c478bd9Sstevel@tonic-gate 
5*7c478bd9Sstevel@tonic-gate /*
6*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
7*7c478bd9Sstevel@tonic-gate  * All rights reserved.  The Berkeley software License Agreement
8*7c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
9*7c478bd9Sstevel@tonic-gate  */
10*7c478bd9Sstevel@tonic-gate 
11*7c478bd9Sstevel@tonic-gate /*
12*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1983-1998 by Sun Microsystems, Inc.
13*7c478bd9Sstevel@tonic-gate  * All rights reserved.
14*7c478bd9Sstevel@tonic-gate  */
15*7c478bd9Sstevel@tonic-gate 
16*7c478bd9Sstevel@tonic-gate #ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.1	*/
17*7c478bd9Sstevel@tonic-gate 
18*7c478bd9Sstevel@tonic-gate /*
19*7c478bd9Sstevel@tonic-gate  * users
20*7c478bd9Sstevel@tonic-gate  */
21*7c478bd9Sstevel@tonic-gate 
22*7c478bd9Sstevel@tonic-gate #include <stdio.h>
23*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
24*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
25*7c478bd9Sstevel@tonic-gate #include <utmpx.h>
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate static	char	*strndup(char *p, int n);
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate struct utmpx *utmpx;
30*7c478bd9Sstevel@tonic-gate char	**names;
31*7c478bd9Sstevel@tonic-gate char	**namp;
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate main(argc, argv)
34*7c478bd9Sstevel@tonic-gate char **argv;
35*7c478bd9Sstevel@tonic-gate {
36*7c478bd9Sstevel@tonic-gate 	char	 *tp;
37*7c478bd9Sstevel@tonic-gate 	int	nusers = 0;
38*7c478bd9Sstevel@tonic-gate 	int	bufflen = BUFSIZ;
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate 	if (argc == 2)
41*7c478bd9Sstevel@tonic-gate 		if (!utmpxname(argv[1])) {
42*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Filename is too long\n");
43*7c478bd9Sstevel@tonic-gate 			exit(1);
44*7c478bd9Sstevel@tonic-gate 		}
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate 	names = namp = (char **)realloc((void *)NULL, BUFSIZ * sizeof (char *));
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate 	setutxent();
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate 	while ((utmpx = getutxent()) != NULL) {
51*7c478bd9Sstevel@tonic-gate 		if (utmpx->ut_name[0] == '\0')
52*7c478bd9Sstevel@tonic-gate 			continue;
53*7c478bd9Sstevel@tonic-gate 		if (utmpx->ut_type != USER_PROCESS)
54*7c478bd9Sstevel@tonic-gate 			continue;
55*7c478bd9Sstevel@tonic-gate 		if (nonuserx(*utmpx))
56*7c478bd9Sstevel@tonic-gate 			continue;
57*7c478bd9Sstevel@tonic-gate 		if (nusers == bufflen) {
58*7c478bd9Sstevel@tonic-gate 			bufflen *= 2;
59*7c478bd9Sstevel@tonic-gate 			names = (char **)realloc(names,
60*7c478bd9Sstevel@tonic-gate 						bufflen * sizeof (char *));
61*7c478bd9Sstevel@tonic-gate 			namp = names + nusers;
62*7c478bd9Sstevel@tonic-gate 		}
63*7c478bd9Sstevel@tonic-gate 		*namp++ = strndup(utmpx->ut_name, sizeof (utmpx->ut_name));
64*7c478bd9Sstevel@tonic-gate 		nusers++;
65*7c478bd9Sstevel@tonic-gate 	}
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate 	endutxent();
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate 	summary();
70*7c478bd9Sstevel@tonic-gate 	exit(0);
71*7c478bd9Sstevel@tonic-gate }
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate static	char	*
74*7c478bd9Sstevel@tonic-gate strndup(char *p, int n)
75*7c478bd9Sstevel@tonic-gate {
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate 	register char	*x;
78*7c478bd9Sstevel@tonic-gate 	x = malloc(n + 1);
79*7c478bd9Sstevel@tonic-gate 	strncpy(x, p, n);
80*7c478bd9Sstevel@tonic-gate 	*(x + n) = '\0';
81*7c478bd9Sstevel@tonic-gate 	return (x);
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate }
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate scmp(const void *p, const void *q)
86*7c478bd9Sstevel@tonic-gate {
87*7c478bd9Sstevel@tonic-gate 	return (strcmp((char *)p, (char *)q));
88*7c478bd9Sstevel@tonic-gate }
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate summary()
91*7c478bd9Sstevel@tonic-gate {
92*7c478bd9Sstevel@tonic-gate 	register char **p;
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate 	qsort(names, namp - names, sizeof (names[0]), scmp);
95*7c478bd9Sstevel@tonic-gate 	for (p = names; p < namp; p++) {
96*7c478bd9Sstevel@tonic-gate 		if (p != names)
97*7c478bd9Sstevel@tonic-gate 			putchar(' ');
98*7c478bd9Sstevel@tonic-gate 		fputs(*p, stdout);
99*7c478bd9Sstevel@tonic-gate 	}
100*7c478bd9Sstevel@tonic-gate 	if (namp != names)		/* at least one user */
101*7c478bd9Sstevel@tonic-gate 		putchar('\n');
102*7c478bd9Sstevel@tonic-gate }
103