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

Annotation of src/usr.bin/lastcomm/lastcomm.c, Revision 1.11

1.11    ! millert     1: /*     $OpenBSD: lastcomm.c,v 1.10 2002/02/16 21:27:47 millert Exp $   */
1.2       deraadt     2: /*     $NetBSD: lastcomm.c,v 1.9 1995/10/22 01:43:42 ghudson Exp $     */
1.1       deraadt     3:
                      4: /*
                      5:  * Copyright (c) 1980, 1993
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
1.11    ! millert    16:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    17:  *    may be used to endorse or promote products derived from this software
                     18:  *    without specific prior written permission.
                     19:  *
                     20:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     21:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     22:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     23:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     24:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     25:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     26:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     27:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     28:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     29:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     30:  * SUCH DAMAGE.
                     31:  */
                     32:
                     33: #ifndef lint
                     34: static char copyright[] =
                     35: "@(#) Copyright (c) 1980, 1993\n\
                     36:        The Regents of the University of California.  All rights reserved.\n";
                     37: #endif /* not lint */
                     38:
                     39: #ifndef lint
                     40: #if 0
                     41: static char sccsid[] = "@(#)lastcomm.c 8.2 (Berkeley) 4/29/95";
                     42: #endif
1.11    ! millert    43: static char rcsid[] = "$OpenBSD: lastcomm.c,v 1.10 2002/02/16 21:27:47 millert Exp $";
1.1       deraadt    44: #endif /* not lint */
                     45:
                     46: #include <sys/param.h>
                     47: #include <sys/stat.h>
                     48: #include <sys/acct.h>
                     49:
                     50: #include <ctype.h>
                     51: #include <err.h>
                     52: #include <fcntl.h>
1.2       deraadt    53: #include <math.h>
1.1       deraadt    54: #include <stdio.h>
                     55: #include <stdlib.h>
                     56: #include <string.h>
                     57: #include <struct.h>
1.2       deraadt    58: #include <tzfile.h>
1.1       deraadt    59: #include <unistd.h>
                     60: #include <utmp.h>
                     61: #include "pathnames.h"
                     62:
1.10      millert    63: time_t  expand(u_int);
                     64: char   *flagbits(int);
                     65: char   *getdev(dev_t);
                     66: int     requested(char *[], struct acct *);
                     67: void    usage(void);
