← Commit list

c3657428d

Eric Andersen
Eric Andersen
2001-11-30
FAILED Duration: 498s

Subject

Add in a 'time' applet. Adds 4.5k. -Erik

Binary

No binary (build failed)

Architecture & Compiler Flags

Architecture: x86_64-linux-gnu Compiler: gcc (Ubuntu 11.4.0-1ubuntu1~22.04.3) 11.4.0

—

Build Log

$ git checkout -f c3657428d3207d35cda634adbe23f75457f7912b
HEAD is now at c3657428d Add in a 'time' applet.  Adds 4.5k.  -Erik

$ git clean -fdx
Removing .menuconfig.log
Removing include/config/
Removing scripts/lxdialog/checklist.o
Removing scripts/lxdialog/inputbox.o
Removing scripts/lxdialog/lxdialog
Removing scripts/lxdialog/lxdialog.o
Removing scripts/lxdialog/menubox.o
Removing scripts/lxdialog/msgbox.o
Removing scripts/lxdialog/textbox.o
Removing scripts/lxdialog/util.o
Removing scripts/lxdialog/yesno.o

$ make SHELL=/bin/bash CC=/root/asi_db/compat/cc-wrapper.sh HOSTCC=/root/asi_db/compat/cc-wrapper.sh defconfig
make: *** No rule to make target 'defconfig'.  Stop.

$ make SHELL=/bin/bash CC=/root/asi_db/compat/cc-wrapper.sh HOSTCC=/root/asi_db/compat/cc-wrapper.sh allnoconfig
make: *** No rule to make target 'allnoconfig'.  Stop.

$ make SHELL=/bin/bash CC=/root/asi_db/compat/cc-wrapper.sh HOSTCC=/root/asi_db/compat/cc-wrapper.sh include/bb_config.h
make: *** No rule to make target 'include/bb_config.h'.  Stop.

$ make SHELL=/bin/bash CC=/root/asi_db/compat/cc-wrapper.sh HOSTCC=/root/asi_db/compat/cc-wrapper.sh -j3 V=1
mkdir -p /root/asi_db/builds/busybox/_worktrees/w0/include/config
make -C scripts/lxdialog all
make[1]: Entering directory '/root/asi_db/builds/busybox/_worktrees/w0/scripts/lxdialog'
/root/asi_db/compat/cc-wrapper.sh -Wall -Wstrict-prototypes -O2 -fomit-frame-pointer -DLOCALE  -DCURSES_LOC="<ncurses.h>" -c -o checklist.o checklist.c
/root/asi_db/compat/cc-wrapper.sh -Wall -Wstrict-prototypes -O2 -fomit-frame-pointer -DLOCALE  -DCURSES_LOC="<ncurses.h>" -c -o menubox.o menubox.c
/root/asi_db/compat/cc-wrapper.sh -Wall -Wstrict-prototypes -O2 -fomit-frame-pointer -DLOCALE  -DCURSES_LOC="<ncurses.h>" -c -o textbox.o textbox.c
/root/asi_db/compat/cc-wrapper.sh -Wall -Wstrict-prototypes -O2 -fomit-frame-pointer -DLOCALE  -DCURSES_LOC="<ncurses.h>" -c -o yesno.o yesno.c
/root/asi_db/compat/cc-wrapper.sh -Wall -Wstrict-prototypes -O2 -fomit-frame-pointer -DLOCALE  -DCURSES_LOC="<ncurses.h>" -c -o inputbox.o inputbox.c
/root/asi_db/compat/cc-wrapper.sh -Wall -Wstrict-prototypes -O2 -fomit-frame-pointer -DLOCALE  -DCURSES_LOC="<ncurses.h>" -c -o util.o util.c
/root/asi_db/compat/cc-wrapper.sh -Wall -Wstrict-prototypes -O2 -fomit-frame-pointer -DLOCALE  -DCURSES_LOC="<ncurses.h>" -c -o lxdialog.o lxdialog.c
/root/asi_db/compat/cc-wrapper.sh -Wall -Wstrict-prototypes -O2 -fomit-frame-pointer -DLOCALE  -DCURSES_LOC="<ncurses.h>" -c -o msgbox.o msgbox.c
/root/asi_db/compat/cc-wrapper.sh -o lxdialog checklist.o menubox.o textbox.o yesno.o inputbox.o util.o lxdialog.o msgbox.o -lncurses
make[1]: Leaving directory '/root/asi_db/builds/busybox/_worktrees/w0/scripts/lxdialog'
/bin/bash scripts/Menuconfig sysdeps/linux/config.in
Using defaults found in sysdeps/linux/defconfig
Preparing scripts: functions, parsingAwk died with error code 137. Giving up.
.....done.


