11590Srgrimes/* 21590Srgrimes * Copyright (c) 1980, 1987, 1993 31590Srgrimes * The Regents of the University of California. All rights reserved. 41590Srgrimes * 51590Srgrimes * Redistribution and use in source and binary forms, with or without 61590Srgrimes * modification, are permitted provided that the following conditions 71590Srgrimes * are met: 81590Srgrimes * 1. Redistributions of source code must retain the above copyright 91590Srgrimes * notice, this list of conditions and the following disclaimer. 101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111590Srgrimes * notice, this list of conditions and the following disclaimer in the 121590Srgrimes * documentation and/or other materials provided with the distribution. 131590Srgrimes * 4. Neither the name of the University nor the names of its contributors 141590Srgrimes * may be used to endorse or promote products derived from this software 151590Srgrimes * without specific prior written permission. 161590Srgrimes * 171590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 181590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 191590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 201590Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 211590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 221590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 231590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 241590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 251590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 261590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 271590Srgrimes * SUCH DAMAGE. 281590Srgrimes */ 291590Srgrimes 301590Srgrimes#ifndef lint 3128563Scharnierstatic const char copyright[] = 321590Srgrimes"@(#) Copyright (c) 1980, 1987, 1993\n\ 331590Srgrimes The Regents of the University of California. All rights reserved.\n"; 341590Srgrimes#endif /* not lint */ 351590Srgrimes 361590Srgrimes#ifndef lint 3728563Scharnier#if 0 381590Srgrimesstatic char sccsid[] = "@(#)users.c 8.1 (Berkeley) 6/6/93"; 3928563Scharnier#endif 4028563Scharnierstatic const char rcsid[] = 4150477Speter "$FreeBSD: releng/10.3/usr.bin/users/users.c 223222 2011-06-18 07:47:15Z ed $"; 421590Srgrimes#endif /* not lint */ 431590Srgrimes 44200066Sed#include <sys/param.h> 451590Srgrimes#include <sys/types.h> 4628563Scharnier#include <err.h> 4728563Scharnier#include <stdio.h> 4828563Scharnier#include <stdlib.h> 4928563Scharnier#include <string.h> 5028563Scharnier#include <unistd.h> 51202200Sed#include <utmpx.h> 521590Srgrimes 53223222Sedtypedef char namebuf[sizeof(((struct utmpx *)0)->ut_user) + 1]; 541590Srgrimes 5592922Simpint scmp(const void *, const void *); 5692922Simpstatic void usage(void); 5728563Scharnier 5828789Scharnierint 59102944Sdwmalonemain(int argc, char **argv) 601590Srgrimes{ 6128563Scharnier namebuf *names = NULL; 6228563Scharnier int ncnt = 0; 6328563Scharnier int nmax = 0; 6428563Scharnier int cnt; 65200154Sed struct utmpx *ut; 6628563Scharnier int ch; 671590Srgrimes 6824360Simp while ((ch = getopt(argc, argv, "")) != -1) 691590Srgrimes switch(ch) { 701590Srgrimes case '?': 711590Srgrimes default: 7228563Scharnier usage(); 731590Srgrimes } 741590Srgrimes argc -= optind; 751590Srgrimes argv += optind; 761590Srgrimes 77200154Sed setutxent(); 78200154Sed while ((ut = getutxent()) != NULL) { 79200067Sed if (ut->ut_type != USER_PROCESS) 80200066Sed continue; 81200066Sed if (ncnt >= nmax) { 82200066Sed nmax += 32; 83200066Sed names = realloc(names, sizeof(*names) * nmax); 84200066Sed if (!names) { 85200066Sed errx(1, "realloc"); 86200066Sed /* NOTREACHED */ 871590Srgrimes } 881590Srgrimes } 89200066Sed (void)strlcpy(names[ncnt], ut->ut_user, sizeof(*names)); 90200066Sed ++ncnt; 9128563Scharnier } 92200154Sed endutxent(); 93200066Sed if (ncnt > 0) { 94200066Sed qsort(names, ncnt, sizeof(namebuf), scmp); 95200066Sed (void)printf("%s", names[0]); 961590Srgrimes for (cnt = 1; cnt < ncnt; ++cnt) 97200066Sed if (strcmp(names[cnt], names[cnt - 1]) != 0) 98200066Sed (void)printf(" %s", names[cnt]); 991590Srgrimes (void)printf("\n"); 1001590Srgrimes } 1011590Srgrimes exit(0); 1021590Srgrimes} 1031590Srgrimes 10428563Scharnierstatic void 105102944Sdwmaloneusage(void) 10628563Scharnier{ 10728563Scharnier (void)fprintf(stderr, "usage: users\n"); 10828563Scharnier exit(1); 10928563Scharnier} 11028563Scharnier 11128563Scharnierint 112102944Sdwmalonescmp(const void *p, const void *q) 1131590Srgrimes{ 114200066Sed 115200066Sed return (strcmp(p, q)); 1161590Srgrimes} 117