summaryrefslogtreecommitdiffstats
path: root/usr.bin/users
diff options
context:
space:
mode:
authorgahr <gahr@FreeBSD.org>2014-07-10 12:15:02 +0000
committergahr <gahr@FreeBSD.org>2014-07-10 12:15:02 +0000
commitb90da9ae5d8efce3441dadbf3a04688bc3f32ac3 (patch)
treeec2441bbf0b62db7fd0931afedd919c7e9870830 /usr.bin/users
parentca3cc30361e30c5372bad28c35af233f161336ed (diff)
downloadFreeBSD-src-b90da9ae5d8efce3441dadbf3a04688bc3f32ac3.zip
FreeBSD-src-b90da9ae5d8efce3441dadbf3a04688bc3f32ac3.tar.gz
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
Diffstat (limited to 'usr.bin/users')
-rw-r--r--usr.bin/users/Makefile3
-rw-r--r--usr.bin/users/users.cc (renamed from usr.bin/users/users.c)88
2 files changed, 26 insertions, 65 deletions
diff --git a/usr.bin/users/Makefile b/usr.bin/users/Makefile
index 3d6524b..29b5983 100644
--- a/usr.bin/users/Makefile
+++ b/usr.bin/users/Makefile
@@ -1,6 +1,7 @@
# @(#)Makefile 8.1 (Berkeley) 6/6/93
# $FreeBSD$
-PROG= users
+WARNS= 3
+PROG_CXX= users
.include <bsd.prog.mk>
diff --git a/usr.bin/users/users.c b/usr.bin/users/users.cc
index e0d04e7..ef64a9f 100644
--- a/usr.bin/users/users.c
+++ b/usr.bin/users/users.cc
@@ -1,6 +1,6 @@
/*
- * Copyright (c) 1980, 1987, 1993
- * The Regents of the University of California. All rights reserved.
+ * Copyright (c) 2014 Pietro Cerutti <gahr@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 */
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
-#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>
-typedef char namebuf[sizeof(((struct utmpx *)0)->ut_user) + 1];
-typedef int (*scmp)(const void *, const void *);
-
-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;
}
OpenPOWER on IntegriCloud