Annotation of 43BSDReno/usr.bin/time/time.c, revision 1.1

1.1     ! root        1: /*
        !             2:  * Copyright (c) 1987, 1988 The Regents of the University of California.
        !             3:  * All rights reserved.
        !             4:  *
        !             5:  * Redistribution and use in source and binary forms are permitted
        !             6:  * provided that: (1) source distributions retain this entire copyright
        !             7:  * notice and comment, and (2) distributions including binaries display
        !             8:  * the following acknowledgement:  ``This product includes software
        !             9:  * developed by the University of California, Berkeley and its contributors''
        !            10:  * in the documentation or other materials provided with the distribution
        !            11:  * and in all advertising materials mentioning features or use of this
        !            12:  * software. Neither the name of the University nor the names of its
        !            13:  * contributors may be used to endorse or promote products derived
        !            14:  * from this software without specific prior written permission.
        !            15:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
        !            16:  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
        !            17:  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
        !            18:  */
        !            19: 
        !            20: #ifndef lint
        !            21: char copyright[] =
        !            22: "@(#) Copyright (c) 1987, 1988 The Regents of the University of California.\n\
        !            23:  All rights reserved.\n";
        !            24: #endif /* not lint */
        !            25: 
        !            26: #ifndef lint
        !            27: static char sccsid[] = "@(#)time.c     4.9 (Berkeley) 6/1/90";
        !            28: #endif /* not lint */
        !            29: 
        !            30: #include <sys/types.h>
        !            31: #include <sys/time.h>
        !            32: #include <sys/resource.h>
        !            33: #include <sys/signal.h>
        !            34: #include <stdio.h>
        !            35: 
        !            36: main(argc, argv)
        !            37:        int argc;
        !            38:        char **argv;
        !            39: {
        !            40:        extern int optind;
        !            41:        register int pid;
        !            42:        int ch, status, lflag;
        !            43:        struct timeval before, after;
        !            44:        struct rusage ru;
        !            45: 
        !            46:        lflag = 0;
        !            47:        while ((ch = getopt(argc, argv, "l")) != EOF)
        !            48:                switch((char)ch) {
        !            49:                case 'l':
        !            50:                        lflag = 1;
        !            51:                        break;
        !            52:                case '?':
        !            53:                default:
        !            54:                        fprintf(stderr, "usage: time [-l] command.\n");
        !            55:                        exit(1);
        !            56:                }
        !            57: 
        !            58:        if (!(argc -= optind))
        !            59:                exit(0);
        !            60:        argv += optind;
        !            61: 
        !            62:        gettimeofday(&before, (struct timezone *)NULL);
        !            63:        switch(pid = vfork()) {
        !            64:        case -1:                        /* error */
        !            65:                perror("time");
        !            66:                exit(1);
        !            67:                /* NOTREACHED */
        !            68:        case 0:                         /* child */
        !            69:                execvp(*argv, argv);
        !            70:                perror(*argv);
        !            71:                _exit(1);
        !            72:                /* NOTREACHED */
        !            73:        }
        !            74:        /* parent */
        !            75:        (void)signal(SIGINT, SIG_IGN);
        !            76:        (void)signal(SIGQUIT, SIG_IGN);
        !            77:        while (wait3(&status, 0, &ru) != pid);          /* XXX use waitpid */
        !            78:        gettimeofday(&after, (struct timezone *)NULL);
        !            79:        if (status&0377)
        !            80:                fprintf(stderr, "Command terminated abnormally.\n");
        !            81:        after.tv_sec -= before.tv_sec;
        !            82:        after.tv_usec -= before.tv_usec;
        !            83:        if (after.tv_usec < 0)
        !            84:                after.tv_sec--, after.tv_usec += 1000000;
        !            85:        fprintf(stderr, "%9ld.%02ld real ", after.tv_sec, after.tv_usec/10000);
        !            86:        fprintf(stderr, "%9ld.%02ld user ",
        !            87:            ru.ru_utime.tv_sec, ru.ru_utime.tv_usec/10000);
        !            88:        fprintf(stderr, "%9ld.%02ld sys\n",
        !            89:            ru.ru_stime.tv_sec, ru.ru_stime.tv_usec/10000);
        !            90:        if (lflag) {
        !            91:                int hz = 100;                   /* XXX */
        !            92:                long ticks;
        !            93: 
        !            94:                ticks = hz * (ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) +
        !            95:                     hz * (ru.ru_utime.tv_usec + ru.ru_stime.tv_usec) / 1000000;
        !            96:                fprintf(stderr, "%10ld  %s\n",
        !            97:                        ru.ru_maxrss, "maximum resident set size");
        !            98:                fprintf(stderr, "%10ld  %s\n",
        !            99:                        ru.ru_ixrss / ticks, "average shared memory size");
        !           100:                fprintf(stderr, "%10ld  %s\n",
        !           101:                        ru.ru_idrss / ticks, "average unshared data size");
        !           102:                fprintf(stderr, "%10ld  %s\n",
        !           103:                        ru.ru_isrss / ticks, "average unshared stack size");
        !           104:                fprintf(stderr, "%10ld  %s\n",
        !           105:                        ru.ru_minflt, "page reclaims");
        !           106:                fprintf(stderr, "%10ld  %s\n",
        !           107:                        ru.ru_majflt, "page faults");
        !           108:                fprintf(stderr, "%10ld  %s\n",
        !           109:                        ru.ru_nswap, "swaps");
        !           110:                fprintf(stderr, "%10ld  %s\n",
        !           111:                        ru.ru_inblock, "block input operations");
        !           112:                fprintf(stderr, "%10ld  %s\n",
        !           113:                        ru.ru_oublock, "block output operations");
        !           114:                fprintf(stderr, "%10ld  %s\n",
        !           115:                        ru.ru_msgsnd, "messages sent");
        !           116:                fprintf(stderr, "%10ld  %s\n",
        !           117:                        ru.ru_msgrcv, "messages received");
        !           118:                fprintf(stderr, "%10ld  %s\n",
        !           119:                        ru.ru_nsignals, "signals received");
        !           120:                fprintf(stderr, "%10ld  %s\n",
        !           121:                        ru.ru_nvcsw, "voluntary context switches");
        !           122:                fprintf(stderr, "%10ld  %s\n",
        !           123:                        ru.ru_nivcsw, "involuntary context switches");
        !           124:        }
        !           125:        exit (status>>8);
        !           126: }

unix.superglobalmegacorp.com

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