Your BusyBox configuration changes were NOT saved.

lxtemp.c:1:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
    1 | main() {}
      | ^~~~
textbox.c: In function ‘dialog_textbox’:
textbox.c:42:10: warning: variable ‘search_term’ set but not used [-Wunused-but-set-variable]
   42 |     char search_term[MAX_LEN + 1];
      |          ^~~~~~~~~~~
textbox.c: In function ‘print_line’:
textbox.c:462:12: warning: variable ‘x’ set but not used [-Wunused-but-set-variable]
  462 |     int y, x;
      |            ^
textbox.c:462:9: warning: variable ‘y’ set but not used [-Wunused-but-set-variable]
  462 |     int y, x;
      |         ^
inputbox.c: In function ‘dialog_inputbox’:
inputbox.c:107:17: warning: pointer targets in passing argument 1 of ‘strcpy’ differ in signedness [-Wpointer-sign]
  107 |         strcpy (instr, init);
      |                 ^~~~~
      |                 |
      |                 unsigned char *
In file included from /usr/include/features.h:486,
                 from /usr/include/x86_64-linux-gnu/sys/sysmacros.h:22,
                 from /root/asi_db/compat/compat_stime.h:9,
                 from <command-line>:
/usr/include/x86_64-linux-gnu/bits/string_fortified.h:77:1: note: expected ‘char * restrict’ but argument is of type ‘unsigned char *’
   77 | __NTH (strcpy (char *__restrict __dest, const char *__restrict __src))
      | ^~~~~
inputbox.c:109:23: warning: pointer targets in passing argument 1 of ‘strlen’ differ in signedness [-Wpointer-sign]
  109 |     input_x = strlen (instr);
      |                       ^~~~~
      |                       |
      |                       unsigned char *
In file included from dialog.h:27,
                 from inputbox.c:22:
/usr/include/string.h:407:35: note: expected ‘const char *’ but argument is of type ‘unsigned char *’
  407 | extern size_t strlen (const char *__s)
      |                       ~~~~~~~~~~~~^~~
In file included from dialog.h:29,
                 from inputbox.c:22:
inputbox.c:117:26: warning: pointer targets in passing argument 2 of ‘waddnstr’ differ in signedness [-Wpointer-sign]
  117 |         waddstr (dialog, instr);
      |                          ^~~~~
      |                          |
      |                          unsigned char *
/usr/include/curses.h:830:47: note: expected ‘const char *’ but argument is of type ‘unsigned char *’
  830 | extern NCURSES_EXPORT(int) waddnstr (WINDOW *,const char *,int);        /* implemented */
      |                                               ^~~~~~~~~~~~
inputbox.c:147:43: warning: pointer targets in passing argument 1 of ‘strlen’ differ in signedness [-Wpointer-sign]
  147 |                         input_x = strlen (instr) - scroll;
      |                                           ^~~~~
      |                                           |
      |                                           unsigned char *
In file included from dialog.h:27,
                 from inputbox.c:22:
