[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.8

1.8     ! deraadt     1: /*     $OpenBSD: rwho.c,v 1.7 1997/04/13 02:21:16 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.8     ! deraadt    44: static char rcsid[] = "$OpenBSD: rwho.c,v 1.7 1997/04/13 02:21:16 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.1       deraadt    56:
                     57: DIR    *dirp;
                     58:
                     59: struct whod wd;
                     60: #define        NUSERS  1000
                     61: struct myutmp {
                     62:        char    myhost[MAXHOSTNAMELEN];
                     63:        int     myidle;
                     64:        struct  outmp myutmp;
                     65: } myutmp[NUSERS];
                     66: int    nusers;
                     67:
1.3       deraadt    68: int    utmpcmp __P((struct myutmp *, struct myutmp *));
                     69:
1.1       deraadt    70: #define        WHDRSIZE        (sizeof (wd) - sizeof (wd.wd_we))
                     71: /*
                     72:  * this macro should be shared with ruptime.
                     73:  */
                     74: #define        down(w,now)     ((now) - (w)->wd_recvtime > 11 * 60)
                     75:
1.3       deraadt    76: char   *ctime();
1.1       deraadt    77: time_t now;
                     78: int    aflg;
                     79:
1.3       deraadt    80: int
1.1       deraadt    81: main(argc, argv)
                     82:        int argc;
                     83:        char **argv;
                     84: {
                     85:        extern char *optarg;
                     86:        extern int optind;
                     87:        int ch;
                     88:        struct dirent *dp;
                     89:        int cc, width;
                     90:        register struct whod *w = &wd;
                     91:        register struct whoent *we;
                     92:        register struct myutmp *mp;
                     93:        int f, n, i;
                     94:        time_t time();
1.6       deraadt    95:        int nhosts = 0;
1.1       deraadt    96:
1.5       millert    97:        while ((ch = getopt(argc, argv, "a")) != -1)
1.1       deraadt    98:                switch((char)ch) {
                     99:                case 'a':
                    100:                        aflg = 1;
                    101:                        break;
                    102:                case '?':
                    103:                default:
                    104:                        fprintf(stderr, "usage: rwho [-a]\n");
                    105:                        exit(1);
                    106:                }
                    107:        if (chdir(_PATH_RWHODIR) || (dirp = opendir(".")) == NULL) {
                    108:                perror(_PATH_RWHODIR);
                    109:                exit(1);
                    110:        }
                    111:        mp = myutmp;
                    112:        (void)time(&now);
1.3       deraadt   113:        while ((dp = readdir(dirp))) {
1.1       deraadt   114:                if (dp->d_ino == 0 || strncmp(dp->d_name, "whod.", 5))
                    115:                        continue;
                    116:                f = open(dp->d_name, O_RDONLY);
                    117:                if (f < 0)
                    118:                        continue;
                    119:                cc = read(f, (char *)&wd, sizeof (struct whod));
                    120:                if (cc < WHDRSIZE) {
                    121:                        (void) close(f);
                    122:                        continue;
                    123:                }
1.6       deraadt   124:                nhosts++;
1.1       deraadt   125:                if (down(w,now)) {
                    126:                        (void) close(f);
                    127:                        continue;
                    128:                }
                    129:                cc -= WHDRSIZE;
                    130:                we = w->wd_we;
                    131:                for (n = cc / sizeof (struct whoent); n > 0; n--) {
                    132:                        if (aflg == 0 && we->we_idle >= 60*60) {
                    133:                                we++;
                    134:                                continue;
                    135:                        }
                    136:                        if (nusers >= NUSERS) {
                    137:                                printf("too many users\n");
                    138:                                exit(1);
                    139:                        }
                    140:                        mp->myutmp = we->we_utmp; mp->myidle = we->we_idle;
1.3       deraadt   141:                        (void) strncpy(mp->myhost, w->wd_hostname,
                    142:                            sizeof(mp->myhost)-1);
                    143:                        mp->myhost[sizeof(mp->myhost)-1] = '\0';
1.1       deraadt   144:                        nusers++; we++; mp++;
                    145:                }
                    146:                (void) close(f);
                    147:        }
1.6       deraadt   148:        if (nhosts == 0)
                    149:                errx(0, "no hosts in %s.", _PATH_RWHODIR);
1.4       deraadt   150:        qsort((char *)myutmp, nusers, sizeof (struct myutmp),
                    151:            (int (*)())utmpcmp);
1.1       deraadt   152:        mp = myutmp;
                    153:        width = 0;
                    154:        for (i = 0; i < nusers; i++) {
                    155:                int j = strlen(mp->myhost) + 1 + strlen(mp->myutmp.out_line);
                    156:                if (j > width)
                    157:                        width = j;
                    158:                mp++;
                    159:        }
                    160:        mp = myutmp;
                    161:        for (i = 0; i < nusers; i++) {
1.8     ! deraadt   162:                char buf[BUFSIZ], vis_user[4*8];
1.7       deraadt   163:                (void)snprintf(buf, sizeof buf, "%s:%s", mp->myhost,
                    164:                    mp->myutmp.out_line);
1.8     ! deraadt   165:                strvis(vis_user, mp->myutmp.out_name, VIS_CSTYLE);
1.1       deraadt   166:                printf("%-8.8s %-*s %.12s",
1.8     ! deraadt   167:                   vis_user,
1.1       deraadt   168:                   width,
                    169:                   buf,
                    170:                   ctime((time_t *)&mp->myutmp.out_time)+4);
                    171:                mp->myidle /= 60;
                    172:                if (mp->myidle) {
                    173:                        if (aflg) {
                    174:                                if (mp->myidle >= 100*60)
                    175:                                        mp->myidle = 100*60 - 1;
                    176:                                if (mp->myidle >= 60)
                    177:                                        printf(" %2d", mp->myidle / 60);
                    178:                                else
                    179:                                        printf("   ");
                    180:                        } else
                    181:                                printf(" ");
                    182:                        printf(":%02d", mp->myidle % 60);
                    183:                }
                    184:                printf("\n");
                    185:                mp++;
                    186:        }
                    187:        exit(0);
                    188: }
                    189:
1.3       deraadt   190: int
1.1       deraadt   191: utmpcmp(u1, u2)
                    192:        struct myutmp *u1, *u2;
                    193: {
                    194:        int rc;
                    195:
                    196:        rc = strncmp(u1->myutmp.out_name, u2->myutmp.out_name, 8);
                    197:        if (rc)
                    198:                return (rc);
                    199:        rc = strncmp(u1->myhost, u2->myhost, 8);
                    200:        if (rc)
                    201:                return (rc);
                    202:        return (strncmp(u1->myutmp.out_line, u2->myutmp.out_line, 8));
                    203: }