1.1       deraadt    68: char   *user_from_uid();
                     69:
                     70: int
                     71: main(argc, argv)
                     72:        int argc;
                     73:        char *argv[];
                     74: {
1.9       mpech      75:        char *p;
1.1       deraadt    76:        struct acct ab;
                     77:        struct stat sb;
                     78:        FILE *fp;
                     79:        off_t size;
                     80:        time_t t;
1.2       deraadt    81:        double delta;
1.1       deraadt    82:        int ch;
                     83:        char *acctfile;
                     84:
                     85:        acctfile = _PATH_ACCT;
1.4       millert    86:        while ((ch = getopt(argc, argv, "f:")) != -1)
1.1       deraadt    87:                switch((char)ch) {
                     88:                case 'f':
                     89:                        acctfile = optarg;
                     90:                        break;
                     91:                case '?':
                     92:                default:
                     93:                        usage();
                     94:                }
                     95:        argc -= optind;
                     96:        argv += optind;
                     97:
                     98:        /* Open the file. */
                     99:        if ((fp = fopen(acctfile, "r")) == NULL || fstat(fileno(fp), &sb))
                    100:                err(1, "%s", acctfile);
                    101:
                    102:        /*
                    103:         * Round off to integral number of accounting records, probably
                    104:         * not necessary, but it doesn't hurt.
                    105:         */
                    106:        size = sb.st_size - sb.st_size % sizeof(struct acct);
                    107:
                    108:        /* Check if any records to display. */
                    109:        if (size < sizeof(struct acct))
                    110:                exit(0);
                    111:
                    112:        /*
                    113:         * Seek to before the last entry in the file; use lseek(2) in case
                    114:         * the file is bigger than a "long".
                    115:         */
                    116:        size -= sizeof(struct acct);
                    117:        if (lseek(fileno(fp), size, SEEK_SET) == -1)
                    118:                err(1, "%s", acctfile);
                    119:
                    120:        for (;;) {
                    121:                if (fread(&ab, sizeof(struct acct), 1, fp) != 1)
                    122:                        err(1, "%s", acctfile);
                    123:
                    124:                if (ab.ac_comm[0] == '\0') {
                    125:                        ab.ac_comm[0] = '?';
                    126:                        ab.ac_comm[1] = '\0';
                    127:                } else
                    128:                        for (p = &ab.ac_comm[0];
                    129:                            p < &ab.ac_comm[fldsiz(acct, ac_comm)] && *p; ++p)
                    130:                                if (!isprint(*p))
                    131:                                        *p = '?';
1.6       flipk     132:                if (!*argv || requested(argv, &ab))
                    133:                {
                    134:                        t = expand(ab.ac_utime) + expand(ab.ac_stime);
                    135:                        (void)printf("%-*.*s %-7s %-*.*s %-*.*s %6.2f secs %.16s",
1.7       deraadt   136:                            (int)fldsiz(acct, ac_comm),
                    137:                            (int)fldsiz(acct, ac_comm),
                    138:                            ab.ac_comm, flagbits(ab.ac_flag), UT_NAMESIZE,
                    139:                            UT_NAMESIZE, user_from_uid(ab.ac_uid, 0),
                    140:                            UT_LINESIZE, UT_LINESIZE, getdev(ab.ac_tty),
                    141:                            t / (double)AHZ, ctime(&ab.ac_btime));
1.6       flipk     142:                        delta = expand(ab.ac_etime) / (double)AHZ;
1.8       pvalchev  143:                        printf(" (%1.0f:%02.0f:%05.2f)\n",
1.7       deraadt   144:                            delta / SECSPERHOUR,
                    145:                            fmod(delta, SECSPERHOUR) / SECSPERMIN,
                    146:                            fmod(delta, SECSPERMIN));
1.6       flipk     147:                }
1.5       flipk     148:
                    149:                if (size == 0)
                    150:                        break;
1.6       flipk     151:                /* seek to previous entry */
                    152:                if (fseek(fp, 2 * -(long)sizeof(struct acct), SEEK_CUR) == -1)
                    153:                        err(1, "%s", acctfile);
                    154:                size -= sizeof(struct acct);
1.1       deraadt   155:        }
                    156:        exit(0);
                    157: }
                    158:
                    159: time_t
                    160: expand(t)
                    161:        u_int t;
                    162: {
1.9       mpech     163:        time_t nt;
1.1       deraadt   164:
                    165:        nt = t & 017777;
                    166:        t >>= 13;
                    167:        while (t) {
                    168:                t--;
                    169:                nt <<= 3;
                    170:        }
                    171:        return (nt);
                    172: }
                    173:
                    174: char *
                    175: flagbits(f)
1.9       mpech     176:        int f;
1.1       deraadt   177: {
                    178:        static char flags[20] = "-";
                    179:        char *p;
                    180:
                    181: #define        BIT(flag, ch)   if (f & flag) *p++ = ch
                    182:
                    183:        p = flags + 1;
                    184:        BIT(ASU, 'S');
                    185:        BIT(AFORK, 'F');
                    186:        BIT(ACOMPAT, 'C');
                    187:        BIT(ACORE, 'D');
                    188:        BIT(AXSIG, 'X');
                    189:        *p = '\0';
                    190:        return (flags);
                    191: }
                    192:
                    193: int
                    194: requested(argv, acp)
1.9       mpech     195:        char *argv[];
                    196:        struct acct *acp;
1.1       deraadt   197: {
                    198:        do {
                    199:                if (!strcmp(user_from_uid(acp->ac_uid, 0), *argv))
                    200:                        return (1);
                    201:                if (!strcmp(getdev(acp->ac_tty), *argv))
                    202:                        return (1);
                    203:                if (!strncmp(acp->ac_comm, *argv, fldsiz(acct, ac_comm)))
                    204:                        return (1);
                    205:        } while (*++argv);
                    206:        return (0);
                    207: }
                    208:
                    209: char *
                    210: getdev(dev)
                    211:        dev_t dev;
                    212: {
                    213:        static dev_t lastdev = (dev_t)-1;
                    214:        static char *lastname;
                    215:
                    216:        if (dev == NODEV)                       /* Special case. */
                    217:                return ("__");
                    218:        if (dev == lastdev)                     /* One-element cache. */
                    219:                return (lastname);
                    220:        lastdev = dev;
                    221:        if ((lastname = devname(dev, S_IFCHR)) == NULL)
                    222:                lastname = "??";
                    223:        return (lastname);
                    224: }
                    225:
                    226: void
                    227: usage()
                    228: {
                    229:        (void)fprintf(stderr,
                    230:            "lastcomm [ -f file ] [command ...] [user ...] [tty ...]\n");
                    231:        exit(1);
                    232: }