users.c revision 50477
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 * 3. All advertising materials mentioning features or use of this software 141590Srgrimes * must display the following acknowledgement: 151590Srgrimes * This product includes software developed by the University of 161590Srgrimes * California, Berkeley and its contributors. 171590Srgrimes * 4. Neither the name of the University nor the names of its contributors 181590Srgrimes * may be used to endorse or promote products derived from this software 191590Srgrimes * without specific prior written permission. 201590Srgrimes * 211590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241590Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311590Srgrimes * SUCH DAMAGE. 321590Srgrimes */ 331590Srgrimes 341590Srgrimes#ifndef lint 3528563Scharnierstatic const char copyright[] = 361590Srgrimes"@(#) Copyright (c) 1980, 1987, 1993\n\ 371590Srgrimes The Regents of the University of California. All rights reserved.\n"; 381590Srgrimes#endif /* not lint */ 391590Srgrimes 401590Srgrimes#ifndef lint 4128563Scharnier#if 0 421590Srgrimesstatic char sccsid[] = "@(#)users.c 8.1 (Berkeley) 6/6/93"; 4328563Scharnier#endif 4428563Scharnierstatic const char rcsid[] = 4550477Speter "$FreeBSD: head/usr.bin/users/users.c 50477 1999-08-28 01:08:13Z peter $"; 461590Srgrimes#endif /* not lint */ 471590Srgrimes 481590Srgrimes#include <sys/types.h> 4928563Scharnier#include <err.h> 5028563Scharnier#include <stdio.h> 5128563Scharnier#include <stdlib.h> 5228563Scharnier#include <string.h> 5328563Scharnier#include <unistd.h> 541590Srgrimes#include <utmp.h> 551590Srgrimes 5628563Scharniertypedef char namebuf[UT_NAMESIZE]; 571590Srgrimes 5828563Scharnierint scmp __P((const void *, const void *)); 5928563Scharnierstatic void usage __P((void)); 6028563Scharnier 6128789Scharnierint 621590Srgrimesmain(argc, argv) 631590Srgrimes int argc; 641590Srgrimes char **argv; 651590Srgrimes{ 6628563Scharnier namebuf *names = NULL; 6728563Scharnier int ncnt = 0; 6828563Scharnier int nmax = 0; 6928563Scharnier int cnt; 701590Srgrimes struct utmp utmp; 7128563Scharnier int ch; 721590Srgrimes 7324360Simp while ((ch = getopt(argc, argv, "")) != -1) 741590Srgrimes switch(ch) { 751590Srgrimes case '?': 761590Srgrimes default: 7728563Scharnier usage(); 781590Srgrimes } 791590Srgrimes argc -= optind; 801590Srgrimes argv += optind; 811590Srgrimes 8228563Scharnier if (!freopen(_PATH_UTMP, "r", stdin)) 8328563Scharnier errx(1, "can't open %s", _PATH_UTMP); 8428563Scharnier while (fread((char *)&utmp, sizeof(utmp), 1, stdin) == 1) { 851590Srgrimes if (*utmp.ut_name) { 8628563Scharnier if (ncnt >= nmax) { 8728563Scharnier nmax += 32; 8828563Scharnier names = realloc(names, sizeof (*names) * nmax); 8928563Scharnier if (!names) { 9028563Scharnier errx(1, "realloc"); 9128563Scharnier /* NOTREACHED */ 9228563Scharnier } 931590Srgrimes } 941590Srgrimes (void)strncpy(names[ncnt], utmp.ut_name, UT_NAMESIZE); 951590Srgrimes ++ncnt; 961590Srgrimes } 9728563Scharnier } 981590Srgrimes if (ncnt) { 991590Srgrimes qsort(names, ncnt, UT_NAMESIZE, scmp); 1001590Srgrimes (void)printf("%.*s", UT_NAMESIZE, names[0]); 1011590Srgrimes for (cnt = 1; cnt < ncnt; ++cnt) 1021590Srgrimes if (strncmp(names[cnt], names[cnt - 1], UT_NAMESIZE)) 1031590Srgrimes (void)printf(" %.*s", UT_NAMESIZE, names[cnt]); 1041590Srgrimes (void)printf("\n"); 1051590Srgrimes } 1061590Srgrimes exit(0); 1071590Srgrimes} 1081590Srgrimes 10928563Scharnierstatic void 11028563Scharnierusage() 11128563Scharnier{ 11228563Scharnier (void)fprintf(stderr, "usage: users\n"); 11328563Scharnier exit(1); 11428563Scharnier} 11528563Scharnier 11628563Scharnierint 1171590Srgrimesscmp(p, q) 11828563Scharnier const void *p, *q; 1191590Srgrimes{ 12028563Scharnier return(strncmp((char *)p, (char *)q, UT_NAMESIZE)); 1211590Srgrimes} 122