annotate toys/posix/time.c @ 948:55e587acefa9

add grep
author Strake
date Fri, 12 Jul 2013 18:10:52 -0500
parents 8e3b60814ad7
children 144d5ba7d410
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
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
12 help
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
13 usage: time [-p] COMMAND [ARGS...]
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
14
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
15 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
16 (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
17 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
18
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
19 -p posix mode (ignored)
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
20 */
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 #include "toys.h"
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
23
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
24 void time_main(void)
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
25 {
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
26 pid_t pid;
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
27 struct timeval tv, tv2;
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
28
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
29 gettimeofday(&tv, NULL);
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
30 if (!(pid = fork())) xexec(toys.optargs);
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
31 else {
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
32 int stat;
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
33 struct rusage ru;
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
34 float r, u, s;
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
35
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
36 wait4(pid, &stat, 0, &ru);
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
37 gettimeofday(&tv2, NULL);
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
38 if (tv.tv_usec > tv2.tv_usec) {
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
39 tv2.tv_usec += 1000000;
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
40 tv2.tv_sec--;
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
41 }
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
42 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
43 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
44 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
45 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
46 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
47 }
8e3b60814ad7 Add time command (that only does posix mode).
Rob Landley <rob@landley.net>
parents:
diff changeset
48 }