Annotation of researchv10no/cmd/ac.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  * acct [ -w wtmp ] [ -d ] [ -p ] [ people ]
                      3:  */
                      4: static char *sccsid = "@(#)ac.c        4.4 (Berkeley) 2/13/82";
                      5: 
                      6: #include <stdio.h>
                      7: #include <ctype.h>
                      8: #include <time.h>
                      9: #include <utmp.h>
                     10: #include <sys/types.h>
                     11: #include <sys/timeb.h>
                     12: 
                     13: #define NMAX sizeof(ibuf.ut_name)
                     14: #define LMAX sizeof(ibuf.ut_line)
                     15: 
                     16: /*
                     17: #define        TSIZE   1000
                     18: */
                     19: #define TSIZE  6242
                     20: #define        USIZE   1500
                     21: struct  utmp ibuf;
                     22: 
                     23: struct ubuf {
                     24:        char    uname[NMAX];
                     25:        long    utime;
                     26: } ubuf[USIZE];
                     27: 
                     28: struct tbuf {
                     29:        struct  ubuf    *userp;
                     30:        long    ttime;
                     31: } tbuf[TSIZE];
                     32: 
                     33: char   *wtmp;
                     34: int    pflag, byday;
                     35: long   dtime;
                     36: long   midnight;
                     37: long   lastime;
                     38: long   day     = 86400L;
                     39: int    pcount;
                     40: char   **pptr;
                     41: 
                     42: main(argc, argv) 
                     43: char **argv;
                     44: {
                     45:        int c, fl;
                     46:        register i;
                     47:        FILE *wf;
                     48: 
                     49:        wtmp = "/usr/adm/wtmp";
                     50:        while (--argc > 0 && **++argv == '-')
                     51:        switch(*++*argv) {
                     52:        case 'd':
                     53:                byday++;
                     54:                continue;
                     55: 
                     56:        case 'w':
                     57:                if (--argc>0)
                     58:                        wtmp = *++argv;
                     59:                continue;
                     60: 
                     61:        case 'p':
                     62:                pflag++;
                     63:                continue;
                     64:        }
                     65:        pcount = argc;
                     66:        pptr = argv;
                     67:        if (strcmp("-", wtmp) == 0)
                     68:                wf = stdin;
                     69:        else
                     70:        if ((wf = fopen(wtmp, "r")) == NULL) {
                     71:                fprintf(stderr, "No %s\n", wtmp);
                     72:                exit(1);
                     73:        }
                     74:        for(;;) {
                     75:                if (fread((char *)&ibuf, sizeof(ibuf), 1, wf) != 1)
                     76:                        break;
                     77:                fl = 0;
                     78:                for (i=0; i<NMAX; i++) {
                     79:                        c = ibuf.ut_name[i];
                     80:                        if (c == '*')   /* result of rexec */
                     81:                                ibuf.ut_name[i] = c = '\0';
                     82:                        if (isprint(c) && c != ' ') {
                     83:                                if (fl)
                     84:                                        goto skip;
                     85:                                continue;
                     86:                        }
                     87:                        if (c==' ' || c=='\0') {
                     88:                                fl++;
                     89:                                ibuf.ut_name[i] = '\0';
                     90:                        } else
                     91:                                goto skip;
                     92:                }
                     93:                loop();
                     94:     skip:;
                     95:        }
                     96:        ibuf.ut_name[0] = '\0';
                     97:        ibuf.ut_line[0] = '~';
                     98:        time(&ibuf.ut_time);
                     99:        loop();
                    100:        print();
                    101:        exit(0);
                    102: }
                    103: 
                    104: loop()
                    105: {
                    106:        register i;
                    107:        register struct tbuf *tp;
                    108:        register struct ubuf *up;
                    109: 
                    110:        if(ibuf.ut_line[0] == '|') {
                    111:                dtime = ibuf.ut_time;
                    112:                return;
                    113:        }
                    114:        if(ibuf.ut_line[0] == '}') {
                    115:                if(dtime == 0)
                    116:                        return;
                    117:                for(tp = tbuf; tp < &tbuf[TSIZE]; tp++)
                    118:                        tp->ttime += ibuf.ut_time-dtime;
                    119:                dtime = 0;
                    120:                return;
                    121:        }
                    122:        if (lastime>ibuf.ut_time || lastime+(1.5*day)<ibuf.ut_time)
                    123:                midnight = 0;
                    124:        if (midnight==0)
                    125:                newday();
                    126:        lastime = ibuf.ut_time;
                    127:        if (byday && ibuf.ut_time > midnight) {
                    128:                upall(1);
                    129:                print();
                    130:                newday();
                    131:                for (up=ubuf; up < &ubuf[USIZE]; up++)
                    132:                        up->utime = 0;
                    133:        }
                    134:        if (ibuf.ut_line[0] == '~') {
                    135:                ibuf.ut_name[0] = '\0';
                    136:                upall(0);
                    137:                return;
                    138:        }
                    139:        /*
                    140:        if (ibuf.ut_line[0]=='t')
                    141:                i = (ibuf.ut_line[3]-'0')*10 + (ibuf.ut_line[4]-'0');
                    142:        else
                    143:                i = TSIZE-1;
                    144:        if (i<0 || i>=TSIZE)
                    145:                i = TSIZE-1;
                    146:        */
                    147: 
                    148:        /*
                    149:         * Correction contributed by Phyllis Kantar @ Rand-unix
                    150:         *
                    151:         * Fixes long standing problem with tty names other than 00-99
                    152:         */
                    153:        if (ibuf.ut_line[0]=='t') {
                    154:                i = (ibuf.ut_line[3]-'0');
                    155:                if(ibuf.ut_line[4])
                    156:                        i = i*79 + (ibuf.ut_line[4]-'0');
                    157:        } else
                    158:                i = TSIZE-1;
                    159:        if (i<0 || i>=TSIZE) {
                    160:                i = TSIZE-1;
                    161:                fprintf(stderr, "ac: Bad tty name: %s\n", ibuf.ut_line);
                    162:        }
                    163: 
                    164:        tp = &tbuf[i];
                    165:        update(tp, 0);
                    166: }
                    167: 
                    168: print()
                    169: {
                    170:        int i;
                    171:        long ttime, t;
                    172: 
                    173:        ttime = 0;
                    174:        for (i=0; i<USIZE; i++) {
                    175:                if(!among(i))
                    176:                        continue;
                    177:                t = ubuf[i].utime;
                    178:                if (t>0)
                    179:                        ttime += t;
                    180:                if (pflag && ubuf[i].utime > 0) {
                    181:                        printf("\t%-*.*s%6.2f\n", NMAX, NMAX,
                    182:                            ubuf[i].uname, ubuf[i].utime/3600.);
                    183:                }
                    184:        }
                    185:        if (ttime > 0) {
                    186:                pdate();
                    187:                printf("\ttotal%9.2f\n", ttime/3600.);
                    188:        }
                    189: }
                    190: 
                    191: upall(f)
                    192: {
                    193:        register struct tbuf *tp;
                    194: 
                    195:        for (tp=tbuf; tp < &tbuf[TSIZE]; tp++)
                    196:                update(tp, f);
                    197: }
                    198: 
                    199: update(tp, f)
                    200: struct tbuf *tp;
                    201: {
                    202:        int j;
                    203:        struct ubuf *up;
                    204:        long t, t1;
                    205: 
                    206:        if (f)
                    207:                t = midnight;
                    208:        else
                    209:                t = ibuf.ut_time;
                    210:        if (tp->userp) {
                    211:                t1 = t - tp->ttime;
                    212:                if (t1 > 0)
                    213:                        tp->userp->utime += t1;
                    214:        }
                    215:        tp->ttime = t;
                    216:        if (f)
                    217:                return;
                    218:        if (ibuf.ut_name[0]=='\0') {
                    219:                tp->userp = 0;
                    220:                return;
                    221:        }
                    222:        for (up=ubuf; up < &ubuf[USIZE]; up++) {
                    223:                if (up->uname[0] == '\0')
                    224:                        break;
                    225:                for (j=0; j<NMAX && up->uname[j]==ibuf.ut_name[j]; j++);
                    226:                if (j>=NMAX)
                    227:                        break;
                    228:        }
                    229: 
                    230:        if (up > &ubuf[USIZE-1]) {
                    231:                fprintf (stderr, "ac: too many users\n");
                    232:                exit (1);
                    233:        }
                    234:        for (j=0; j<NMAX; j++)
                    235:                up->uname[j] = ibuf.ut_name[j];
                    236:        tp->userp = up;
                    237: }
                    238: 
                    239: among(i)
                    240: {
                    241:        register j, k;
                    242:        register char *p;
                    243: 
                    244:        if (pcount==0)
                    245:                return(1);
                    246:        for (j=0; j<pcount; j++) {
                    247:                p = pptr[j];
                    248:                for (k=0; k<NMAX; k++) {
                    249:                        if (*p == ubuf[i].uname[k]) {
                    250:                                if (*p++ == '\0' || k == NMAX-1)
                    251:                                        return(1);
                    252:                        } else
                    253:                                break;
                    254:                }
                    255:        }
                    256:        return(0);
                    257: }
                    258: 
                    259: newday()
                    260: {
                    261:        long ttime;
                    262:        struct timeb tb;
                    263:        struct tm *localtime();
                    264: 
                    265:        time(&ttime);
                    266:        if (midnight == 0) {
                    267:                ftime(&tb);
                    268:                midnight = 60*(long)tb.timezone;
                    269:                if (localtime(&ttime)->tm_isdst)
                    270:                        midnight -= 3600;
                    271:        }
                    272:        while (midnight <= ibuf.ut_time)
                    273:                midnight += day;
                    274: }
                    275: 
                    276: pdate()
                    277: {
                    278:        long x;
                    279:        char *ctime();
                    280: 
                    281:        if (byday==0)
                    282:                return;
                    283:        x = midnight-1;
                    284:        printf("%.6s", ctime(&x)+4);
                    285: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.