Annotation of researchv9/cmd/time.c, revision 1.1

1.1     ! root        1: /* time command */
        !             2: 
        !             3: #include <stdio.h>
        !             4: #include <signal.h>
        !             5: #include <sys/types.h>
        !             6: #include <sys/times.h>
        !             7: #include <sys/vtimes.h>
        !             8: 
        !             9: extern int errno;
        !            10: extern char *sys_errlist[];
        !            11: int    vflag = 0;
        !            12: char   output[BUFSIZ];
        !            13: 
        !            14: #define        U       0001
        !            15: #define        S       0002
        !            16: #define        R       0004
        !            17: #define        T       0010
        !            18: #define        D       0020
        !            19: #define        M       0040
        !            20: #define        F       0100
        !            21: #define        I       0200
        !            22: #define        O       0400
        !            23: #define        ALL     0777
        !            24: 
        !            25: #define        t(x)    (flags&x)
        !            26: 
        !            27: int    flags = U|S|R;
        !            28: char   names[] = "usrtdmfio";
        !            29: 
        !            30: main(argc, argv)
        !            31: char **argv;
        !            32: {
        !            33:        double x;
        !            34:        int status;
        !            35:        struct vtimes vt;
        !            36:        time_t before, after;
        !            37:        char buf[BUFSIZ];
        !            38:        register char *p;
        !            39:        register i, pid;
        !            40: 
        !            41:        setbuf(stderr, buf);
        !            42:        if(argc>1 && argv[1][0]=='-') {
        !            43:                flags = 0;
        !            44:                for(flags=0; argc>1 && argv[1][0]=='-'; --argc, argv++)
        !            45:                        for(p=argv[1]+1; *p; p++) {
        !            46:                                if(*p == 'v')
        !            47:                                        flags = ALL;
        !            48:                                else if((i=index(*p, names)) < 0)
        !            49:                                        fprintf(stderr, "time: bad flag %c\n", *p);
        !            50:                                else
        !            51:                                        flags |= 1<<i;
        !            52:                        }
        !            53:        }
        !            54:        if(argc <= 1) {
        !            55:                fprintf(stderr, "usage: time [-usrtdmfio] [-v] cmd\n");
        !            56:                exit(1);
        !            57:        }
        !            58:        time(&before);
        !            59:        pid = fork();
        !            60:        if(pid == -1) {
        !            61:                perror("Try again");
        !            62:                exit(1);
        !            63:        }
        !            64:        if(pid == 0) {
        !            65:                execvp(argv[1], &argv[1]);
        !            66:                fprintf(stderr, "%s: %s\n", argv[1], sys_errlist[errno]);
        !            67:                exit(1);
        !            68:        }
        !            69:        signal(SIGINT, SIG_IGN);
        !            70:        signal(SIGQUIT, SIG_IGN);
        !            71:        while(wait(&status) != pid)
        !            72:                ;
        !            73:        time(&after);
        !            74:        vtimes((struct vtimes *)0, &vt);
        !            75:        if((status&0377) != 0)
        !            76:                fprintf(stderr,"Command terminated abnormally.\n");
        !            77:        x = vt.vm_utime+vt.vm_stime;
        !            78:        if(t(U))
        !            79:                add("%.1fu", (double)vt.vm_utime/60);
        !            80:        if(t(S))
        !            81:                add("%.1fs", (double)vt.vm_stime/60);
        !            82:        if(t(R))
        !            83:                add("%dr", after-before);
        !            84:        if(t(T))
        !            85:                add("%.0ft", vt.vm_ixrss/2/x);
        !            86:        if(t(D))
        !            87:                add("%.0fd", vt.vm_idsrss/2/x);
        !            88:        if(t(M))
        !            89:                add("%dm", vt.vm_maxrss/2);
        !            90:        if(t(F))
        !            91:                add("%df", vt.vm_majflt);
        !            92:        if(t(I))
        !            93:                add("%di", vt.vm_inblk);
        !            94:        if(t(O))
        !            95:                add("%do", vt.vm_oublk);
        !            96:        add("\t");
        !            97:        for(i=1; i<argc; i++) {
        !            98:                add("%s", argv[i]);
        !            99:                if(i > 4) {
        !           100:                        add("...");
        !           101:                        break;
        !           102:                }
        !           103:        }
        !           104:        fprintf(stderr, "%s\n", output);
        !           105:        return (status&0377)? -1 : (status>>8)&0377;
        !           106: }
        !           107: add(a, b, c, d, e)
        !           108:        char *a;
        !           109:        int b, c, d, e;
        !           110: {
        !           111:        static beenhere=0;
        !           112:        if(beenhere)
        !           113:                strcat(output, " ");
        !           114:        sprintf(output+strlen(output), a, b, c, d, e);
        !           115:        beenhere++;
        !           116: }
        !           117: index(c, s)
        !           118:        register char *s;
        !           119: {
        !           120:        register i;
        !           121:        for(i=0; *s; i++, s++)
        !           122:                if(*s == c)
        !           123:                        return i;
        !           124:        return -1;
        !           125: }

unix.superglobalmegacorp.com

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