/usr/include/string.h:407:35: note: expected ‘const char *’ but argument is of type ‘unsigned char *’
  407 | extern size_t strlen (const char *__s)
      |                       ~~~~~~~~~~~~^~~
lxdialog.c: In function ‘j_inputbox’:
lxdialog.c:211:25: warning: pointer targets in passing argument 2 of ‘fprintf’ differ in signedness [-Wpointer-sign]
  211 |         fprintf(stderr, dialog_input_result);
      |                         ^~~~~~~~~~~~~~~~~~~
      |                         |
      |                         unsigned char *
In file included from /usr/include/stdio.h:894,
                 from /usr/include/curses.h:232,
                 from dialog.h:29,
                 from lxdialog.c:22:
/usr/include/x86_64-linux-gnu/bits/stdio2.h:103:60: note: expected ‘const char * restrict’ but argument is of type ‘unsigned char *’
  103 | fprintf (FILE *__restrict __stream, const char *__restrict __fmt, ...)
      |                                     ~~~~~~~~~~~~~~~~~~~~~~~^~~~~
lxdialog.c:211:25: warning: format not a string literal and no format arguments [-Wformat-security]
  211 |         fprintf(stderr, dialog_input_result);
      |                         ^~~~~~~~~~~~~~~~~~~
scripts/Menuconfig: line 559: 1186096 Killed                  awk "$1"
Error opening terminal: unknown.

Commit Diff GitHub →

