- add libbb function str_tolower to convert a string to lowercase.
[oweals/busybox.git] / miscutils / time.c
index e0bdfe13c4196908852d1beb906eab3c4714fcee..a4591950250ffc3a81633aaadcf44e48ecf7e8a3 100644 (file)
@@ -2,7 +2,7 @@
 /* `time' utility to display resource usage of processes.
    Copyright (C) 1990, 91, 92, 93, 96 Free Software Foundation, Inc.
 
-   Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
+   Licensed under GPL version 2, see file LICENSE in this tarball for details.
 */
 /* Originally written by David Keppel <pardo@cs.washington.edu>.
    Heavily modified by David MacKenzie <djm@gnu.ai.mit.edu>.
 */
 
 #include "busybox.h"
-#include <stdlib.h>
-#include <stdio.h>
-#include <signal.h>
-#include <errno.h>
-#include <getopt.h>
-#include <string.h>
-#include <limits.h>
-#include <unistd.h>
-#include <sys/types.h> /* For pid_t. */
-#include <sys/wait.h>
-#include <sys/param.h> /* For getpagesize, maybe.  */
 
 #define TV_MSEC tv_usec / 1000
-#include <sys/resource.h>
 
 /* Information on the resources used by a child process.  */
 typedef struct {
@@ -77,11 +65,12 @@ static const char *const long_format =
        "\tSocket messages sent: %s\n"
        "\tSocket messages received: %r\n"
        "\tSignals delivered: %k\n"
-       "\tPage size (bytes): %Z\n" "\tExit status: %x";
+       "\tPage size (bytes): %Z\n"
+       "\tExit status: %x";
 
 
-  /* Wait for and fill in data on child process PID.
-     Return 0 on error, 1 if ok.  */
+/* Wait for and fill in data on child process PID.
+   Return 0 on error, 1 if ok.  */
 
 /* pid_t is short on BSDI, so don't try to promote it.  */
 static int resuse_end(pid_t pid, resource_t * resp)
@@ -111,19 +100,14 @@ static int resuse_end(pid_t pid, resource_t * resp)
        return 1;
 }
 
-/* Print ARGV to FP, with each entry in ARGV separated by FILLER.  */
-static void fprintargv(FILE * fp, char *const *argv, const char *filler)
+/* Print ARGV, with each entry in ARGV separated by FILLER.  */
+static void printargv(char *const *argv, const char *filler)
 {
-       char *const *av;
-
-       av = argv;
-       fputs(*av, fp);
-       while (*++av) {
-               fputs(filler, fp);
-               fputs(*av, fp);
+       fputs(*argv, stdout);
+       while (*++argv) {
+               fputs(filler, stdout);
+               fputs(*argv, stdout);
        }
-       if (ferror(fp))
-               bb_error_msg_and_die(bb_msg_write_error);
 }
 
 /* Return the number of kilobytes corresponding to a number of pages PAGES.
@@ -136,28 +120,26 @@ static void fprintargv(FILE * fp, char *const *argv, const char *filler)
 
 static unsigned long ptok(unsigned long pages)
 {
-       static unsigned long ps = 0;
+       static unsigned long ps;
        unsigned long tmp;
-       static long size = LONG_MAX;
 
        /* Initialization.  */
        if (ps == 0)
-               ps = (long) getpagesize();
+               ps = getpagesize();
 
        /* Conversion.  */
        if (pages > (LONG_MAX / ps)) {  /* Could overflow.  */
                tmp = pages / 1024;     /* Smaller first, */
-               size = tmp * ps;        /* then larger.  */
-       } else {                        /* Could underflow.  */
-               tmp = pages * ps;       /* Larger first, */
-               size = tmp / 1024;      /* then smaller.  */
+               return tmp * ps;        /* then larger.  */
        }
-       return size;
+       /* Could underflow.  */
+       tmp = pages * ps;       /* Larger first, */
+       return tmp / 1024;      /* then smaller.  */
 }
 
 /* summarize: Report on the system use of a command.
 
-   Copy the FMT argument to FP except that `%' sequences
+   Print the FMT argument except that `%' sequences
    have special meaning, and `\n' and `\t' are translated into
    newline and tab, respectively, and `\\' is translated into `\'.
 
@@ -195,25 +177,23 @@ static unsigned long ptok(unsigned long pages)
    to kbytes by multiplying by the page size, dividing by 1024,
    and dividing by elapsed real time.
 
-   FP is the stream to print to.
    FMT is the format string, interpreted as described above.
    COMMAND is the command and args that are being summarized.
    RESP is resource information on the command.  */
 
