aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEd Schouten <ed@FreeBSD.org>2010-04-11 12:02:13 +0000
committerEd Schouten <ed@FreeBSD.org>2010-04-11 12:02:13 +0000
commitaacd7d6afc4ba98c63165ed85376c7837a70e920 (patch)
tree96eac4c8a6be34da1f629d046c32b724fb8917b1
parentd8c136591af009ff38f9a4620d0848b97cfa1e98 (diff)
downloadsrc-aacd7d6afc4ba98c63165ed85376c7837a70e920.tar.gz
src-aacd7d6afc4ba98c63165ed85376c7837a70e920.zip
Alphabetically sort the output of lastlogin(8).
According to the manpage, the entries have to be sorted by uid. This is no longer possible, since our utmpx implementation is completely unaware of user IDs. You can safely add entries for multiple users sharing the same uid. Make the output less random by sorting everything by name.
Notes
Notes: svn path=/head/; revision=206471
-rw-r--r--usr.sbin/lastlogin/lastlogin.82
-rw-r--r--usr.sbin/lastlogin/lastlogin.c24
2 files changed, 22 insertions, 4 deletions
diff --git a/usr.sbin/lastlogin/lastlogin.8 b/usr.sbin/lastlogin/lastlogin.8
index 4d07a2bd249c..063016399e77 100644
--- a/usr.sbin/lastlogin/lastlogin.8
+++ b/usr.sbin/lastlogin/lastlogin.8
@@ -55,7 +55,7 @@ If more than one
is given, the session information for each user is printed in
the order given on the command line.
Otherwise, information
-for all users is printed, sorted by uid.
+for all users is printed, sorted by name.
.Pp
The
.Nm
diff --git a/usr.sbin/lastlogin/lastlogin.c b/usr.sbin/lastlogin/lastlogin.c
index ae3ba996f269..9c3d433cc6be 100644
--- a/usr.sbin/lastlogin/lastlogin.c
+++ b/usr.sbin/lastlogin/lastlogin.c
@@ -39,6 +39,7 @@ __RCSID("$NetBSD: lastlogin.c,v 1.4 1998/02/03 04:45:35 perry Exp $");
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include <time.h>
#include <unistd.h>
#include <utmpx.h>
@@ -47,11 +48,19 @@ __RCSID("$NetBSD: lastlogin.c,v 1.4 1998/02/03 04:45:35 perry Exp $");
static void output(struct utmpx *);
static void usage(void);
+static int
+utcmp(const void *u1, const void *u2)
+{
+
+ return (strcmp(((const struct utmpx *)u1)->ut_user,
+ ((const struct utmpx *)u2)->ut_user));
+}
+
int
main(int argc, char *argv[])
{
- int ch, i;
- struct utmpx *u;
+ int ch, i, ulistsize;
+ struct utmpx *u, *ulist;
while ((ch = getopt(argc, argv, "")) != -1) {
usage();
@@ -74,12 +83,21 @@ main(int argc, char *argv[])
else {
if (setutxdb(UTXDB_LASTLOGIN, NULL) != 0)
errx(1, "failed to open lastlog database");
+ ulist = NULL;
+ ulistsize = 0;
while ((u = getutxent()) != NULL) {
if (u->ut_type != USER_PROCESS)
continue;
- output(u);
+ if ((ulistsize % 16) == 0)
+ ulist = realloc(ulist,
+ (ulistsize + 16) * sizeof(struct utmpx));
+ ulist[ulistsize++] = *u;
}
endutxent();
+
+ qsort(ulist, ulistsize, sizeof(struct utmpx), utcmp);
+ for (i = 0; i < ulistsize; i++)
+ output(&ulist[i]);
}
exit(0);