include/applets.h+3 -0
@@ -437,6 +437,9 @@
#ifdef CONFIG_TFTP
APPLET(tftp, tftp_main, _BB_DIR_USR_BIN)
#endif
+#ifdef BB_TIME
+ APPLET(time, time_main, _BB_DIR_USR_BIN)
+#endif
#ifdef CONFIG_TOUCH
APPLET(touch, touch_main, _BB_DIR_BIN)
#endif
include/usage.h+7 -0
@@ -1701,6 +1701,13 @@
USAGE_TFTP_BS( \
"\t-b SIZE\tTransfer blocks of SIZE octets.\n" \
)
+#define time_trivial_usage \
+ "[OPTION]... COMMAND [ARGS...]"
+#define time_full_usage \
+ "Runs the program COMMAND with arguments ARGS. When COMMAND finishes,\n"
+ "COMMAND's resource usage information is displayed\n\n"
+ "Options:\n" \
+ "\t-v\tDisplays verbose resource usage information."
#define touch_trivial_usage \
"[-c] FILE [FILE ...]"
miscutils/config.in+1 -0
@@ -13,6 +13,7 @@ bool 'makedevs' CONFIG_MAKEDEVS
bool 'mktemp' CONFIG_MKTEMP
bool 'mt' CONFIG_MT
bool 'readlink' CONFIG_READLINK
+bool 'time' CONFIG_TIME
bool 'update' CONFIG_UPDATE
bool 'watchdog' CONFIG_WATCHDOG
miscutils/time.c+502 -0
@@ -0,0 +1,502 @@
+/* `time' utility to display resource usage of processes.
+ Copyright (C) 1990, 91, 92, 93, 96 Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2, or (at your option)
+ any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+ 02111-1307, USA. */
+
+/* Originally written by David Keppel <[email protected]>.
+ Heavily modified by David MacKenzie <[email protected]>.
+ Heavily modified for busybox by Erik Andersen <[email protected]>
+ */
+
+#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/time.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>
+#include "busybox.h"
+
+/* Information on the resources used by a child process. */
+typedef struct
+{
+ int waitstatus;
+ struct rusage ru;
+ struct timeval start, elapsed; /* Wallclock time of process. */
+} resource_t;
+
+/* msec = milliseconds = 1/1,000 (1*10e-3) second.
+ usec = microseconds = 1/1,000,000 (1*10e-6) second. */
+
+#ifndef TICKS_PER_SEC
+#define TICKS_PER_SEC 100
+#endif
+
+/* The number of milliseconds in one `tick' used by the `rusage' structure. */
+#define MSEC_PER_TICK (1000 / TICKS_PER_SEC)
+
+/* Return the number of clock ticks that occur in M milliseconds. */
+#define MSEC_TO_TICKS(m) ((m) / MSEC_PER_TICK)
+
+#define UL unsigned long
+
+static const char *const default_format = "real\t%E\nuser\t%u\nsys\t%T";
+
+/* The output format for the -p option .*/
+static const char *const posix_format = "real %e\nuser %U\nsys %S";
+
+
+/* Format string for printing all statistics verbosely.
+ Keep this output to 24 lines so users on terminals can see it all.*/
+static const char *const long_format =
+ "\tCommand being timed: \"%C\"\n"
+ "\tUser time (seconds): %U\n"
+ "\tSystem time (seconds): %S\n"
+ "\tPercent of CPU this job got: %P\n"
+ "\tElapsed (wall clock) time (h:mm:ss or m:ss): %E\n"
+ "\tAverage shared text size (kbytes): %X\n"
+ "\tAverage unshared data size (kbytes): %D\n"
+ "\tAverage stack size (kbytes): %p\n"
+ "\tAverage total size (kbytes): %K\n"
+ "\tMaximum resident set size (kbytes): %M\n"
+ "\tAverage resident set size (kbytes): %t\n"
+ "\tMajor (requiring I/O) page faults: %F\n"
+ "\tMinor (reclaiming a frame) page faults: %R\n"
+ "\tVoluntary context switches: %w\n"
+ "\tInvoluntary context switches: %c\n"
+ "\tSwaps: %W\n"
+ "\tFile system inputs: %I\n"
+ "\tFile system outputs: %O\n"
+ "\tSocket messages sent: %s\n"
+ "\tSocket messages received: %r\n"
+ "\tSignals delivered: %k\n"
+ "\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. */
+
+/* pid_t is short on BSDI, so don't try to promote it. */
+static int resuse_end (pid_t pid, resource_t *resp)
+{
+ int status;
+
+ pid_t caught;
+
+ /* Ignore signals, but don't ignore the children. When wait3
+ returns the child process, set the time the command finished. */
+ while ((caught = wait3 (&status, 0, &resp->ru)) != pid)
+ {
+ if (caught == -1)
+ return 0;
+ }
+
+ gettimeofday (&resp->elapsed, (struct timezone *) 0);
+ resp->elapsed.tv_sec -= resp->start.tv_sec;
+ if (resp->elapsed.tv_usec < resp->start.tv_usec)
+ {
+ /* Manually carry a one from the seconds field. */
+ resp->elapsed.tv_usec += 1000000;
+ --resp->elapsed.tv_sec;
+ }
+ resp->elapsed.tv_usec -= resp->start.tv_usec;
+
+ resp->waitstatus = status;
+
+ 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)
+{
+ char *const *av;
+
+ av = argv;
+ fputs (*av, fp);
+ while (*++av)
+ {
+ fputs (filler, fp);
+ fputs (*av, fp);
+ }
+ if (ferror (fp))
+ error_msg_and_die("write error");
+}
+
+/* Return the number of kilobytes corresponding to a number of pages PAGES.
+ (Actually, we use it to convert pages*ticks into kilobytes*ticks.)
+
+ Try to do arithmetic so that the risk of overflow errors is minimized.
+ This is funky since the pagesize could be less than 1K.
+ Note: Some machines express getrusage statistics in terms of K,
+ others in terms of pages. */
+
+static unsigned long ptok (unsigned long pages)
+{
+ static unsigned long ps = 0;
+ unsigned long tmp;
+ static long size = LONG_MAX;
+
+ /* Initialization. */
+ if (ps == 0)
+ ps = (long) 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 size;
+}
+
+/* summarize: Report on the system use of a command.
+
+ Copy the FMT argument to FP except that `%' sequences
+ have special meaning, and `\n' and `\t' are translated into
+ newline and tab, respectively, and `\\' is translated into `\'.
+
+ The character following a `%' can be:
+ (* means the tcsh time builtin also recognizes it)
+ % == a literal `%'
+ C == command name and arguments
+* D == average unshared data size in K (ru_idrss+ru_isrss)
+* E == elapsed real (wall clock) time in [hour:]min:sec
+* F == major page faults (required physical I/O) (ru_majflt)
+* I == file system inputs (ru_inblock)
+* K == average total mem usage (ru_idrss+ru_isrss+ru_ixrss)
+* M == maximum resident set size in K (ru_maxrss)
+* O == file system outputs (ru_oublock)
+* P == percent of CPU this job got (total cpu time / elapsed time)
+* R == minor page faults (reclaims; no physical I/O involved) (ru_minflt)
+* S == system (kernel) time (seconds) (ru_stime)
+* T == system time in [hour:]min:sec
+* U == user time (seconds) (ru_utime)
+* u == user time in [hour:]min:sec
+* W == times swapped out (ru_nswap)
+* X == average amount of shared text in K (ru_ixrss)
+ Z == page size
+* c == involuntary context switches (ru_nivcsw)
+ e == elapsed real time in seconds
+* k == signals delivered (ru_nsignals)
+ p == average unshared stack size in K (ru_isrss)
+* r == socket messages received (ru_msgrcv)
+* s == socket messages sent (ru_msgsnd)
+ t == average resident set size in K (ru_idrss)
+* w == voluntary context switches (ru_nvcsw)
+ x == exit status of command
+
+ Various memory usages are found by converting from page-seconds
+ 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)
+{
+ 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", WSTOPSIG (resp->waitstatus));
+ else if (WIFSIGNALED (resp->waitstatus))
+ fprintf (fp, "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", WEXITSTATUS (resp->waitstatus));
+
+ /* Convert all times to milliseconds. Occasionally, one of these values
+ comes out as zero. Dividing by zero causes problems, so we first
+ check the time value. If it is zero, then we take `evasive action'
+ instead of calculating a value. */
+
+ r = resp->elapsed.tv_sec * 1000 + resp->elapsed.tv_usec / 1000;
+
+ 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;
+
+ while (*fmt)
+ {
+ switch (*fmt)
+ {
+ case '%':
+ switch (*++fmt)
+ {
+ case '%': /* Literal '%'. */
+ putc ('%', fp);
+ break;
+ case 'C': /* The command that got timed. */
+ fprintargv (fp, command, " ");
+ break;
+ case 'D': /* Average unshared data size. */
+ fprintf (fp, "%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",
+ resp->elapsed.tv_sec / 3600,
+ (resp->elapsed.tv_sec % 3600) / 60,
+ resp->elapsed.tv_sec % 60);
+ else
+ fprintf (fp, "%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);
+ break;
+ case 'I': /* Inputs. */
+ fprintf (fp, "%ld", resp->ru.ru_inblock);
+ break;
+ case 'K': /* Average mem usage == data+stack+text. */
+ fprintf (fp, "%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));
+ break;
+ case 'O': /* Outputs. */
+ fprintf (fp, "%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));
+ else
+ fprintf (fp, "?%%");
+ break;
+ case 'R': /* Minor page faults (reclaims). */
+ fprintf (fp, "%ld", resp->ru.ru_minflt);
+ break;
+ case 'S': /* System time. */
+ fprintf (fp, "%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",
+ 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. */
+ resp->ru.ru_stime.tv_sec / 60,
+ resp->ru.ru_stime.tv_sec % 60,
+ resp->ru.ru_stime.tv_sec / 10000);
+ break;
+ case 'U': /* User time. */
+ fprintf (fp, "%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",
+ 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. */
+ resp->ru.ru_utime.tv_sec / 60,
+ resp->ru.ru_utime.tv_sec % 60,
+ resp->ru.ru_utime.tv_sec / 10000);
+ break;
+ case 'W': /* Times swapped out. */
+ fprintf (fp, "%ld", resp->ru.ru_nswap);
+ break;
+ case 'X': /* Average shared text size. */
+ fprintf (fp, "%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 ());
+ break;
+ case 'c': /* Involuntary context switches. */
+ fprintf (fp, "%ld", resp->ru.ru_nivcsw);
+ break;
+ case 'e': /* Elapsed real time in seconds. */
+ fprintf (fp, "%ld.%02ld",
+ resp->elapsed.tv_sec,
+ resp->elapsed.tv_usec / 10000);
+ break;
+ case 'k': /* Signals delivered. */
+ fprintf (fp, "%ld", resp->ru.ru_nsignals);
+ break;
+ case 'p': /* Average stack segment. */
+ fprintf (fp, "%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);
+ break;
+ case 's': /* Outgoing socket messages sent. */
+ fprintf (fp, "%ld", resp->ru.ru_msgsnd);
+ break;
+ case 't': /* Average resident set size. */
+ fprintf (fp, "%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);
+ break;
+ case 'x': /* Exit status. */
+ fprintf (fp, "%d", WEXITSTATUS (resp->waitstatus));
+ break;
+ case '\0':
+ putc ('?', fp);
+ return;
+ default:
+ putc ('?', fp);
+ putc (*fmt, fp);
+ }
+ ++fmt;
+ break;
+
+ case '\\': /* Format escape. */
+ switch (*++fmt)
+ {
+ case 't':
+ putc ('\t', fp);
+ break;
+ case 'n':
+ putc ('\n', fp);
+ break;
+ case '\\':
+ putc ('\\', fp);
+ break;
+ default:
+ putc ('?', fp);
+ putc ('\\', fp);
+ putc (*fmt, fp);
+ }
+ ++fmt;
+ break;
+
+ default:
+ putc (*fmt++, fp);
+ }
+
+ if (ferror (fp))
+ error_msg_and_die("write error");
+ }
+ putc ('\n', fp);
+
+ if (ferror (fp))
+ error_msg_and_die("write error");
+}
+
+/* Run command CMD and return statistics on it.
+ Put the statistics in *RESP. */
+static void run_command (char *const *cmd, resource_t *resp)
+{
+ pid_t pid; /* Pid of child. */
+ __sighandler_t interrupt_signal, quit_signal;
+
+ gettimeofday (&resp->start, (struct timezone *) 0);
+ pid = fork (); /* Run CMD as child process. */
+ if (pid < 0)
+ 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);
+ error_msg("cannot run %s", cmd[0]);
+ _exit (errno == ENOENT ? 127 : 126);
+ }
+
+ /* Have signals kill the child but not self (if possible). */
+ interrupt_signal = signal (SIGINT, SIG_IGN);
+ quit_signal = signal (SIGQUIT, SIG_IGN);
+
+ if (resuse_end (pid, resp) == 0)
+ error_msg("error waiting for child process");
+
+ /* Re-enable signals. */
+ signal (SIGINT, interrupt_signal);
+ signal (SIGQUIT, quit_signal);
+}
+
+extern int time_main (int argc, char **argv)
+{
+ int gotone;
+ resource_t res;
+ const char *output_format = default_format;
+
+ argc--;
+ argv++;
+ /* 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) {
+ case 'v':
+ output_format = long_format;
+ break;
+ case 'p':
+ output_format = posix_format;
+ break;
+ default:
+ show_usage();
+ }
+ argc--;
+ argv++;
+ gotone = 1;
+ }
+ }
+
+ if (argv == NULL || *argv == NULL)
+ show_usage();
+
+ run_command (argv, &res);
+ summarize (stdout, output_format, argv, &res);
+ fflush (stdout);
+
+ 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;
+}