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

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