57 lines
1.2 KiB
C
57 lines
1.2 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) {
|
|
puts("time: missing operand\ntime [cmd]");
|
|
return 0;
|
|
}
|
|
|
|
long ticks = sysconf(_SC_CLK_TCK);
|
|
if (ticks <= 0) {
|
|
fprintf(stderr, "time: %s\n", strerror(errno));
|
|
return 1;
|
|
}
|
|
|
|
static struct tms tms;
|
|
clock_t r1 = times(&tms);
|
|
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(&tms);
|
|
if (r2 == (clock_t)-1) {
|
|
fprintf(stderr, "time: %s\n", strerror(errno));
|
|
return 1;
|
|
}
|
|
|
|
/* Print */
|
|
long real = (r2 - r1) / ticks;
|
|
long user = tms.tms_cutime / ticks;
|
|
long sys = tms.tms_cstime / ticks;
|
|
|
|
fprintf(stderr, "real:\t%ldm %ld.%lds\nsys:\t%ldm %ld.%lds\nuser:\t%ldm %ld.%lds\n", real / 60, real % 60, (r2 - r1) % 100, sys / 60, sys % 60, tms.tms_cstime % 100, user / 60, user % 60, tms.tms_cutime % 100);
|
|
if (status != 0)
|
|
printf("Proccess returned %d\n", status);
|
|
|
|
return 0;
|
|
}
|