Commit | Line | Data |
---|---|---|
f42904bc | 1 | /* |
0f32f273 KB |
2 | * Copyright (c) 1980, 1987, 1993 |
3 | * The Regents of the University of California. All rights reserved. | |
57f00f07 | 4 | * |
32ce521f | 5 | * %sccs.include.redist.c% |
f42904bc DF |
6 | */ |
7 | ||
8 | #ifndef lint | |
0f32f273 KB |
9 | static char copyright[] = |
10 | "@(#) Copyright (c) 1980, 1987, 1993\n\ | |
11 | The Regents of the University of California. All rights reserved.\n"; | |
57f00f07 | 12 | #endif /* not lint */ |
f42904bc DF |
13 | |
14 | #ifndef lint | |
0f32f273 | 15 | static char sccsid[] = "@(#)users.c 8.1 (Berkeley) %G%"; |
57f00f07 | 16 | #endif /* not lint */ |
f42904bc | 17 | |
abe5b335 | 18 | #include <sys/types.h> |
407aebd6 | 19 | #include <utmp.h> |
abe5b335 | 20 | #include <stdio.h> |
407aebd6 | 21 | |
94b8b339 | 22 | #define MAXUSERS 200 |
407aebd6 | 23 | |
434340f2 KB |
24 | main(argc, argv) |
25 | int argc; | |
26 | char **argv; | |
407aebd6 | 27 | { |
434340f2 | 28 | extern int optind; |
94b8b339 KB |
29 | register int cnt, ncnt; |
30 | struct utmp utmp; | |
31 | char names[MAXUSERS][UT_NAMESIZE]; | |
434340f2 KB |
32 | int ch, scmp(); |
33 | ||
34 | while ((ch = getopt(argc, argv, "")) != EOF) | |
35 | switch(ch) { | |
36 | case '?': | |
37 | default: | |
38 | (void)fprintf(stderr, "usage: users\n"); | |
39 | exit(1); | |
40 | } | |
41 | argc -= optind; | |
42 | argv += optind; | |
407aebd6 | 43 | |
94b8b339 | 44 | if (!freopen(_PATH_UTMP, "r", stdin)) { |
f5506e0f | 45 | (void)fprintf(stderr, "users: can't open %s.\n", _PATH_UTMP); |
57f00f07 | 46 | exit(1); |
407aebd6 | 47 | } |
94b8b339 KB |
48 | for (ncnt = 0; |
49 | fread((char *)&utmp, sizeof(utmp), 1, stdin) == 1;) | |
abe5b335 | 50 | if (*utmp.ut_name) { |
94b8b339 KB |
51 | if (ncnt == MAXUSERS) { |
52 | (void)fprintf(stderr, | |
53 | "users: too many users.\n"); | |
abe5b335 KB |
54 | break; |
55 | } | |
94b8b339 KB |
56 | (void)strncpy(names[ncnt], utmp.ut_name, UT_NAMESIZE); |
57 | ++ncnt; | |
abe5b335 | 58 | } |
407aebd6 | 59 | |
94b8b339 KB |
60 | if (ncnt) { |
61 | qsort(names, ncnt, UT_NAMESIZE, scmp); | |
65086738 | 62 | (void)printf("%.*s", UT_NAMESIZE, names[0]); |
79d24029 KB |
63 | for (cnt = 1; cnt < ncnt; ++cnt) |
64 | if (strncmp(names[cnt], names[cnt - 1], UT_NAMESIZE)) | |
65 | (void)printf(" %.*s", UT_NAMESIZE, names[cnt]); | |
94b8b339 | 66 | (void)printf("\n"); |
407aebd6 | 67 | } |
94b8b339 | 68 | exit(0); |
abe5b335 KB |
69 | } |
70 | ||
57f00f07 | 71 | scmp(p, q) |
94b8b339 | 72 | char *p, *q; |
abe5b335 | 73 | { |
65086738 | 74 | return(strncmp(p, q, UT_NAMESIZE)); |
407aebd6 | 75 | } |