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

Annotation of src/usr.bin/who/who.c, Revision 1.28

1.28    ! deraadt     1: /*     $OpenBSD: who.c,v 1.27 2015/10/21 16:06:57 millert Exp $        */
1.1       deraadt     2: /*     $NetBSD: who.c,v 1.4 1994/12/07 04:28:49 jtc Exp $      */
                      3:
                      4: /*
                      5:  * Copyright (c) 1989, 1993
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * This code is derived from software contributed to Berkeley by
                      9:  * Michael Fischbein.
                     10:  *
                     11:  * Redistribution and use in source and binary forms, with or without
                     12:  * modification, are permitted provided that the following conditions
                     13:  * are met:
                     14:  * 1. Redistributions of source code must retain the above copyright
                     15:  *    notice, this list of conditions and the following disclaimer.
                     16:  * 2. Redistributions in binary form must reproduce the above copyright
                     17:  *    notice, this list of conditions and the following disclaimer in the
                     18:  *    documentation and/or other materials provided with the distribution.
1.15      millert    19:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    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: #include <sys/types.h>
                     37: #include <sys/stat.h>
1.9       millert    38: #include <paths.h>
1.1       deraadt    39: #include <pwd.h>
                     40: #include <utmp.h>
                     41: #include <stdio.h>
                     42: #include <string.h>
                     43: #include <stdlib.h>
                     44: #include <unistd.h>
                     45: #include <time.h>
                     46: #include <err.h>
1.5       flipk      47: #include <locale.h>
                     48:
1.12      millert    49: void  output(struct utmp *);
                     50: void  output_labels(void);
                     51: void  who_am_i(FILE *);
                     52: void  usage(void);
                     53: FILE *file(char *);
1.1       deraadt    54:
                     55: int only_current_term;         /* show info about the current terminal only */
                     56: int show_term;                 /* show term state */
                     57: int show_idle;                 /* show idle time */
1.5       flipk      58: int show_labels;               /* show column labels */
1.6       denny      59: int show_quick;                        /* quick, names only */
1.1       deraadt    60:
1.10      deraadt    61: #define NAME_WIDTH     8
1.19      otto       62: #define HOST_WIDTH     45
                     63:
                     64: int hostwidth = HOST_WIDTH;
