1f22acdffSgbrunett /*
2*23a1cceaSRoger A. Faulkner * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
3f22acdffSgbrunett */
4f22acdffSgbrunett
57c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
67c478bd9Sstevel@tonic-gate /* All Rights Reserved */
77c478bd9Sstevel@tonic-gate
87c478bd9Sstevel@tonic-gate /*
97c478bd9Sstevel@tonic-gate * Copyright (c) 1980 Regents of the University of California.
107c478bd9Sstevel@tonic-gate * All rights reserved. The Berkeley software License Agreement
117c478bd9Sstevel@tonic-gate * specifies the terms and conditions for redistribution.
127c478bd9Sstevel@tonic-gate */
137c478bd9Sstevel@tonic-gate
147c478bd9Sstevel@tonic-gate /*
157c478bd9Sstevel@tonic-gate * users
167c478bd9Sstevel@tonic-gate */
177c478bd9Sstevel@tonic-gate
187c478bd9Sstevel@tonic-gate #include <stdio.h>
197c478bd9Sstevel@tonic-gate #include <sys/types.h>
207c478bd9Sstevel@tonic-gate #include <stdlib.h>
217c478bd9Sstevel@tonic-gate #include <utmpx.h>
22f22acdffSgbrunett #include <string.h>
23f22acdffSgbrunett
24f22acdffSgbrunett static char **names;
25f22acdffSgbrunett static char **namp;
267c478bd9Sstevel@tonic-gate
27f22acdffSgbrunett static int scmp(const void *p, const void *q);
28f22acdffSgbrunett static void summary(void);
297c478bd9Sstevel@tonic-gate
30f22acdffSgbrunett int
main(int argc,char ** argv)31f22acdffSgbrunett main(int argc, char **argv)
327c478bd9Sstevel@tonic-gate {
337c478bd9Sstevel@tonic-gate int nusers = 0;
347c478bd9Sstevel@tonic-gate int bufflen = BUFSIZ;
35f22acdffSgbrunett struct utmpx *utmpx;
367c478bd9Sstevel@tonic-gate
377c478bd9Sstevel@tonic-gate if (argc == 2)
387c478bd9Sstevel@tonic-gate if (!utmpxname(argv[1])) {
39f22acdffSgbrunett (void) fprintf(stderr, "Filename is too long\n");
407c478bd9Sstevel@tonic-gate exit(1);
417c478bd9Sstevel@tonic-gate }
427c478bd9Sstevel@tonic-gate
437c478bd9Sstevel@tonic-gate names = namp = (char **)realloc((void *)NULL, BUFSIZ * sizeof (char *));
447c478bd9Sstevel@tonic-gate
457c478bd9Sstevel@tonic-gate setutxent();
467c478bd9Sstevel@tonic-gate
477c478bd9Sstevel@tonic-gate while ((utmpx = getutxent()) != NULL) {
487c478bd9Sstevel@tonic-gate if (utmpx->ut_name[0] == '\0')
497c478bd9Sstevel@tonic-gate continue;
507c478bd9Sstevel@tonic-gate if (utmpx->ut_type != USER_PROCESS)
517c478bd9Sstevel@tonic-gate continue;
527c478bd9Sstevel@tonic-gate if (nonuserx(*utmpx))
537c478bd9Sstevel@tonic-gate continue;
547c478bd9Sstevel@tonic-gate if (nusers == bufflen) {
557c478bd9Sstevel@tonic-gate bufflen *= 2;
567c478bd9Sstevel@tonic-gate names = (char **)realloc(names,
577c478bd9Sstevel@tonic-gate bufflen * sizeof (char *));
587c478bd9Sstevel@tonic-gate namp = names + nusers;
597c478bd9Sstevel@tonic-gate }
607c478bd9Sstevel@tonic-gate *namp++ = strndup(utmpx->ut_name, sizeof (utmpx->ut_name));
617c478bd9Sstevel@tonic-gate nusers++;
627c478bd9Sstevel@tonic-gate }
637c478bd9Sstevel@tonic-gate
647c478bd9Sstevel@tonic-gate endutxent();
657c478bd9Sstevel@tonic-gate
667c478bd9Sstevel@tonic-gate summary();
67f22acdffSgbrunett return (0);
687c478bd9Sstevel@tonic-gate }
697c478bd9Sstevel@tonic-gate
70f22acdffSgbrunett static int
scmp(const void * p,const void * q)717c478bd9Sstevel@tonic-gate scmp(const void *p, const void *q)
727c478bd9Sstevel@tonic-gate {
737c478bd9Sstevel@tonic-gate return (strcmp((char *)p, (char *)q));
747c478bd9Sstevel@tonic-gate }
757c478bd9Sstevel@tonic-gate
76f22acdffSgbrunett static void
summary(void)77f22acdffSgbrunett summary(void)
787c478bd9Sstevel@tonic-gate {
797c478bd9Sstevel@tonic-gate register char **p;
807c478bd9Sstevel@tonic-gate
817c478bd9Sstevel@tonic-gate qsort(names, namp - names, sizeof (names[0]), scmp);
827c478bd9Sstevel@tonic-gate for (p = names; p < namp; p++) {
837c478bd9Sstevel@tonic-gate if (p != names)
84f22acdffSgbrunett (void) putchar(' ');
85f22acdffSgbrunett (void) fputs(*p, stdout);
867c478bd9Sstevel@tonic-gate }
877c478bd9Sstevel@tonic-gate if (namp != names) /* at least one user */
88f22acdffSgbrunett (void) putchar('\n');
897c478bd9Sstevel@tonic-gate }
90