From owner-svn-src-all@FreeBSD.ORG Thu Jul 10 12:15:03 2014 Return-Path: Delivered-To: svn-src-all@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 83F69946; Thu, 10 Jul 2014 12:15:03 +0000 (UTC) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:1900:2254:2068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 7188C2426; Thu, 10 Jul 2014 12:15:03 +0000 (UTC) Received: from svn.freebsd.org ([127.0.1.70]) by svn.freebsd.org (8.14.8/8.14.8) with ESMTP id s6ACF352055262; Thu, 10 Jul 2014 12:15:03 GMT (envelope-from gahr@svn.freebsd.org) Received: (from gahr@localhost) by svn.freebsd.org (8.14.8/8.14.8/Submit) id s6ACF3v1055260; Thu, 10 Jul 2014 12:15:03 GMT (envelope-from gahr@svn.freebsd.org) Message-Id: <201407101215.s6ACF3v1055260@svn.freebsd.org> From: Pietro Cerutti Date: Thu, 10 Jul 2014 12:15:03 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r268491 - head/usr.bin/users X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 10 Jul 2014 12:15:03 -0000 Author: gahr (ports committer) Date: Thu Jul 10 12:15:02 2014 New Revision: 268491 URL: http://svnweb.freebsd.org/changeset/base/268491 Log: Reimplements users(1) in C++. This reduces the lines of code by roughly 50% (not counting the COPYRIGHT header) and makes it more readable by using standard algorithms. Approved by: bapt Added: head/usr.bin/users/users.cc (contents, props changed) - copied, changed from r268030, head/usr.bin/users/users.c Deleted: head/usr.bin/users/users.c Modified: head/usr.bin/users/Makefile (contents, props changed) Modified: head/usr.bin/users/Makefile ============================================================================== --- head/usr.bin/users/Makefile Thu Jul 10 11:20:24 2014 (r268490) +++ head/usr.bin/users/Makefile Thu Jul 10 12:15:02 2014 (r268491) @@ -1,6 +1,7 @@ # @(#)Makefile 8.1 (Berkeley) 6/6/93 # $FreeBSD$ -PROG= users +WARNS= 3 +PROG_CXX= users .include Copied and modified: head/usr.bin/users/users.cc (from r268030, head/usr.bin/users/users.c) ============================================================================== --- head/usr.bin/users/users.c Mon Jun 30 05:33:52 2014 (r268030, copy source) +++ head/usr.bin/users/users.cc Thu Jul 10 12:15:02 2014 (r268491) @@ -1,6 +1,6 @@ /* - * Copyright (c) 1980, 1987, 1993 - * The Regents of the University of California. All rights reserved. + * Copyright (c) 2014 Pietro Cerutti + * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -27,83 +27,43 @@ * SUCH DAMAGE. */ -#ifndef lint -static const char copyright[] = -"@(#) Copyright (c) 1980, 1987, 1993\n\ - The Regents of the University of California. All rights reserved.\n"; -#endif /* not lint */ - -#ifndef lint -#if 0 -static char sccsid[] = "@(#)users.c 8.1 (Berkeley) 6/6/93"; -#endif -static const char rcsid[] = - "$FreeBSD$"; -#endif /* not lint */ - -#include -#include -#include -#include -#include -#include -#include -#include +#include +__FBSDID("$FreeBSD$"); -typedef char namebuf[sizeof(((struct utmpx *)0)->ut_user) + 1]; -typedef int (*scmp)(const void *, const void *); +#include -static void usage(void); +#include +#include +#include +#include +#include +using namespace std; int -main(int argc, char **argv) +main(int argc, char **) { - namebuf *names = NULL; - int ncnt = 0; - int nmax = 0; - int cnt; struct utmpx *ut; - int ch; + vector names; - while ((ch = getopt(argc, argv, "")) != -1) - switch(ch) { - case '?': - default: - usage(); - } - argc -= optind; - argv += optind; + if (argc > 1) { + cerr << "usage: users" << endl; + return (1); + } setutxent(); while ((ut = getutxent()) != NULL) { if (ut->ut_type != USER_PROCESS) continue; - if (ncnt >= nmax) { - nmax += 32; - names = realloc(names, sizeof(*names) * nmax); - if (!names) { - errx(1, "realloc"); - /* NOTREACHED */ - } - } - strlcpy(names[ncnt], ut->ut_user, sizeof(*names)); - ++ncnt; + names.push_back(ut->ut_user); } endutxent(); - if (ncnt > 0) { - qsort(names, ncnt, sizeof(*names), (scmp)strcmp); - printf("%s", names[0]); - for (cnt = 1; cnt < ncnt; ++cnt) - if (strcmp(names[cnt], names[cnt - 1]) != 0) - printf(" %s", names[cnt]); - printf("\n"); + + if (names.size() == 0) { + return (0); } - exit(0); -} -static void -usage(void) -{ - fprintf(stderr, "usage: users\n"); - exit(1); + sort(begin(names), end(names)); + vector::iterator last(unique(begin(names), end(names))); + copy(begin(names), last-1, ostream_iterator(cout, " ")); + cout << *(last-1) << endl; }