1.21      semarie    65: char *mytty;
1.10      deraadt    66:
1.1       deraadt    67: int
1.16      deraadt    68: main(int argc, char *argv[])
1.1       deraadt    69: {
                     70:        struct utmp usr;
1.5       flipk      71:        FILE *ufp;
1.21      semarie    72:        char *t;
1.1       deraadt    73:        int c;
                     74:
1.5       flipk      75:        setlocale(LC_ALL, "");
                     76:
1.28    ! deraadt    77:        if (pledge("stdio unveil rpath getpw", NULL) == -1)
1.23      deraadt    78:                err(1, "pledge");
1.21      semarie    79:
1.27      millert    80:        if ((mytty = ttyname(0))) {
1.25      deraadt    81:                /* strip any directory component */
                     82:                if ((t = strrchr(mytty, '/')))
                     83:                        mytty = t + 1;
                     84:        }
1.21      semarie    85:
1.5       flipk      86:        only_current_term = show_term = show_idle = show_labels = 0;
1.6       denny      87:        show_quick = 0;
                     88:        while ((c = getopt(argc, argv, "HmqTu")) != -1) {
1.1       deraadt    89:                switch (c) {
1.6       denny      90:                case 'H':
                     91:                        show_labels = 1;
                     92:                        break;
1.1       deraadt    93:                case 'm':
                     94:                        only_current_term = 1;
                     95:                        break;
1.6       denny      96:                case 'q':
                     97:                        show_quick = 1;
                     98:                        break;
1.1       deraadt    99:                case 'T':
                    100:                        show_term = 1;
                    101:                        break;
                    102:                case 'u':
                    103:                        show_idle = 1;
                    104:                        break;
                    105:                default:
                    106:                        usage();
                    107:                        /* NOTREACHED */
                    108:                }
                    109:        }
                    110:        argc -= optind;
                    111:        argv += optind;
                    112:
1.6       denny     113:        if (show_quick) {
                    114:                only_current_term = show_term = show_idle = show_labels = 0;
                    115:        }
1.19      otto      116:
                    117:        if (show_term)
                    118:                hostwidth -= 2;
                    119:        if (show_idle)
                    120:                hostwidth -= 6;
1.6       denny     121:
1.5       flipk     122:        if (show_labels)
                    123:                output_labels();
                    124:
1.28    ! deraadt   125:        if (unveil(_PATH_UTMP, "r") == -1)
        !           126:                err(1, "unveil");
1.1       deraadt   127:        switch (argc) {
                    128:        case 0:                                 /* who */
1.28    ! deraadt   129:                if (pledge("stdio rpath getpw", NULL) == -1)
        !           130:                        err(1, "pledge");
1.1       deraadt   131:                ufp = file(_PATH_UTMP);
                    132:
                    133:                if (only_current_term) {
                    134:                        who_am_i(ufp);
1.6       denny     135:                } else if (show_quick) {
                    136:                        int count = 0;
                    137:
                    138:                        while (fread((char *)&usr, sizeof(usr), 1, ufp) == 1) {
                    139:                                if (*usr.ut_name && *usr.ut_line) {
1.10      deraadt   140:                                        (void)printf("%-*.*s ", NAME_WIDTH,
1.6       denny     141:                                                UT_NAMESIZE, usr.ut_name);
                    142:                                        if ((++count % 8) == 0)
                    143:                                                (void) printf("\n");
                    144:                                }
                    145:                        }
                    146:                        if (count % 8)
                    147:                                (void) printf("\n");
                    148:                        (void) printf ("# users=%d\n", count);
1.1       deraadt   149:                } else {
                    150:                        /* only entries with both name and line fields */
                    151:                        while (fread((char *)&usr, sizeof(usr), 1, ufp) == 1)
                    152:                                if (*usr.ut_name && *usr.ut_line)
                    153:                                        output(&usr);
                    154:                }
                    155:                break;
                    156:        case 1:                                 /* who utmp_file */
1.28    ! deraadt   157:                if (unveil(*argv, "r") == -1)
        !           158:                        err(1, "unveil");
        !           159:                if (pledge("stdio rpath getpw", NULL) == -1)
        !           160:                        err(1, "pledge");
1.1       deraadt   161:                ufp = file(*argv);
                    162:
                    163:                if (only_current_term) {
                    164:                        who_am_i(ufp);
1.6       denny     165:                } else if (show_quick) {
                    166:                        int count = 0;
                    167:
                    168:                        while (fread((char *)&usr, sizeof(usr), 1, ufp) == 1) {
                    169:                                if (*usr.ut_name && *usr.ut_line) {
1.10      deraadt   170:                                        (void)printf("%-*.*s ", NAME_WIDTH,
1.6       denny     171:                                                UT_NAMESIZE, usr.ut_name);
                    172:                                        if ((++count % 8) == 0)
                    173:                                                (void) printf("\n");
                    174:                                }
                    175:                        }
                    176:                        if (count % 8)
                    177:                                (void) printf("\n");
                    178:                        (void) printf ("# users=%d\n", count);
1.1       deraadt   179:                } else {
                    180:                        /* all entries */
                    181:                        while (fread((char *)&usr, sizeof(usr), 1, ufp) == 1)
                    182:                                output(&usr);
                    183:                }
                    184:                break;
                    185:        case 2:                                 /* who am i */
1.28    ! deraadt   186:                if (pledge("stdio rpath getpw", NULL) == -1)
        !           187:                        err(1, "pledge");
1.1       deraadt   188:                ufp = file(_PATH_UTMP);
                    189:                who_am_i(ufp);
                    190:                break;
                    191:        default:
                    192:                usage();
                    193:                /* NOTREACHED */
                    194:        }
                    195:        exit(0);
                    196: }
                    197:
                    198: void
1.16      deraadt   199: who_am_i(FILE *ufp)
1.1       deraadt   200: {
                    201:        struct utmp usr;
                    202:        struct passwd *pw;
                    203:
                    204:        /* search through the utmp and find an entry for this tty */
1.21      semarie   205:        if (mytty) {
1.1       deraadt   206:                while (fread((char *)&usr, sizeof(usr), 1, ufp) == 1)
1.21      semarie   207:                        if (*usr.ut_name && !strcmp(usr.ut_line, mytty)) {
1.1       deraadt   208:                                output(&usr);
                    209:                                return;
                    210:                        }
                    211:                /* well, at least we know what the tty is */
1.21      semarie   212:                (void)strncpy(usr.ut_line, mytty, UT_LINESIZE);
1.1       deraadt   213:        } else
1.13      deraadt   214:                (void)strncpy(usr.ut_line, "tty??", UT_LINESIZE);
1.1       deraadt   215:
                    216:        pw = getpwuid(getuid());
                    217:        (void)strncpy(usr.ut_name, pw ? pw->pw_name : "?", UT_NAMESIZE);
                    218:        (void)time(&usr.ut_time);
                    219:        *usr.ut_host = '\0';
                    220:        output(&usr);
                    221: }
                    222:
                    223: void
