date and time created 87/02/15 16:03:36 by lepreau
[unix-history] / usr / src / usr.bin / users / users.c
CommitLineData
f42904bc
DF
1/*
2 * Copyright (c) 1980 Regents of the University of California.
3 * All rights reserved. The Berkeley software License Agreement
4 * specifies the terms and conditions for redistribution.
5 */
6
7#ifndef lint
8char copyright[] =
9"@(#) Copyright (c) 1980 Regents of the University of California.\n\
10 All rights reserved.\n";
11#endif not lint
12
13#ifndef lint
ff7e7f72 14static char sccsid[] = "@(#)users.c 5.4 (Berkeley) %G%";
f42904bc
DF
15#endif not lint
16
407aebd6
BJ
17/*
18 * users
19 */
abe5b335 20#include <sys/types.h>
407aebd6 21#include <utmp.h>
abe5b335 22#include <stdio.h>
407aebd6 23
abe5b335
KB
24#define ERREXIT 1
25#define OKEXIT 0
26#define NMAX sizeof(utmp.ut_name)
27#define MAXUSERS 200
ff7e7f72 28#define UTMP_FILE "/etc/utmp"
407aebd6 29
abe5b335
KB
30static struct utmp utmp; /* read structure */
31static int ncnt; /* count of names */
ff7e7f72 32static char *names[MAXUSERS]; /* names table */
407aebd6 33
ff7e7f72 34main()
407aebd6 35{
abe5b335 36 register FILE *fp; /* file pointer */
407aebd6 37
ff7e7f72
KB
38 if (!(fp = fopen(UTMP_FILE,"r"))) {
39 perror(UTMP_FILE);
abe5b335 40 exit(ERREXIT);
407aebd6 41 }
abe5b335
KB
42 while (fread((char *)&utmp,sizeof(utmp),1,fp) == 1)
43 if (*utmp.ut_name) {
44 if (++ncnt > MAXUSERS) {
45 ncnt = MAXUSERS;
46 fputs("users: too many users.\n",stderr);
47 break;
48 }
49 nsave();
50 }
ff7e7f72 51 summary();
abe5b335 52 exit(OKEXIT);
407aebd6
BJ
53}
54
abe5b335 55nsave()
407aebd6 56{
ff7e7f72 57 static char **namp = names; /* pointer to names table */
abe5b335 58 char *calloc();
407aebd6 59
abe5b335
KB
60 if (!(*namp = calloc((u_int)(NMAX + 1),sizeof(char)))) {
61 fputs("users: malloc error.\n",stderr);
62 exit(ERREXIT);
63 }
64 bcopy(utmp.ut_name,*namp++,NMAX);
407aebd6 65}
abe5b335 66
407aebd6
BJ
67summary()
68{
ff7e7f72 69 register char **p;
abe5b335 70 int scmp();
407aebd6 71
ff7e7f72
KB
72 if (!ncnt)
73 return;
abe5b335 74 qsort((char *)names,ncnt,sizeof(names[0]),scmp);
ff7e7f72
KB
75 fputs(names[0],stdout);
76 for (p = &names[1];--ncnt;++p) {
77 putchar(' ');
abe5b335 78 fputs(*p,stdout);
407aebd6 79 }
abe5b335
KB
80 putchar('\n');
81}
82
83scmp(p,q)
84char **p,
85 **q;
86{
87 return(strcmp(*p,*q));
407aebd6 88}