annotate toys/posix/time.c @ 1746:b11f536bac74 draft

install -D bugfix from David Halls. (I tweaked some comment text while I was there.)
author Rob Landley <rob@landley.net>
date Sat, 21 Mar 2015 15:49:38 -0500
parents 0d9bad339158
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
795
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
1 /* time.c - time a simple command
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
2 *
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
3 * Copyright 2013 Rob Landley <rob@landley.net>
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
4 *
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
5 * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/time.html
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
6
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
7 USE_TIME(NEWTOY(time, "<1^p", TOYFLAG_USR|TOYFLAG_BIN))
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
8
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
9 config TIME
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
10 bool "time"
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
11 default y
1733
0d9bad339158 The time command depends on floating point support.
Rob Landley <rob@landley.net>
parents: 1676
diff changeset
12 depends on TOYBOX_FLOAT
795
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
13 help
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
14 usage: time [-p] COMMAND [ARGS...]
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
15
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
16 Run command line and report real, user, and system time elapsed in seconds.
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
17 (real = clock on the wall, user = cpu used by command's code,
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
18 system = cpu used by OS on behalf of command.)
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
19
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
20 -p posix mode (ignored)
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
21 */
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
22
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
23 #include "toys.h"
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
24
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
25 void time_main(void)
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
26 {
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
27 pid_t pid;
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
28 struct timeval tv, tv2;
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
29
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
30 gettimeofday(&tv, NULL);
1676
cbb1aca81eca Make toy_exec() check if argc is in optargs and deal with it there so we don't need a separate xexec_optargs().
Rob Landley <rob@landley.net>
parents: 1327
diff changeset
31 if (!(pid = xfork())) xexec(toys.optargs);
795
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
32 else {
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
33 int stat;
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
34 struct rusage ru;
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
35 float r, u, s;
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
36
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
37 wait4(pid, &stat, 0, &ru);
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
38 gettimeofday(&tv2, NULL);
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
39 if (tv.tv_usec > tv2.tv_usec) {
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
40 tv2.tv_usec += 1000000;
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
41 tv2.tv_sec--;
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
42 }
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
43 r = (tv2.tv_sec-tv.tv_sec)+((tv2.tv_usec-tv.tv_usec)/1000000.0);
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
44 u = ru.ru_utime.tv_sec+(ru.ru_utime.tv_usec/1000000.0);
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
45 s = ru.ru_stime.tv_sec+(ru.ru_stime.tv_usec/1000000.0);
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
46 fprintf(stderr, "real %f\nuser %f\nsys %f\n", r, u, s);
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
47 toys.exitval = WIFEXITED(stat) ? WEXITSTATUS(stat) : WTERMSIG(stat);
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
48 }
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
49 }