54 lines
1.0 KiB
C
54 lines
1.0 KiB
C
#include <stdio.h>
|
|
#include <errno.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
#include <sys/wait.h>
|
|
#include <sys/times.h>
|
|
|
|
int main(int argc, char **argv) {
|
|
if (argc < 2) {
|
|
fprintf(stderr, "time: missing operand\n");
|
|
return 1;
|
|
}
|
|
|
|
/* Get time */
|
|
struct tms t1, t2;
|
|
long ticks = sysconf(_SC_CLK_TCK);
|
|
if (ticks <= 0) {
|
|
fprintf(stderr, "time: %s\n", strerror(errno));
|
|
return 1;
|
|
}
|
|
|
|
clock_t r1 = times(&t1);
|
|
if (r1 == (clock_t)-1) {
|
|
fprintf(stderr, "time: %s\n", strerror(errno));
|
|
return 1;
|
|
}
|
|
|
|
/* Run */
|
|
pid_t pid;
|
|
if ((pid = fork()) == 0) {
|
|
execvp(argv[1], argv + 1);
|
|
exit(1);
|
|
}
|
|
|
|
int status = 0;
|
|
waitpid(pid, &status, 0);
|
|
|
|
/* Get time */
|
|
clock_t r2 = times(&t2);
|
|
if (r2 == (clock_t)-1) {
|
|
fprintf(stderr, "time: %s\n", strerror(errno));
|
|
return 1;
|
|
}
|
|
|
|
|
|
/* Print */
|
|
fprintf(stderr, "real %.4f\nuser %.4f\nsys %.4f\n", (r2 - r1) / (double)ticks, t1.tms_cutime / (double)ticks, t2.tms_cstime / (double)ticks);
|
|
if (status != 0)
|
|
printf("Proccess returned %d\n", status);
|
|
|
|
return 0;
|
|
}
|