1319Sgbrunett /*
2*13093SRoger.Faulkner@Oracle.COM * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
3319Sgbrunett */
4319Sgbrunett
50Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
60Sstevel@tonic-gate /* All Rights Reserved */
70Sstevel@tonic-gate
80Sstevel@tonic-gate /*
90Sstevel@tonic-gate * Copyright (c) 1980 Regents of the University of California.
100Sstevel@tonic-gate * All rights reserved. The Berkeley software License Agreement
110Sstevel@tonic-gate * specifies the terms and conditions for redistribution.
120Sstevel@tonic-gate */
130Sstevel@tonic-gate
140Sstevel@tonic-gate /*
150Sstevel@tonic-gate * users
160Sstevel@tonic-gate */
170Sstevel@tonic-gate
180Sstevel@tonic-gate #include <stdio.h>
190Sstevel@tonic-gate #include <sys/types.h>
200Sstevel@tonic-gate #include <stdlib.h>
210Sstevel@tonic-gate #include <utmpx.h>
22319Sgbrunett #include <string.h>
230Sstevel@tonic-gate
24319Sgbrunett static char **names;
25319Sgbrunett static char **namp;
260Sstevel@tonic-gate
27319Sgbrunett static int scmp(const void *p, const void *q);
28319Sgbrunett static void summary(void);
290Sstevel@tonic-gate
30319Sgbrunett int
main(int argc,char ** argv)31319Sgbrunett main(int argc, char **argv)
320Sstevel@tonic-gate {
330Sstevel@tonic-gate int nusers = 0;
340Sstevel@tonic-gate int bufflen = BUFSIZ;
35319Sgbrunett struct utmpx *utmpx;
360Sstevel@tonic-gate
370Sstevel@tonic-gate if (argc == 2)
380Sstevel@tonic-gate if (!utmpxname(argv[1])) {
39319Sgbrunett (void) fprintf(stderr, "Filename is too long\n");
400Sstevel@tonic-gate exit(1);
410Sstevel@tonic-gate }
420Sstevel@tonic-gate
430Sstevel@tonic-gate names = namp = (char **)realloc((void *)NULL, BUFSIZ * sizeof (char *));
440Sstevel@tonic-gate
450Sstevel@tonic-gate setutxent();
460Sstevel@tonic-gate
470Sstevel@tonic-gate while ((utmpx = getutxent()) != NULL) {
480Sstevel@tonic-gate if (utmpx->ut_name[0] == '\0')
490Sstevel@tonic-gate continue;
500Sstevel@tonic-gate if (utmpx->ut_type != USER_PROCESS)
510Sstevel@tonic-gate continue;
520Sstevel@tonic-gate if (nonuserx(*utmpx))
530Sstevel@tonic-gate continue;
540Sstevel@tonic-gate if (nusers == bufflen) {
550Sstevel@tonic-gate bufflen *= 2;
560Sstevel@tonic-gate names = (char **)realloc(names,
57*13093SRoger.Faulkner@Oracle.COM bufflen * sizeof (char *));
580Sstevel@tonic-gate namp = names + nusers;
590Sstevel@tonic-gate }
600Sstevel@tonic-gate *namp++ = strndup(utmpx->ut_name, sizeof (utmpx->ut_name));
610Sstevel@tonic-gate nusers++;
620Sstevel@tonic-gate }
630Sstevel@tonic-gate
640Sstevel@tonic-gate endutxent();
650Sstevel@tonic-gate
660Sstevel@tonic-gate summary();
67319Sgbrunett return (0);
680Sstevel@tonic-gate }
690Sstevel@tonic-gate
70319Sgbrunett static int
scmp(const void * p,const void * q)710Sstevel@tonic-gate scmp(const void *p, const void *q)
720Sstevel@tonic-gate {
730Sstevel@tonic-gate return (strcmp((char *)p, (char *)q));
740Sstevel@tonic-gate }
750Sstevel@tonic-gate
76319Sgbrunett static void
summary(void)77319Sgbrunett summary(void)
780Sstevel@tonic-gate {
790Sstevel@tonic-gate register char **p;
800Sstevel@tonic-gate
810Sstevel@tonic-gate qsort(names, namp - names, sizeof (names[0]), scmp);
820Sstevel@tonic-gate for (p = names; p < namp; p++) {
830Sstevel@tonic-gate if (p != names)
84319Sgbrunett (void) putchar(' ');
85319Sgbrunett (void) fputs(*p, stdout);
860Sstevel@tonic-gate }
870Sstevel@tonic-gate if (namp != names) /* at least one user */
88319Sgbrunett (void) putchar('\n');
890Sstevel@tonic-gate }
90