Annotation of 43BSD/ucb/rwho.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  * Copyright (c) 1983 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) 1983 Regents of the University of California.\n\
                     10:  All rights reserved.\n";
                     11: #endif not lint
                     12: 
                     13: #ifndef lint
                     14: static char sccsid[] = "@(#)rwho.c     5.2 (Berkeley) 6/18/85";
                     15: #endif not lint
                     16: 
                     17: #include <sys/param.h>
                     18: #include <stdio.h>
                     19: #include <sys/dir.h>
                     20: #include <protocols/rwhod.h>
                     21: 
                     22: DIR    *dirp;
                     23: 
                     24: struct whod wd;
                     25: int    utmpcmp();
                     26: #define        NUSERS  1000
                     27: struct myutmp {
                     28:        char    myhost[32];
                     29:        int     myidle;
                     30:        struct  outmp myutmp;
                     31: } myutmp[NUSERS];
                     32: int    nusers;
                     33: 
                     34: #define        WHDRSIZE        (sizeof (wd) - sizeof (wd.wd_we))
                     35: #define        RWHODIR         "/usr/spool/rwho"
                     36: /* 
                     37:  * this macro should be shared with ruptime.
                     38:  */
                     39: #define        down(w,now)     ((now) - (w)->wd_recvtime > 11 * 60)
                     40: 
                     41: char   *ctime(), *strcpy();
                     42: int    now;
                     43: int    aflg;
                     44: 
                     45: main(argc, argv)
                     46:        int argc;
                     47:        char **argv;
                     48: {
                     49:        struct direct *dp;
                     50:        int cc, width;
                     51:        register struct whod *w = &wd;
                     52:        register struct whoent *we;
                     53:        register struct myutmp *mp;
                     54:        int f, n, i;
                     55: 
                     56:        argc--, argv++;
                     57: again:
                     58:        if (argc > 0 && !strcmp(argv[0], "-a")) {
                     59:                argc--, argv++;
                     60:                aflg++;
                     61:                goto again;
                     62:        }
                     63:        (void) time(&now);
                     64:        if (chdir(RWHODIR) < 0) {
                     65:                perror(RWHODIR);
                     66:                exit(1);
                     67:        }
                     68:        dirp = opendir(".");
                     69:        if (dirp == NULL) {
                     70:                perror(RWHODIR);
                     71:                exit(1);
                     72:        }
                     73:        mp = myutmp;
                     74:        while (dp = readdir(dirp)) {
                     75:                if (dp->d_ino == 0)
                     76:                        continue;
                     77:                if (strncmp(dp->d_name, "whod.", 5))
                     78:                        continue;
                     79:                f = open(dp->d_name, 0);
                     80:                if (f < 0)
                     81:                        continue;
                     82:                cc = read(f, (char *)&wd, sizeof (struct whod));
                     83:                if (cc < WHDRSIZE) {
                     84:                        (void) close(f);
                     85:                        continue;
                     86:                }
                     87:                if (down(w,now)) {
                     88:                        (void) close(f);
                     89:                        continue;
                     90:                }
                     91:                cc -= WHDRSIZE;
                     92:                we = w->wd_we;
                     93:                for (n = cc / sizeof (struct whoent); n > 0; n--) {
                     94:                        if (aflg == 0 && we->we_idle >= 60*60) {
                     95:                                we++;
                     96:                                continue;
                     97:                        }
                     98:                        if (nusers >= NUSERS) {
                     99:                                printf("too many users\n");
                    100:                                exit(1);
                    101:                        }
                    102:                        mp->myutmp = we->we_utmp; mp->myidle = we->we_idle;
                    103:                        (void) strcpy(mp->myhost, w->wd_hostname);
                    104:                        nusers++; we++; mp++;
                    105:                }
                    106:                (void) close(f);
                    107:        }
                    108:        qsort((char *)myutmp, nusers, sizeof (struct myutmp), utmpcmp);
                    109:        mp = myutmp;
                    110:        width = 0;
                    111:        for (i = 0; i < nusers; i++) {
                    112:                int j = strlen(mp->myhost) + 1 + strlen(mp->myutmp.out_line);
                    113:                if (j > width)
                    114:                        width = j;
                    115:                mp++;
                    116:        }
                    117:        mp = myutmp;
                    118:        for (i = 0; i < nusers; i++) {
                    119:                char buf[BUFSIZ];
                    120:                (void)sprintf(buf, "%s:%s", mp->myhost, mp->myutmp.out_line);
                    121:                printf("%-8.8s %-*s %.12s",
                    122:                   mp->myutmp.out_name,
                    123:                   width,
                    124:                   buf,
                    125:                   ctime((time_t *)&mp->myutmp.out_time)+4);
                    126:                mp->myidle /= 60;
                    127:                if (mp->myidle) {
                    128:                        if (aflg) {
                    129:                                if (mp->myidle >= 100*60)
                    130:                                        mp->myidle = 100*60 - 1;
                    131:                                if (mp->myidle >= 60)
                    132:                                        printf(" %2d", mp->myidle / 60);
                    133:                                else
                    134:                                        printf("   ");
                    135:                        } else
                    136:                                printf(" ");
                    137:                        printf(":%02d", mp->myidle % 60);
                    138:                }
                    139:                printf("\n");
                    140:                mp++;
                    141:        }
                    142:        exit(0);
                    143: }
                    144: 
                    145: utmpcmp(u1, u2)
                    146:        struct myutmp *u1, *u2;
                    147: {
                    148:        int rc;
                    149: 
                    150:        rc = strncmp(u1->myutmp.out_name, u2->myutmp.out_name, 8);
                    151:        if (rc)
                    152:                return (rc);
                    153:        rc = strncmp(u1->myhost, u2->myhost, 8);
                    154:        if (rc)
                    155:                return (rc);
                    156:        return (strncmp(u1->myutmp.out_line, u2->myutmp.out_line, 8));
                    157: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.