-static void summarize(FILE * fp, const char *fmt, char **command,
-                                         resource_t * resp)
+static void summarize(const char *fmt, char **command, resource_t * resp)
 {
        unsigned long r;        /* Elapsed real milliseconds.  */
        unsigned long v;        /* Elapsed virtual (CPU) milliseconds.  */
 
        if (WIFSTOPPED(resp->waitstatus))
-               fprintf(fp, "Command stopped by signal %d\n",
+               printf("Command stopped by signal %d\n",
                                WSTOPSIG(resp->waitstatus));
        else if (WIFSIGNALED(resp->waitstatus))
-               fprintf(fp, "Command terminated by signal %d\n",
+               printf("Command terminated by signal %d\n",
                                WTERMSIG(resp->waitstatus));
        else if (WIFEXITED(resp->waitstatus) && WEXITSTATUS(resp->waitstatus))
-               fprintf(fp, "Command exited with non-zero status %d\n",
+               printf("Command exited with non-zero status %d\n",
                                WEXITSTATUS(resp->waitstatus));
 
        /* Convert all times to milliseconds.  Occasionally, one of these values
@@ -226,180 +206,194 @@ static void summarize(FILE * fp, const char *fmt, char **command,
        v = resp->ru.ru_utime.tv_sec * 1000 + resp->ru.ru_utime.TV_MSEC +
                resp->ru.ru_stime.tv_sec * 1000 + resp->ru.ru_stime.TV_MSEC;
 
+       /* putchar() != putc(stdout) in glibc! */
+
        while (*fmt) {
+               /* Handle leading literal part */
+               int n = strcspn(fmt, "%\\");
+               if (n) {
+                       printf("%.*s", n, fmt);
+                       fmt += n;
+                       continue;
+               }
+
                switch (*fmt) {
+#ifdef NOT_NEEDED
+               /* Handle literal char */
+               /* Usually we optimize for size, but there is a limit
+                * for everything. With this we do a lot of 1-byte writes */
+               default:
+                       putc(*fmt, stdout);
+                       break;
+#endif
+
                case '%':
                        switch (*++fmt) {
-                       case '%':       /* Literal '%'.  */
-                               putc('%', fp);
+#ifdef NOT_NEEDED_YET
+               /* Our format strings do not have these */
+               /* and we do not take format str from user */
+                       default:
+                               putc('%', stdout);
+                               /*FALLTHROUGH*/
+                       case '%':
+                               if (!*fmt) goto ret;
+                               putc(*fmt, stdout);
                                break;
+#endif
                        case 'C':       /* The command that got timed.  */
-                               fprintargv(fp, command, " ");
+                               printargv(command, " ");
                                break;
                        case 'D':       /* Average unshared data size.  */
-                               fprintf(fp, "%lu",
+                               printf("%lu",
                                                MSEC_TO_TICKS(v) == 0 ? 0 :
                                                ptok((UL) resp->ru.ru_idrss) / MSEC_TO_TICKS(v) +
                                                ptok((UL) resp->ru.ru_isrss) / MSEC_TO_TICKS(v));
                                break;
                        case 'E':       /* Elapsed real (wall clock) time.  */
                                if (resp->elapsed.tv_sec >= 3600)       /* One hour -> h:m:s.  */
-                                       fprintf(fp, "%ldh %ldm %02lds",
+                                       printf("%ldh %ldm %02lds",
                                                        resp->elapsed.tv_sec / 3600,
                                                        (resp->elapsed.tv_sec % 3600) / 60,
                                                        resp->elapsed.tv_sec % 60);
                                else
-                                       fprintf(fp, "%ldm %ld.%02lds",  /* -> m:s.  */
+                                       printf("%ldm %ld.%02lds",       /* -> m:s.  */
                                                        resp->elapsed.tv_sec / 60,
                                                        resp->elapsed.tv_sec % 60,
                                                        resp->elapsed.tv_usec / 10000);
                                break;
                        case 'F':       /* Major page faults.  */
-                               fprintf(fp, "%ld", resp->ru.ru_majflt);
+                               printf("%ld", resp->ru.ru_majflt);
                                break;
                        case 'I':       /* Inputs.  */
-                               fprintf(fp, "%ld", resp->ru.ru_inblock);
+                               printf("%ld", resp->ru.ru_inblock);
                                break;
                        case 'K':       /* Average mem usage == data+stack+text.  */
-                               fprintf(fp, "%lu",
+                               printf("%lu",
                                                MSEC_TO_TICKS(v) == 0 ? 0 :
                                                ptok((UL) resp->ru.ru_idrss) / MSEC_TO_TICKS(v) +
                                                ptok((UL) resp->ru.ru_isrss) / MSEC_TO_TICKS(v) +
                                                ptok((UL) resp->ru.ru_ixrss) / MSEC_TO_TICKS(v));
                                break;
                        case 'M':       /* Maximum resident set size.  */
-                               fprintf(fp, "%lu", ptok((UL) resp->ru.ru_maxrss));
+                               printf("%lu", ptok((UL) resp->ru.ru_maxrss));
                                break;
                        case 'O':       /* Outputs.  */
-                               fprintf(fp, "%ld", resp->ru.ru_oublock);
+                               printf("%ld", resp->ru.ru_oublock);
                                break;
                        case 'P':       /* Percent of CPU this job got.  */
                                /* % cpu is (total cpu time)/(elapsed time).  */
                                if (r > 0)
-                                       fprintf(fp, "%lu%%", (v * 100 / r));
+                                       printf("%lu%%", (v * 100 / r));
                                else
-                                       fprintf(fp, "?%%");
+                                       printf("?%%");
                                break;
                        case 'R':       /* Minor page faults (reclaims).  */
-                               fprintf(fp, "%ld", resp->ru.ru_minflt);
+                               printf("%ld", resp->ru.ru_minflt);
                                break;
                        case 'S':       /* System time.  */
-                               fprintf(fp, "%ld.%02ld",
+                               printf("%ld.%02ld",
                                                resp->ru.ru_stime.tv_sec,
                                                resp->ru.ru_stime.TV_MSEC / 10);
                                break;
                        case 'T':       /* System time.  */
                                if (resp->ru.ru_stime.tv_sec >= 3600) /* One hour -> h:m:s.  */
-                                       fprintf(fp, "%ldh %ldm %02lds",
+                                       printf("%ldh %ldm %02lds",
                                                        resp->ru.ru_stime.tv_sec / 3600,
                                                        (resp->ru.ru_stime.tv_sec % 3600) / 60,
                                                        resp->ru.ru_stime.tv_sec % 60);
                                else
-                                       fprintf(fp, "%ldm %ld.%02lds",  /* -> m:s.  */
+                                       printf("%ldm %ld.%02lds",       /* -> m:s.  */
                                                        resp->ru.ru_stime.tv_sec / 60,
                                                        resp->ru.ru_stime.tv_sec % 60,
                                                        resp->ru.ru_stime.tv_usec / 10000);
                                break;
                        case 'U':       /* User time.  */
-                               fprintf(fp, "%ld.%02ld",
+                               printf("%ld.%02ld",
                                                resp->ru.ru_utime.tv_sec,
                                                resp->ru.ru_utime.TV_MSEC / 10);
                                break;
                        case 'u':       /* User time.  */
                                if (resp->ru.ru_utime.tv_sec >= 3600) /* One hour -> h:m:s.  */
-                                       fprintf(fp, "%ldh %ldm %02lds",
+                                       printf("%ldh %ldm %02lds",
                                                        resp->ru.ru_utime.tv_sec / 3600,
                                                        (resp->ru.ru_utime.tv_sec % 3600) / 60,
                                                        resp->ru.ru_utime.tv_sec % 60);
                                else
-                                       fprintf(fp, "%ldm %ld.%02lds",  /* -> m:s.  */
+                                       printf("%ldm %ld.%02lds",       /* -> m:s.  */
                                                        resp->ru.ru_utime.tv_sec / 60,
                                                        resp->ru.ru_utime.tv_sec % 60,
                                                        resp->ru.ru_utime.tv_usec / 10000);
                                break;
                        case 'W':       /* Times swapped out.  */
-                               fprintf(fp, "%ld", resp->ru.ru_nswap);
+                               printf("%ld", resp->ru.ru_nswap);
                                break;
                        case 'X':       /* Average shared text size.  */
-                               fprintf(fp, "%lu",
+                               printf("%lu",
                                                MSEC_TO_TICKS(v) == 0 ? 0 :
                                                ptok((UL) resp->ru.ru_ixrss) / MSEC_TO_TICKS(v));
                                break;
                        case 'Z':       /* Page size.  */
-                               fprintf(fp, "%d", getpagesize());
+                               printf("%d", getpagesize());
                                break;
                        case 'c':       /* Involuntary context switches.  */
-                               fprintf(fp, "%ld", resp->ru.ru_nivcsw);
+                               printf("%ld", resp->ru.ru_nivcsw);
                                break;
                        case 'e':       /* Elapsed real time in seconds.  */
-                               fprintf(fp, "%ld.%02ld",
+                               printf("%ld.%02ld",
                                                resp->elapsed.tv_sec, resp->elapsed.tv_usec / 10000);
                                break;
                        case 'k':       /* Signals delivered.  */
-                               fprintf(fp, "%ld", resp->ru.ru_nsignals);
+                               printf("%ld", resp->ru.ru_nsignals);
                                break;
                        case 'p':       /* Average stack segment.  */
-                               fprintf(fp, "%lu",
+                               printf("%lu",
                                                MSEC_TO_TICKS(v) == 0 ? 0 :
                                                ptok((UL) resp->ru.ru_isrss) / MSEC_TO_TICKS(v));
                                break;
                        case 'r':       /* Incoming socket messages received.  */
-                               fprintf(fp, "%ld", resp->ru.ru_msgrcv);
+                               printf("%ld", resp->ru.ru_msgrcv);
                                break;
                        case 's':       /* Outgoing socket messages sent.  */
-                               fprintf(fp, "%ld", resp->ru.ru_msgsnd);
+                               printf("%ld", resp->ru.ru_msgsnd);
                                break;
                        case 't':       /* Average resident set size.  */
-                               fprintf(fp, "%lu",
+                               printf("%lu",
                                                MSEC_TO_TICKS(v) == 0 ? 0 :
                                                ptok((UL) resp->ru.ru_idrss) / MSEC_TO_TICKS(v));
                                break;
                        case 'w':       /* Voluntary context switches.  */
-                               fprintf(fp, "%ld", resp->ru.ru_nvcsw);
+                               printf("%ld", resp->ru.ru_nvcsw);
                                break;
                        case 'x':       /* Exit status.  */
-                               fprintf(fp, "%d", WEXITSTATUS(resp->waitstatus));
+                               printf("%d", WEXITSTATUS(resp->waitstatus));
                                break;
-                       case '\0':
-                               putc('?', fp);
-                               return;
-                       default:
-                               putc('?', fp);
-                               putc(*fmt, fp);
                        }
-                       ++fmt;
                        break;
 
+#ifdef NOT_NEEDED_YET
                case '\\':              /* Format escape.  */
                        switch (*++fmt) {
+                       default:
+                               putc('\\', stdout);
+                               /*FALLTHROUGH*/
+                       case '\\':
+                               if (!*fmt) goto ret;
+                               putc(*fmt, stdout);
+                               break;
                        case 't':
-                               putc('\t', fp);
+                               putc('\t', stdout);
                                break;
                        case 'n':
-                               putc('\n', fp);
-                               break;
-                       case '\\':
-                               putc('\\', fp);
+                               putc('\n', stdout);
                                break;
-                       default:
-                               putc('?', fp);
-                               putc('\\', fp);
-                               putc(*fmt, fp);
                        }
-                       ++fmt;
                        break;
-
-               default:
-                       putc(*fmt++, fp);
+#endif
                }
-
-               if (ferror(fp))
-                       bb_error_msg_and_die(bb_msg_write_error);
+               ++fmt;
        }
-       putc('\n', fp);
-
-       if (ferror(fp))
-               bb_error_msg_and_die(bb_msg_write_error);
+ /* ret: */
+       putc('\n', stdout);
 }
 
 /* Run command CMD and return statistics on it.
@@ -410,13 +404,13 @@ static void run_command(char *const *cmd, resource_t * resp)
        __sighandler_t interrupt_signal, quit_signal;
 
        gettimeofday(&resp->start, (struct timezone *) 0);
-       pid = fork();           /* Run CMD as child process.  */
+       pid = vfork();          /* Run CMD as child process.  */
        if (pid < 0)
                bb_error_msg_and_die("cannot fork");
        else if (pid == 0) {    /* If child.  */
                /* Don't cast execvp arguments; that causes errors on some systems,
                   versus merely warnings if the cast is left off.  */
-               execvp(cmd[0], cmd);
+               BB_EXECVP(cmd[0], cmd);
                bb_error_msg("cannot run %s", cmd[0]);
                _exit(errno == ENOENT ? 127 : 126);
        }
@@ -433,20 +427,19 @@ static void run_command(char *const *cmd, resource_t * resp)
        signal(SIGQUIT, quit_signal);
 }
 
+int time_main(int argc, char **argv);
 int time_main(int argc, char **argv)
 {
-       int gotone;
        resource_t res;
        const char *output_format = default_format;
+       char c;
 
-       argc--;
-       argv++;
+       goto next;
        /* Parse any options  -- don't use getopt() here so we don't
         * consume the args of our client application... */
-       while (argc > 0 && **argv == '-') {
-               gotone = 0;
-               while (gotone == 0 && *++(*argv)) {
-                       switch (**argv) {
+       while (argc > 0 && argv[0][0] == '-') {
+               while ((c = *++*argv)) {
+                       switch (c) {
                        case 'v':
                                output_format = long_format;
                                break;
@@ -456,24 +449,26 @@ int time_main(int argc, char **argv)
                        default:
                                bb_show_usage();
                        }
-                       argc--;
-                       argv++;
-                       gotone = 1;
                }
+ next:
+               argv++;
+               argc--;
+               if (!argc)
+                       bb_show_usage();
        }
 
-       if (argv == NULL || *argv == NULL)
-               bb_show_usage();
-
        run_command(argv, &res);
-       summarize(stderr, output_format, argv, &res);
-       fflush(stderr);
+
+       /* Cheat. printf's are shorter :) */
+       stdout = stderr;
+       dup2(2, 1); /* just in case libc does something silly :( */
+       summarize(output_format, argv, &res);
 
        if (WIFSTOPPED(res.waitstatus))
-               exit(WSTOPSIG(res.waitstatus));
-       else if (WIFSIGNALED(res.waitstatus))
-               exit(WTERMSIG(res.waitstatus));
-       else if (WIFEXITED(res.waitstatus))
-               exit(WEXITSTATUS(res.waitstatus));
-       return 0;
+               return WSTOPSIG(res.waitstatus);
+       if (WIFSIGNALED(res.waitstatus))
+               return WTERMSIG(res.waitstatus);
+       if (WIFEXITED(res.waitstatus))
+               return WEXITSTATUS(res.waitstatus);
+       fflush_stdout_and_exit(0);
 }