| 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 |
| 8 | char 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 |
| 14 | static char sccsid[] = "@(#)users.c 5.4 (Berkeley) %G%"; |
| 15 | #endif not lint |
| 16 | |
| 17 | /* |
| 18 | * users |
| 19 | */ |
| 20 | #include <sys/types.h> |
| 21 | #include <utmp.h> |
| 22 | #include <stdio.h> |
| 23 | |
| 24 | #define ERREXIT 1 |
| 25 | #define OKEXIT 0 |
| 26 | #define NMAX sizeof(utmp.ut_name) |
| 27 | #define MAXUSERS 200 |
| 28 | #define UTMP_FILE "/etc/utmp" |
| 29 | |
| 30 | static struct utmp utmp; /* read structure */ |
| 31 | static int ncnt; /* count of names */ |
| 32 | static char *names[MAXUSERS]; /* names table */ |
| 33 | |
| 34 | main() |
| 35 | { |
| 36 | register FILE *fp; /* file pointer */ |
| 37 | |
| 38 | if (!(fp = fopen(UTMP_FILE,"r"))) { |
| 39 | perror(UTMP_FILE); |
| 40 | exit(ERREXIT); |
| 41 | } |
| 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 | } |
| 51 | summary(); |
| 52 | exit(OKEXIT); |
| 53 | } |
| 54 | |
| 55 | nsave() |
| 56 | { |
| 57 | static char **namp = names; /* pointer to names table */ |
| 58 | char *calloc(); |
| 59 | |
| 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); |
| 65 | } |
| 66 | |
| 67 | summary() |
| 68 | { |
| 69 | register char **p; |
| 70 | int scmp(); |
| 71 | |
| 72 | if (!ncnt) |
| 73 | return; |
| 74 | qsort((char *)names,ncnt,sizeof(names[0]),scmp); |
| 75 | fputs(names[0],stdout); |
| 76 | for (p = &names[1];--ncnt;++p) { |
| 77 | putchar(' '); |
| 78 | fputs(*p,stdout); |
| 79 | } |
| 80 | putchar('\n'); |
| 81 | } |
| 82 | |
| 83 | scmp(p,q) |
| 84 | char **p, |
| 85 | **q; |
| 86 | { |
| 87 | return(strcmp(*p,*q)); |
| 88 | } |