1.16      deraadt   224: output(struct utmp *up)
1.1       deraadt   225: {
                    226:        struct stat sb;
1.9       millert   227:        char line[sizeof(_PATH_DEV) + sizeof (up->ut_line)];
1.5       flipk     228:        char state = '?';
1.1       deraadt   229:        static time_t now = 0;
1.5       flipk     230:        time_t idle = 0;
1.1       deraadt   231:
                    232:        if (show_term || show_idle) {
                    233:                if (now == 0)
                    234:                        time(&now);
                    235:
1.14      deraadt   236:                memset(line, 0, sizeof line);
                    237:                strlcpy(line, _PATH_DEV, sizeof line);
                    238:                strlcat(line, up->ut_line, sizeof line);
1.1       deraadt   239:
                    240:                if (stat(line, &sb) == 0) {
                    241:                        state = (sb.st_mode & 020) ? '+' : '-';
                    242:                        idle = now - sb.st_atime;
                    243:                } else {
                    244:                        state = '?';
                    245:                        idle = 0;
                    246:                }
                    247:
                    248:        }
                    249:
1.10      deraadt   250:        (void)printf("%-*.*s ", NAME_WIDTH, UT_NAMESIZE, up->ut_name);
1.1       deraadt   251:
                    252:        if (show_term) {
                    253:                (void)printf("%c ", state);
                    254:        }
                    255:
                    256:        (void)printf("%-*.*s ", UT_LINESIZE, UT_LINESIZE, up->ut_line);
                    257:        (void)printf("%.12s ", ctime(&up->ut_time) + 4);
                    258:
                    259:        if (show_idle) {
                    260:                if (idle < 60)
                    261:                        (void)printf("  .   ");
                    262:                else if (idle < (24 * 60 * 60))
                    263:                        (void)printf("%02d:%02d ",
1.20      guenther  264:                                     ((int)idle / (60 * 60)),
                    265:                                     ((int)idle % (60 * 60)) / 60);
1.1       deraadt   266:                else
                    267:                        (void)printf(" old  ");
                    268:        }
                    269:
                    270:        if (*up->ut_host)
1.19      otto      271:                printf("  (%.*s)", hostwidth, up->ut_host);
1.1       deraadt   272:        (void)putchar('\n');
                    273: }
                    274:
1.5       flipk     275: void
1.16      deraadt   276: output_labels(void)
1.5       flipk     277: {
1.10      deraadt   278:        (void)printf("%-*.*s ", NAME_WIDTH, UT_NAMESIZE, "USER");
1.5       flipk     279:
                    280:        if (show_term)
                    281:                (void)printf("S ");
                    282:
                    283:        (void)printf("%-*.*s ", UT_LINESIZE, UT_LINESIZE, "LINE");
                    284:        (void)printf("WHEN         ");
                    285:
                    286:        if (show_idle)
                    287:                (void)printf("IDLE  ");
                    288:
1.19      otto      289:        (void)printf("  %.*s", hostwidth, "FROM");
1.5       flipk     290:
                    291:        (void)putchar('\n');
                    292: }
                    293:
1.1       deraadt   294: FILE *
1.16      deraadt   295: file(char *name)
1.1       deraadt   296: {
                    297:        FILE *ufp;
                    298:
                    299:        if (!(ufp = fopen(name, "r"))) {
                    300:                err(1, "%s", name);
                    301:                /* NOTREACHED */
1.21      semarie   302:        }
                    303:        if (show_term || show_idle) {
1.24      deraadt   304:                if (pledge("stdio rpath getpw", NULL) == -1)
1.23      deraadt   305:                        err(1, "pledge");
1.21      semarie   306:        } else {
1.23      deraadt   307:                if (pledge("stdio getpw", NULL) == -1)
                    308:                        err(1, "pledge");
1.1       deraadt   309:        }
                    310:        return(ufp);
                    311: }
                    312:
                    313: void
1.16      deraadt   314: usage(void)
1.1       deraadt   315: {
1.17      jmc       316:        (void)fprintf(stderr, "usage: who [-HmqTu] [file]\n       who am i\n");
1.1       deraadt   317:        exit(1);
                    318: }