state the format for clarity
[unix-history] / usr / src / usr.bin / users / users.c
CommitLineData
f42904bc 1/*
57f00f07
KB
2 * Copyright (c) 1980, 1987 Regents of the University of California.
3 * All rights reserved.
4 *
32ce521f 5 * %sccs.include.redist.c%
f42904bc
DF
6 */
7
8#ifndef lint
9char copyright[] =
57f00f07 10"@(#) Copyright (c) 1980, 1987 Regents of the University of California.\n\
f42904bc 11 All rights reserved.\n";
57f00f07 12#endif /* not lint */
f42904bc
DF
13
14#ifndef lint
32ce521f 15static char sccsid[] = "@(#)users.c 5.9 (Berkeley) %G%";
57f00f07 16#endif /* not lint */
f42904bc 17
abe5b335 18#include <sys/types.h>
94b8b339 19#include <errno.h>
407aebd6 20#include <utmp.h>
abe5b335 21#include <stdio.h>
407aebd6 22
94b8b339 23#define MAXUSERS 200
407aebd6 24
ff7e7f72 25main()
407aebd6 26{
94b8b339
KB
27 register int cnt, ncnt;
28 struct utmp utmp;
29 char names[MAXUSERS][UT_NAMESIZE];
30 int scmp();
407aebd6 31
94b8b339 32 if (!freopen(_PATH_UTMP, "r", stdin)) {
f5506e0f 33 (void)fprintf(stderr, "users: can't open %s.\n", _PATH_UTMP);
57f00f07 34 exit(1);
407aebd6 35 }
94b8b339
KB
36 for (ncnt = 0;
37 fread((char *)&utmp, sizeof(utmp), 1, stdin) == 1;)
abe5b335 38 if (*utmp.ut_name) {
94b8b339
KB
39 if (ncnt == MAXUSERS) {
40 (void)fprintf(stderr,
41 "users: too many users.\n");
abe5b335
KB
42 break;
43 }
94b8b339
KB
44 (void)strncpy(names[ncnt], utmp.ut_name, UT_NAMESIZE);
45 ++ncnt;
abe5b335 46 }
407aebd6 47
94b8b339
KB
48 if (ncnt) {
49 qsort(names, ncnt, UT_NAMESIZE, scmp);
50 (void)printf("%s", names[0]);
51 for (cnt = 1; cnt < ncnt; ++cnt) {
52 while (cnt < ncnt - 1 &&
53 !strncmp(names[cnt], names[cnt + 1], UT_NAMESIZE))
54 ++cnt;
55 (void)printf(" %.*s", UT_NAMESIZE, names[cnt]);
56 }
57 (void)printf("\n");
407aebd6 58 }
94b8b339 59 exit(0);
abe5b335
KB
60}
61
57f00f07 62scmp(p, q)
94b8b339 63 char *p, *q;
abe5b335 64{
94b8b339 65 return(strcmp(p, q));
407aebd6 66}