date and time created 88/09/26 21:34:00 by bostic
[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 *
5 * Redistribution and use in source and binary forms are permitted
5e8b0e60
KB
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
f42904bc
DF
16 */
17
18#ifndef lint
19char copyright[] =
57f00f07 20"@(#) Copyright (c) 1980, 1987 Regents of the University of California.\n\
f42904bc 21 All rights reserved.\n";
57f00f07 22#endif /* not lint */
f42904bc
DF
23
24#ifndef lint
5e8b0e60 25static char sccsid[] = "@(#)users.c 5.6 (Berkeley) %G%";
57f00f07 26#endif /* not lint */
f42904bc 27
407aebd6
BJ
28/*
29 * users
30 */
abe5b335 31#include <sys/types.h>
407aebd6 32#include <utmp.h>
abe5b335 33#include <stdio.h>
407aebd6 34
abe5b335
KB
35#define NMAX sizeof(utmp.ut_name)
36#define MAXUSERS 200
ff7e7f72 37#define UTMP_FILE "/etc/utmp"
407aebd6 38
57f00f07
KB
39static struct utmp utmp; /* read structure */
40static int ncnt; /* count of names */
41static char *names[MAXUSERS]; /* names table */
407aebd6 42
ff7e7f72 43main()
407aebd6 44{
57f00f07 45 register FILE *fp; /* file pointer */
407aebd6 46
57f00f07 47 if (!(fp = fopen(UTMP_FILE, "r"))) {
ff7e7f72 48 perror(UTMP_FILE);
57f00f07 49 exit(1);
407aebd6 50 }
57f00f07 51 while (fread((char *)&utmp, sizeof(utmp), 1, fp) == 1)
abe5b335
KB
52 if (*utmp.ut_name) {
53 if (++ncnt > MAXUSERS) {
54 ncnt = MAXUSERS;
57f00f07 55 fputs("users: too many users.\n", stderr);
abe5b335
KB
56 break;
57 }
58 nsave();
59 }
ff7e7f72 60 summary();
57f00f07 61 exit(0);
407aebd6
BJ
62}
63
abe5b335 64nsave()
407aebd6 65{
57f00f07
KB
66 static char **namp = names; /* pointer to names table */
67 char *calloc();
407aebd6 68
57f00f07
KB
69 if (!(*namp = calloc((u_int)(NMAX + 1), sizeof(char)))) {
70 fputs("users: malloc error.\n", stderr);
71 exit(1);
abe5b335 72 }
57f00f07 73 bcopy(utmp.ut_name, *namp++, NMAX);
407aebd6 74}
abe5b335 75
407aebd6
BJ
76summary()
77{
57f00f07
KB
78 register char **p;
79 int scmp();
407aebd6 80
ff7e7f72
KB
81 if (!ncnt)
82 return;
57f00f07
KB
83 qsort((char *)names, ncnt, sizeof(names[0]), scmp);
84 fputs(names[0], stdout);
85 for (p = &names[1]; --ncnt; ++p) {
ff7e7f72 86 putchar(' ');
57f00f07 87 fputs(*p, stdout);
407aebd6 88 }
abe5b335
KB
89 putchar('\n');
90}
91
57f00f07
KB
92scmp(p, q)
93 char **p, **q;
abe5b335 94{
57f00f07 95 return(strcmp(*p, *q));
407aebd6 96}