[BACK]Return to rwho.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / rwho

Annotation of src/usr.bin/rwho/rwho.c, Revision 1.10

1.10    ! deraadt     1: /*     $OpenBSD: rwho.c,v 1.9 1997/06/20 10:00:01 deraadt Exp $        */
1.2       deraadt     2:
1.1       deraadt     3: /*
                      4:  * Copyright (c) 1983 The Regents of the University of California.
                      5:  * All rights reserved.
                      6:  *
                      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. Redistributions in binary form must reproduce the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer in the
                     14:  *    documentation and/or other materials provided with the distribution.
                     15:  * 3. All advertising materials mentioning features or use of this software
                     16:  *    must display the following acknowledgement:
                     17:  *     This product includes software developed by the University of
                     18:  *     California, Berkeley and its contributors.
                     19:  * 4. Neither the name of the University nor the names of its contributors
                     20:  *    may be used to endorse or promote products derived from this software
                     21:  *    without specific prior written permission.
                     22:  *
                     23:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     24:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     25:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     26:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     27:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     28:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     29:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     30:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     31:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     32:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     33:  * SUCH DAMAGE.
                     34:  */
                     35:
                     36: #ifndef lint
                     37: char copyright[] =
                     38: "@(#) Copyright (c) 1983 The Regents of the University of California.\n\
                     39:  All rights reserved.\n";
                     40: #endif /* not lint */
                     41:
                     42: #ifndef lint
                     43: /*static char sccsid[] = "from: @(#)rwho.c     5.5 (Berkeley) 6/1/90";*/
1.10    ! deraadt    44: static char rcsid[] = "$OpenBSD: rwho.c,v 1.9 1997/06/20 10:00:01 deraadt Exp $";
1.1       deraadt    45: #endif /* not lint */
                     46:
                     47: #include <sys/param.h>
                     48: #include <sys/file.h>
                     49: #include <dirent.h>
                     50: #include <protocols/rwhod.h>
                     51: #include <stdio.h>
                     52: #include <string.h>
1.3       deraadt    53: #include <unistd.h>
                     54: #include <stdlib.h>
1.8       deraadt    55: #include <vis.h>
1.9       deraadt    56: #include <err.h>
1.1       deraadt    57:
                     58: DIR    *dirp;
                     59:
                     60: struct whod wd;
                     61: #define        NUSERS  1000
                     62: struct myutmp {
                     63:        char    myhost[MAXHOSTNAMELEN];
                     64:        int     myidle;
                     65:        struct  outmp myutmp;
                     66: } myutmp[NUSERS];
                     67: int    nusers;
                     68:
1.3       deraadt    69: int    utmpcmp __P((struct myutmp *, struct myutmp *));
                     70:
1.1       deraadt    71: #define        WHDRSIZE        (sizeof (wd) - sizeof (wd.wd_we))
                     72: /*
                     73:  * this macro should be shared with ruptime.
                     74:  */
                     75: #define        down(w,now)     ((now) - (w)->wd_recvtime > 11 * 60)
                     76:
1.3       deraadt    77: char   *ctime();
1.1       deraadt    78: time_t now;
                     79: int    aflg;
                     80:
1.10    ! deraadt    81: void
        !            82: usage()
        !            83: {
        !            84:        fprintf(stderr, "usage: rwho [-a]\n");
        !            85:        exit(1);
        !            86: }
        !            87:
1.3       deraadt    88: int
1.1       deraadt    89: main(argc, argv)
                     90:        int argc;
                     91:        char **argv;
                     92: {
                     93:        extern char *optarg;
                     94:        extern int optind;
                     95:        int ch;
                     96:        struct dirent *dp;
                     97:        int cc, width;
                     98:        register struct whod *w = &wd;
                     99:        register struct whoent *we;
                    100:        register struct myutmp *mp;
                    101:        int f, n, i;
                    102:        time_t time();
1.6       deraadt   103:        int nhosts = 0;
1.1       deraadt   104:
1.5       millert   105:        while ((ch = getopt(argc, argv, "a")) != -1)
1.1       deraadt   106:                switch((char)ch) {
                    107:                case 'a':
                    108:                        aflg = 1;
                    109:                        break;
                    110:                case '?':
                    111:                default:
1.10    ! deraadt   112:                        usage();
1.1       deraadt   113:                }
1.10    ! deraadt   114:        argc -= optind;
        !           115:        argv += optind;
        !           116:        if (argc != 0)
        !           117:                usage();
        !           118:
1.1       deraadt   119:        if (chdir(_PATH_RWHODIR) || (dirp = opendir(".")) == NULL) {
                    120:                perror(_PATH_RWHODIR);
                    121:                exit(1);
                    122:        }
                    123:        mp = myutmp;
                    124:        (void)time(&now);
1.3       deraadt   125:        while ((dp = readdir(dirp))) {
1.1       deraadt   126:                if (dp->d_ino == 0 || strncmp(dp->d_name, "whod.", 5))
                    127:                        continue;
                    128:                f = open(dp->d_name, O_RDONLY);
                    129:                if (f < 0)
                    130:                        continue;
                    131:                cc = read(f, (char *)&wd, sizeof (struct whod));
                    132:                if (cc < WHDRSIZE) {
                    133:                        (void) close(f);
                    134:                        continue;
                    135:                }
1.6       deraadt   136:                nhosts++;
1.1       deraadt   137:                if (down(w,now)) {
                    138:                        (void) close(f);
                    139:                        continue;
                    140:                }
                    141:                cc -= WHDRSIZE;
                    142:                we = w->wd_we;
                    143:                for (n = cc / sizeof (struct whoent); n > 0; n--) {
                    144:                        if (aflg == 0 && we->we_idle >= 60*60) {
                    145:                                we++;
                    146:                                continue;
                    147:                        }
                    148:                        if (nusers >= NUSERS) {
                    149:                                printf("too many users\n");
                    150:                                exit(1);
                    151:                        }
                    152:                        mp->myutmp = we->we_utmp; mp->myidle = we->we_idle;
1.3       deraadt   153:                        (void) strncpy(mp->myhost, w->wd_hostname,
                    154:                            sizeof(mp->myhost)-1);
                    155:                        mp->myhost[sizeof(mp->myhost)-1] = '\0';
1.1       deraadt   156:                        nusers++; we++; mp++;
                    157:                }
                    158:                (void) close(f);
                    159:        }
1.6       deraadt   160:        if (nhosts == 0)
                    161:                errx(0, "no hosts in %s.", _PATH_RWHODIR);
1.4       deraadt   162:        qsort((char *)myutmp, nusers, sizeof (struct myutmp),
                    163:            (int (*)())utmpcmp);
1.1       deraadt   164:        mp = myutmp;
                    165:        width = 0;
                    166:        for (i = 0; i < nusers; i++) {
                    167:                int j = strlen(mp->myhost) + 1 + strlen(mp->myutmp.out_line);
                    168:                if (j > width)
                    169:                        width = j;
                    170:                mp++;
                    171:        }
                    172:        mp = myutmp;
                    173:        for (i = 0; i < nusers; i++) {
1.8       deraadt   174:                char buf[BUFSIZ], vis_user[4*8];
1.7       deraadt   175:                (void)snprintf(buf, sizeof buf, "%s:%s", mp->myhost,
                    176:                    mp->myutmp.out_line);
1.8       deraadt   177:                strvis(vis_user, mp->myutmp.out_name, VIS_CSTYLE);
1.1       deraadt   178:                printf("%-8.8s %-*s %.12s",
1.8       deraadt   179:                   vis_user,
1.1       deraadt   180:                   width,
                    181:                   buf,
                    182:                   ctime((time_t *)&mp->myutmp.out_time)+4);
                    183:                mp->myidle /= 60;
                    184:                if (mp->myidle) {
                    185:                        if (aflg) {
                    186:                                if (mp->myidle >= 100*60)
                    187:                                        mp->myidle = 100*60 - 1;
                    188:                                if (mp->myidle >= 60)
                    189:                                        printf(" %2d", mp->myidle / 60);
                    190:                                else
                    191:                                        printf("   ");
                    192:                        } else
                    193:                                printf(" ");
                    194:                        printf(":%02d", mp->myidle % 60);
                    195:                }
                    196:                printf("\n");
                    197:                mp++;
                    198:        }
                    199:        exit(0);
                    200: }
                    201:
1.3       deraadt   202: int
1.1       deraadt   203: utmpcmp(u1, u2)
                    204:        struct myutmp *u1, *u2;
                    205: {
                    206:        int rc;
                    207:
                    208:        rc = strncmp(u1->myutmp.out_name, u2->myutmp.out_name, 8);
                    209:        if (rc)
                    210:                return (rc);
                    211:        rc = strncmp(u1->myhost, u2->myhost, 8);
                    212:        if (rc)
                    213:                return (rc);
                    214:        return (strncmp(u1->myutmp.out_line, u2->myutmp.out_line, 8));
                    215: }