Add -p option to time(1).

This commit is contained in:
Jonas 'Sortie' Termansen 2022-12-03 23:22:33 +01:00
parent cbf16b4891
commit c3a3ff86ec
1 changed files with 40 additions and 14 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2015 Jonas 'Sortie' Termansen.
* Copyright (c) 2013, 2015, 2022 Jonas 'Sortie' Termansen.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
@ -20,8 +20,8 @@
#include <sys/types.h>
#include <sys/wait.h>
#include <err.h>
#include <errno.h>
#include <error.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
@ -30,23 +30,39 @@
#include <timespec.h>
#include <unistd.h>
static bool p;
static pid_t child_pid;
static struct timespec start_time, end_time;
void statistics(void)
{
clock_gettime(CLOCK_MONOTONIC, &end_time);
struct tmns tmns;
timens(&tmns);
struct tmns ts;
timens(&ts);
struct timespec real_time = timespec_sub(end_time, start_time);
struct timespec execute_time = timespec_sub(tmns.tmns_cutime, tmns.tmns_utime);
struct timespec system_time = timespec_sub(tmns.tmns_cstime, tmns.tmns_stime);
struct timespec execute_time =timespec_sub(ts.tmns_cutime, ts.tmns_utime);
struct timespec system_time = timespec_sub(ts.tmns_cstime, ts.tmns_stime);
struct timespec user_time = timespec_sub(execute_time, system_time);
fprintf(stderr, "\n");
fprintf(stderr, "real\t%lim%li.%03li\n", (long) real_time.tv_sec / 60, (long) real_time.tv_sec % 60, real_time.tv_nsec / 1000000L);
fprintf(stderr, "user\t%lim%li.%03li\n", (long) user_time.tv_sec / 60, (long) user_time.tv_sec % 60, user_time.tv_nsec / 1000000);
fprintf(stderr, "sys\t%lim%li.%03li\n", (long) system_time.tv_sec / 60, (long) system_time.tv_sec % 60, system_time.tv_nsec / 1000000);
if ( p )
{
fprintf(stderr, "real %li.%03li\n",
(long) real_time.tv_sec, real_time.tv_nsec / 1000000L);
fprintf(stderr, "user %li.%03li\n",
(long) user_time.tv_sec, user_time.tv_nsec / 1000000);
fprintf(stderr, "sys %li.%03li\n",
(long) system_time.tv_sec, system_time.tv_nsec / 1000000);
}
else
{
fprintf(stderr, "\n");
fprintf(stderr, "real\t%lim%li.%03li\n", (long) real_time.tv_sec / 60,
(long) real_time.tv_sec % 60, real_time.tv_nsec / 1000000L);
fprintf(stderr, "user\t%lim%li.%03li\n", (long) user_time.tv_sec / 60,
(long) user_time.tv_sec % 60, user_time.tv_nsec / 1000000);
fprintf(stderr, "sys\t%lim%li.%03li\n", (long) system_time.tv_sec / 60,
(long) system_time.tv_sec % 60, system_time.tv_nsec / 1000000);
}
}
void signal_handler(int signum)
@ -60,6 +76,16 @@ void signal_handler(int signum)
int main(int argc, char* argv[])
{
int opt;
while ( (opt = getopt(argc, argv, "p")) != -1 )
{
switch ( opt )
{
case 'p': p = true; break;
default: return 1;
}
}
clock_gettime(CLOCK_MONOTONIC, &start_time);
sigset_t handleset, oldset;
sigemptyset(&handleset);
@ -67,14 +93,14 @@ int main(int argc, char* argv[])
sigaddset(&handleset, SIGQUIT);
sigprocmask(SIG_BLOCK, &handleset, &oldset);
if ( (child_pid = fork()) < 0 )
error(1, errno, "fork");
err(1, "fork");
if ( !child_pid )
{
sigprocmask(SIG_SETMASK, &oldset, NULL);
if ( argc <= 1 )
if ( argc <= optind )
exit(0);
execvp(argv[1], argv+1);
error(127, errno, "%s", argv[1]);
execvp(argv[optind], argv + optind);
err(127, "%s", argv[optind]);
}
struct sigaction sa;
memset(&sa, 0, sizeof(sa));