Annotation of 3BSD/cmd/uptime.c, revision 1.1

1.1     ! root        1: #include <stdio.h>
        !             2: #include <sys/types.h>
        !             3: #include <a.out.h>
        !             4: #include <time.h>
        !             5: 
        !             6: struct tm *localtime();
        !             7: 
        !             8: #define        DIV60(t)        ((t+30)/60)    /* x/60 rounded */ 
        !             9: 
        !            10: struct nlist   nl[] = {
        !            11: #define X_BOOTIME      0
        !            12:        { "_bootime" },
        !            13: #define X_AVENRUN      1
        !            14:        { "_avenrun" },
        !            15:        { 0 }
        !            16: };
        !            17: 
        !            18: time_t now, bootime;
        !            19: double avenrun[3];
        !            20: 
        !            21: int    kmem;
        !            22: 
        !            23: main(argc, argv)
        !            24:        char **argv;
        !            25: {
        !            26:        char obuf[BUFSIZ];
        !            27:        register int i, days;
        !            28:        register time_t uptime;
        !            29:        register struct tm *nowt;
        !            30: 
        !            31:        setbuf(stdout, obuf);
        !            32:        nlist("/vmunix" , nl);
        !            33:        if (nl[0].n_value == 0) {
        !            34:                fprintf(stderr, "No namelist\n");
        !            35:                exit(1);
        !            36:        }
        !            37:        kmem = open("/dev/kmem", 0);
        !            38:        if (kmem < 0) {
        !            39:                fprintf(stderr ,"No kmem\n");
        !            40:                exit(1);
        !            41:        }
        !            42:        time(&now);
        !            43:        nowt = localtime(&now);
        !            44:        prtat(nowt);
        !            45:        lseek(kmem, (long)nl[X_BOOTIME].n_value, 0);
        !            46:        read(kmem, &bootime, sizeof (bootime));
        !            47:        uptime = now - bootime;
        !            48:        printf("  up");
        !            49:        days = uptime / (60*60*24);
        !            50:        if (days > 0) {
        !            51:                printf(" %d day%s, ", days, days>1?"s":"");
        !            52:                uptime %= (60*60*24);
        !            53:        }
        !            54:        prttime(DIV60(uptime), "");
        !            55:        printf("\t\t");
        !            56:        printf("load average:");
        !            57:        lseek(kmem, (long)nl[X_AVENRUN].n_value, 0);
        !            58:        read(kmem, avenrun, sizeof(avenrun));
        !            59:        for (i = 0; i < 3; i++) {
        !            60:                printf(" %.2f", avenrun[i]);
        !            61:                if (i < 2)
        !            62:                        printf(",");
        !            63:        }
        !            64:        printf("\n");
        !            65: }
        !            66: 
        !            67: prttime(tim, tail)
        !            68:        time_t tim;
        !            69:        char *tail;
        !            70: {
        !            71:        register int didhrs = 0;
        !            72: 
        !            73:        if (tim >= 60) {
        !            74:                printf("%3d:", tim/60);
        !            75:                didhrs++;
        !            76:        } else {
        !            77:                printf("    ");
        !            78:        }
        !            79:        tim %= 60;
        !            80:        if (tim > 0 || didhrs) {
        !            81:                printf(didhrs&&tim<10 ? "%02d" : "%2d", tim);
        !            82:        } else {
        !            83:                printf("  ");
        !            84:        }
        !            85:        printf("%s", tail);
        !            86: }
        !            87: 
        !            88: /* prtat prints a 12 hour time given a pointer to a time of day */
        !            89: prtat(p)
        !            90:        struct tm *p;
        !            91: {
        !            92:        register int t, pm;
        !            93: 
        !            94:        t = p -> tm_hour;
        !            95:        pm = (t > 11);
        !            96:        if (t > 11)
        !            97:                t -= 12;
        !            98:        if (t == 0)
        !            99:                t = 12;
        !           100:        prttime(t*60 + p->tm_min, pm ? "pm" : "am");
        !           101: }

unix.superglobalmegacorp.com

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