2015-02-27 21:43:11 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2015-02-27 21:31:04 +00:00
|
|
|
#include <sys/times.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
2015-02-27 21:43:11 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2015-02-27 21:31:04 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
2015-02-27 21:43:11 +00:00
|
|
|
static void
|
2015-02-27 21:31:04 +00:00
|
|
|
usage(void)
|
|
|
|
{
|
2015-03-04 23:23:09 +00:00
|
|
|
eprintf("usage: %s [-p] cmd [arg ...]\n", argv0);
|
2015-02-27 21:31:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
pid_t pid;
|
2015-03-04 23:23:09 +00:00
|
|
|
struct tms tms; /* user and sys times */
|
|
|
|
clock_t r0, r1; /* real time */
|
|
|
|
long ticks; /* per second */
|
2015-03-08 11:49:24 +00:00
|
|
|
int status, savederrno, ret = 0;
|
2015-02-27 21:31:04 +00:00
|
|
|
|
|
|
|
ARGBEGIN {
|
|
|
|
case 'p':
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
2015-03-04 23:23:09 +00:00
|
|
|
if (!argc)
|
2015-02-27 21:31:04 +00:00
|
|
|
usage();
|
|
|
|
|
2015-02-28 11:16:53 +00:00
|
|
|
if ((ticks = sysconf(_SC_CLK_TCK)) <= 0)
|
2015-03-04 23:23:09 +00:00
|
|
|
eprintf("sysconf _SC_CLK_TCK:");
|
2015-02-27 21:31:04 +00:00
|
|
|
|
2015-03-04 23:23:09 +00:00
|
|
|
if ((r0 = times(&tms)) < 0)
|
|
|
|
eprintf("times:");
|
2015-02-27 21:31:04 +00:00
|
|
|
|
2015-03-04 23:23:09 +00:00
|
|
|
switch ((pid = fork())) {
|
|
|
|
case -1:
|
|
|
|
eprintf("fork:");
|
|
|
|
case 0:
|
|
|
|
execvp(argv[0], argv);
|
|
|
|
savederrno = errno;
|
|
|
|
weprintf("exec %s:", argv[0]);
|
|
|
|
_exit(126 + (savederrno == ENOENT));
|
|
|
|
default:
|
|
|
|
break;
|
2015-02-27 21:31:04 +00:00
|
|
|
}
|
|
|
|
waitpid(pid, &status, 0);
|
|
|
|
|
2015-03-04 23:23:09 +00:00
|
|
|
if ((r1 = times(&tms)) < 0)
|
|
|
|
eprintf("times:");
|
2015-02-27 21:31:04 +00:00
|
|
|
|
2015-03-08 11:49:24 +00:00
|
|
|
if (WIFSIGNALED(status)) {
|
|
|
|
fprintf(stderr, "Command terminated by signal %d\n",
|
|
|
|
WTERMSIG(status));
|
|
|
|
ret = 128 + WTERMSIG(status);
|
|
|
|
}
|
|
|
|
|
2015-02-27 21:31:04 +00:00
|
|
|
fprintf(stderr, "real %f\nuser %f\nsys %f\n",
|
2015-03-04 23:23:09 +00:00
|
|
|
(r1 - r0) / (double)ticks,
|
2015-02-27 21:31:04 +00:00
|
|
|
tms.tms_cutime / (double)ticks,
|
|
|
|
tms.tms_cstime / (double)ticks);
|
|
|
|
|
2015-03-08 11:49:24 +00:00
|
|
|
if (WIFEXITED(status))
|
|
|
|
ret = WEXITSTATUS(status);
|
|
|
|
|
|
|
|
return ret;
|
2015-02-27 21:31:04 +00:00
|
|
|
}
|