summaryrefslogtreecommitdiff
path: root/usr.bin/users
diff options
context:
space:
mode:
authorPhilippe Charnier <charnier@FreeBSD.org>1997-08-25 06:25:54 +0000
committerPhilippe Charnier <charnier@FreeBSD.org>1997-08-25 06:25:54 +0000
commit259847d5dbe2e9c13054aea92030ec850cf3d6e3 (patch)
tree308f19da77b1dc68a7a25d4bd2857f58dc118dd9 /usr.bin/users
parentdfbc6f338873e9aff8584504faaa07b40cb4dae6 (diff)
Notes
Diffstat (limited to 'usr.bin/users')
-rw-r--r--usr.bin/users/users.12
-rw-r--r--usr.bin/users/users.c64
2 files changed, 43 insertions, 23 deletions
diff --git a/usr.bin/users/users.1 b/usr.bin/users/users.1
index 57ae2dbdeef2..04f5c6a2c066 100644
--- a/usr.bin/users/users.1
+++ b/usr.bin/users/users.1
@@ -38,7 +38,7 @@
.Nm users
.Nd list current users
.Sh SYNOPSIS
-.Nm users
+.Nm
.Sh DESCRIPTION
.Nm Users
lists the login names of the users currently on the system,
diff --git a/usr.bin/users/users.c b/usr.bin/users/users.c
index 11571f2bad18..3ff79c85d041 100644
--- a/usr.bin/users/users.c
+++ b/usr.bin/users/users.c
@@ -32,57 +32,69 @@
*/
#ifndef lint
-static char copyright[] =
+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[] =
+ "$Id$";
#endif /* not lint */
#include <sys/types.h>
-#include <utmp.h>
+#include <err.h>
#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <utmp.h>
+
+typedef char namebuf[UT_NAMESIZE];
-#define MAXUSERS 200
+int scmp __P((const void *, const void *));
+static void usage __P((void));
+int
main(argc, argv)
int argc;
char **argv;
{
- extern int optind;
- register int cnt, ncnt;
+ namebuf *names = NULL;
+ int ncnt = 0;
+ int nmax = 0;
+ int cnt;
struct utmp utmp;
- char names[MAXUSERS][UT_NAMESIZE];
- int ch, scmp();
+ int ch;
while ((ch = getopt(argc, argv, "")) != EOF)
switch(ch) {
case '?':
default:
- (void)fprintf(stderr, "usage: users\n");
- exit(1);
+ usage();
}
argc -= optind;
argv += optind;
- if (!freopen(_PATH_UTMP, "r", stdin)) {
- (void)fprintf(stderr, "users: can't open %s.\n", _PATH_UTMP);
- exit(1);
- }
- for (ncnt = 0;
- fread((char *)&utmp, sizeof(utmp), 1, stdin) == 1;)
+ if (!freopen(_PATH_UTMP, "r", stdin))
+ errx(1, "can't open %s", _PATH_UTMP);
+ while (fread((char *)&utmp, sizeof(utmp), 1, stdin) == 1) {
if (*utmp.ut_name) {
- if (ncnt == MAXUSERS) {
- (void)fprintf(stderr,
- "users: too many users.\n");
- break;
+ if (ncnt >= nmax) {
+ nmax += 32;
+ names = realloc(names, sizeof (*names) * nmax);
+ if (!names) {
+ errx(1, "realloc");
+ /* NOTREACHED */
+ }
}
(void)strncpy(names[ncnt], utmp.ut_name, UT_NAMESIZE);
++ncnt;
}
-
+ }
if (ncnt) {
qsort(names, ncnt, UT_NAMESIZE, scmp);
(void)printf("%.*s", UT_NAMESIZE, names[0]);
@@ -94,8 +106,16 @@ main(argc, argv)
exit(0);
}
+static void
+usage()
+{
+ (void)fprintf(stderr, "usage: users\n");
+ exit(1);
+}
+
+int
scmp(p, q)
- char *p, *q;
+ const void *p, *q;
{
- return(strncmp(p, q, UT_NAMESIZE));
+ return(strncmp((char *)p, (char *)q, UT_NAMESIZE));
}