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 <bsd.prog.mk>

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 <g...@freebsd.org>
+ * 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 <sys/param.h>
-#include <sys/types.h>
-#include <err.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-#include <utmpx.h>
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
 
-typedef char namebuf[sizeof(((struct utmpx *)0)->ut_user) + 1];
-typedef int (*scmp)(const void *, const void *);
+#include <utmpx.h>
 
-static void usage(void);
+#include <algorithm>
+#include <iostream>
+#include <iterator>
+#include <string>
+#include <vector>
+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<string> 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<string>::iterator last(unique(begin(names), end(names)));
+       copy(begin(names), last-1, ostream_iterator<string>(cout, " "));
+       cout << *(last-1) << endl;
 }
_______________________________________________
svn-src-head@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/svn-src-head
To unsubscribe, send any mail to "svn-src-head-unsubscr...@freebsd.org"

Reply via email to