Annotation of 43BSDReno/usr.bin/rwho/rwho.c, revision 1.1

1.1     ! root        1: /*
        !             2:  * Copyright (c) 1983 The Regents of the University of California.
        !             3:  * All rights reserved.
        !             4:  *
        !             5:  * Redistribution and use in source and binary forms are permitted
        !             6:  * provided that: (1) source distributions retain this entire copyright
        !             7:  * notice and comment, and (2) distributions including binaries display
        !             8:  * the following acknowledgement:  ``This product includes software
        !             9:  * developed by the University of California, Berkeley and its contributors''
        !            10:  * in the documentation or other materials provided with the distribution
        !            11:  * and in all advertising materials mentioning features or use of this
        !            12:  * software. Neither the name of the University nor the names of its
        !            13:  * contributors may be used to endorse or promote products derived
        !            14:  * from this software without specific prior written permission.
        !            15:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
        !            16:  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
        !            17:  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
        !            18:  */
        !            19: 
        !            20: #ifndef lint
        !            21: char copyright[] =
        !            22: "@(#) Copyright (c) 1983 The Regents of the University of California.\n\
        !            23:  All rights reserved.\n";
        !            24: #endif /* not lint */
        !            25: 
        !            26: #ifndef lint
        !            27: static char sccsid[] = "@(#)rwho.c     5.5 (Berkeley) 6/1/90";
        !            28: #endif /* not lint */
        !            29: 
        !            30: #include <sys/param.h>
        !            31: #include <sys/dir.h>
        !            32: #include <sys/file.h>
        !            33: #include <protocols/rwhod.h>
        !            34: #include <stdio.h>
        !            35: 
        !            36: DIR    *dirp;
        !            37: 
        !            38: struct whod wd;
        !            39: int    utmpcmp();
        !            40: #define        NUSERS  1000
        !            41: struct myutmp {
        !            42:        char    myhost[MAXHOSTNAMELEN];
        !            43:        int     myidle;
        !            44:        struct  outmp myutmp;
        !            45: } myutmp[NUSERS];
        !            46: int    nusers;
        !            47: 
        !            48: #define        WHDRSIZE        (sizeof (wd) - sizeof (wd.wd_we))
        !            49: /* 
        !            50:  * this macro should be shared with ruptime.
        !            51:  */
        !            52: #define        down(w,now)     ((now) - (w)->wd_recvtime > 11 * 60)
        !            53: 
        !            54: char   *ctime(), *strcpy();
        !            55: time_t now;
        !            56: int    aflg;
        !            57: 
        !            58: main(argc, argv)
        !            59:        int argc;
        !            60:        char **argv;
        !            61: {
        !            62:        extern char *optarg;
        !            63:        extern int optind;
        !            64:        int ch;
        !            65:        struct direct *dp;
        !            66:        int cc, width;
        !            67:        register struct whod *w = &wd;
        !            68:        register struct whoent *we;
        !            69:        register struct myutmp *mp;
        !            70:        int f, n, i;
        !            71:        time_t time();
        !            72: 
        !            73:        while ((ch = getopt(argc, argv, "a")) != EOF)
        !            74:                switch((char)ch) {
        !            75:                case 'a':
        !            76:                        aflg = 1;
        !            77:                        break;
        !            78:                case '?':
        !            79:                default:
        !            80:                        fprintf(stderr, "usage: rwho [-a]\n");
        !            81:                        exit(1);
        !            82:                }
        !            83:        if (chdir(_PATH_RWHODIR) || (dirp = opendir(".")) == NULL) {
        !            84:                perror(_PATH_RWHODIR);
        !            85:                exit(1);
        !            86:        }
        !            87:        mp = myutmp;
        !            88:        (void)time(&now);
        !            89:        while (dp = readdir(dirp)) {
        !            90:                if (dp->d_ino == 0 || strncmp(dp->d_name, "whod.", 5))
        !            91:                        continue;
        !            92:                f = open(dp->d_name, O_RDONLY);
        !            93:                if (f < 0)
        !            94:                        continue;
        !            95:                cc = read(f, (char *)&wd, sizeof (struct whod));
        !            96:                if (cc < WHDRSIZE) {
        !            97:                        (void) close(f);
        !            98:                        continue;
        !            99:                }
        !           100:                if (down(w,now)) {
        !           101:                        (void) close(f);
        !           102:                        continue;
        !           103:                }
        !           104:                cc -= WHDRSIZE;
        !           105:                we = w->wd_we;
        !           106:                for (n = cc / sizeof (struct whoent); n > 0; n--) {
        !           107:                        if (aflg == 0 && we->we_idle >= 60*60) {
        !           108:                                we++;
        !           109:                                continue;
        !           110:                        }
        !           111:                        if (nusers >= NUSERS) {
        !           112:                                printf("too many users\n");
        !           113:                                exit(1);
        !           114:                        }
        !           115:                        mp->myutmp = we->we_utmp; mp->myidle = we->we_idle;
        !           116:                        (void) strcpy(mp->myhost, w->wd_hostname);
        !           117:                        nusers++; we++; mp++;
        !           118:                }
        !           119:                (void) close(f);
        !           120:        }
        !           121:        qsort((char *)myutmp, nusers, sizeof (struct myutmp), utmpcmp);
        !           122:        mp = myutmp;
        !           123:        width = 0;
        !           124:        for (i = 0; i < nusers; i++) {
        !           125:                int j = strlen(mp->myhost) + 1 + strlen(mp->myutmp.out_line);
        !           126:                if (j > width)
        !           127:                        width = j;
        !           128:                mp++;
        !           129:        }
        !           130:        mp = myutmp;
        !           131:        for (i = 0; i < nusers; i++) {
        !           132:                char buf[BUFSIZ];
        !           133:                (void)sprintf(buf, "%s:%s", mp->myhost, mp->myutmp.out_line);
        !           134:                printf("%-8.8s %-*s %.12s",
        !           135:                   mp->myutmp.out_name,
        !           136:                   width,
        !           137:                   buf,
        !           138:                   ctime((time_t *)&mp->myutmp.out_time)+4);
        !           139:                mp->myidle /= 60;
        !           140:                if (mp->myidle) {
        !           141:                        if (aflg) {
        !           142:                                if (mp->myidle >= 100*60)
        !           143:                                        mp->myidle = 100*60 - 1;
        !           144:                                if (mp->myidle >= 60)
        !           145:                                        printf(" %2d", mp->myidle / 60);
        !           146:                                else
        !           147:                                        printf("   ");
        !           148:                        } else
        !           149:                                printf(" ");
        !           150:                        printf(":%02d", mp->myidle % 60);
        !           151:                }
        !           152:                printf("\n");
        !           153:                mp++;
        !           154:        }
        !           155:        exit(0);
        !           156: }
        !           157: 
        !           158: utmpcmp(u1, u2)
        !           159:        struct myutmp *u1, *u2;
        !           160: {
        !           161:        int rc;
        !           162: 
        !           163:        rc = strncmp(u1->myutmp.out_name, u2->myutmp.out_name, 8);
        !           164:        if (rc)
        !           165:                return (rc);
        !           166:        rc = strncmp(u1->myhost, u2->myhost, 8);
        !           167:        if (rc)
        !           168:                return (rc);
        !           169:        return (strncmp(u1->myutmp.out_line, u2->myutmp.out_line, 8));
        !           170: }

unix.superglobalmegacorp.com

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