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