1*f22acdffSgbrunett /* 2*f22acdffSgbrunett * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 3*f22acdffSgbrunett * Use is subject to license terms. 4*f22acdffSgbrunett */ 5*f22acdffSgbrunett 67c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 77c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 87c478bd9Sstevel@tonic-gate 97c478bd9Sstevel@tonic-gate 107c478bd9Sstevel@tonic-gate /* 117c478bd9Sstevel@tonic-gate * Copyright (c) 1980 Regents of the University of California. 127c478bd9Sstevel@tonic-gate * All rights reserved. The Berkeley software License Agreement 137c478bd9Sstevel@tonic-gate * specifies the terms and conditions for redistribution. 147c478bd9Sstevel@tonic-gate */ 157c478bd9Sstevel@tonic-gate 16*f22acdffSgbrunett #pragma ident "%Z%%M% %I% %E% SMI" 177c478bd9Sstevel@tonic-gate 187c478bd9Sstevel@tonic-gate /* 197c478bd9Sstevel@tonic-gate * users 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate 227c478bd9Sstevel@tonic-gate #include <stdio.h> 237c478bd9Sstevel@tonic-gate #include <sys/types.h> 247c478bd9Sstevel@tonic-gate #include <stdlib.h> 257c478bd9Sstevel@tonic-gate #include <utmpx.h> 26*f22acdffSgbrunett #include <string.h> 27*f22acdffSgbrunett 28*f22acdffSgbrunett static char **names; 29*f22acdffSgbrunett static char **namp; 307c478bd9Sstevel@tonic-gate 317c478bd9Sstevel@tonic-gate static char *strndup(char *p, int n); 32*f22acdffSgbrunett static int scmp(const void *p, const void *q); 33*f22acdffSgbrunett static void summary(void); 347c478bd9Sstevel@tonic-gate 35*f22acdffSgbrunett int 36*f22acdffSgbrunett main(int argc, char **argv) 377c478bd9Sstevel@tonic-gate { 387c478bd9Sstevel@tonic-gate int nusers = 0; 397c478bd9Sstevel@tonic-gate int bufflen = BUFSIZ; 40*f22acdffSgbrunett struct utmpx *utmpx; 417c478bd9Sstevel@tonic-gate 427c478bd9Sstevel@tonic-gate if (argc == 2) 437c478bd9Sstevel@tonic-gate if (!utmpxname(argv[1])) { 44*f22acdffSgbrunett (void) fprintf(stderr, "Filename is too long\n"); 457c478bd9Sstevel@tonic-gate exit(1); 467c478bd9Sstevel@tonic-gate } 477c478bd9Sstevel@tonic-gate 487c478bd9Sstevel@tonic-gate names = namp = (char **)realloc((void *)NULL, BUFSIZ * sizeof (char *)); 497c478bd9Sstevel@tonic-gate 507c478bd9Sstevel@tonic-gate setutxent(); 517c478bd9Sstevel@tonic-gate 527c478bd9Sstevel@tonic-gate while ((utmpx = getutxent()) != NULL) { 537c478bd9Sstevel@tonic-gate if (utmpx->ut_name[0] == '\0') 547c478bd9Sstevel@tonic-gate continue; 557c478bd9Sstevel@tonic-gate if (utmpx->ut_type != USER_PROCESS) 567c478bd9Sstevel@tonic-gate continue; 577c478bd9Sstevel@tonic-gate if (nonuserx(*utmpx)) 587c478bd9Sstevel@tonic-gate continue; 597c478bd9Sstevel@tonic-gate if (nusers == bufflen) { 607c478bd9Sstevel@tonic-gate bufflen *= 2; 617c478bd9Sstevel@tonic-gate names = (char **)realloc(names, 627c478bd9Sstevel@tonic-gate bufflen * sizeof (char *)); 637c478bd9Sstevel@tonic-gate namp = names + nusers; 647c478bd9Sstevel@tonic-gate } 657c478bd9Sstevel@tonic-gate *namp++ = strndup(utmpx->ut_name, sizeof (utmpx->ut_name)); 667c478bd9Sstevel@tonic-gate nusers++; 677c478bd9Sstevel@tonic-gate } 687c478bd9Sstevel@tonic-gate 697c478bd9Sstevel@tonic-gate endutxent(); 707c478bd9Sstevel@tonic-gate 717c478bd9Sstevel@tonic-gate summary(); 72*f22acdffSgbrunett return (0); 737c478bd9Sstevel@tonic-gate } 747c478bd9Sstevel@tonic-gate 757c478bd9Sstevel@tonic-gate static char * 767c478bd9Sstevel@tonic-gate strndup(char *p, int n) 777c478bd9Sstevel@tonic-gate { 787c478bd9Sstevel@tonic-gate 797c478bd9Sstevel@tonic-gate register char *x; 807c478bd9Sstevel@tonic-gate x = malloc(n + 1); 81*f22acdffSgbrunett (void) strlcpy(x, p, n + 1); 827c478bd9Sstevel@tonic-gate return (x); 837c478bd9Sstevel@tonic-gate 847c478bd9Sstevel@tonic-gate } 857c478bd9Sstevel@tonic-gate 86*f22acdffSgbrunett static int 877c478bd9Sstevel@tonic-gate scmp(const void *p, const void *q) 887c478bd9Sstevel@tonic-gate { 897c478bd9Sstevel@tonic-gate return (strcmp((char *)p, (char *)q)); 907c478bd9Sstevel@tonic-gate } 917c478bd9Sstevel@tonic-gate 92*f22acdffSgbrunett static void 93*f22acdffSgbrunett summary(void) 947c478bd9Sstevel@tonic-gate { 957c478bd9Sstevel@tonic-gate register char **p; 967c478bd9Sstevel@tonic-gate 977c478bd9Sstevel@tonic-gate qsort(names, namp - names, sizeof (names[0]), scmp); 987c478bd9Sstevel@tonic-gate for (p = names; p < namp; p++) { 997c478bd9Sstevel@tonic-gate if (p != names) 100*f22acdffSgbrunett (void) putchar(' '); 101*f22acdffSgbrunett (void) fputs(*p, stdout); 1027c478bd9Sstevel@tonic-gate } 1037c478bd9Sstevel@tonic-gate if (namp != names) /* at least one user */ 104*f22acdffSgbrunett (void) putchar('\n'); 1057c478bd9Sstevel@tonic-gate } 106