coreutils

General Software Utilities
git clone http://git.omkov.net/coreutils
Log | Tree | Refs | README | LICENCE | Download

coreutils/src/time.c (86 lines, 2.1 KiB) -rw-r--r-- file download

a87087e Jamozed 2020-07-06 23:43:24
0
// time.c, version 1.0.1
29dd2d0 Jamozed 2020-07-06 03:21:49
1
// OMKOV coreutils implementation of POSIX time
29dd2d0 Jamozed 2020-07-06 03:21:49
2
// Copyright (C) 2020, Jakob Wakeling
e2140ec Jamozed 2022-03-06 15:27:45
3
// MIT Licence
29dd2d0 Jamozed 2020-07-06 03:21:49
4
b181413 Jamozed 2022-02-05 22:32:00
5
#include "util/error.h"
b181413 Jamozed 2022-02-05 22:32:00
6
#include "util/optget.h"
29dd2d0 Jamozed 2020-07-06 03:21:49
7
29dd2d0 Jamozed 2020-07-06 03:21:49
8
#include <sys/times.h>
29dd2d0 Jamozed 2020-07-06 03:21:49
9
#include <sys/wait.h>
29dd2d0 Jamozed 2020-07-06 03:21:49
10
#include <unistd.h>
29dd2d0 Jamozed 2020-07-06 03:21:49
11
29dd2d0 Jamozed 2020-07-06 03:21:49
12
#include <errno.h>
29dd2d0 Jamozed 2020-07-06 03:21:49
13
#include <stdbool.h>
29dd2d0 Jamozed 2020-07-06 03:21:49
14
#include <stdio.h>
29dd2d0 Jamozed 2020-07-06 03:21:49
15
#include <time.h>
29dd2d0 Jamozed 2020-07-06 03:21:49
16
a87087e Jamozed 2020-07-06 23:43:24
17
#define VERSION "1.0.1"
29dd2d0 Jamozed 2020-07-06 03:21:49
18
30cf081 Jamozed 2020-08-28 23:01:02
19
static struct lop lops[] = {
30cf081 Jamozed 2020-08-28 23:01:02
20
	{ "help",    ARG_NUL, 256 },
30cf081 Jamozed 2020-08-28 23:01:02
21
	{ "version", ARG_NUL, 257 },
30cf081 Jamozed 2020-08-28 23:01:02
22
	{ NULL, 0, 0 }
30cf081 Jamozed 2020-08-28 23:01:02
23
};
30cf081 Jamozed 2020-08-28 23:01:02
24
29dd2d0 Jamozed 2020-07-06 03:21:49
25
static bool pflag;
29dd2d0 Jamozed 2020-07-06 03:21:49
26
30cf081 Jamozed 2020-08-28 23:01:02
27
static void hlp(void);
30cf081 Jamozed 2020-08-28 23:01:02
28
static void ver(void);
29dd2d0 Jamozed 2020-07-06 03:21:49
29
b634f01 Jamozed 2020-07-09 22:11:16
30
int main(int ac, char *av[]) { (void)ac; A0 = av[0];
30cf081 Jamozed 2020-08-28 23:01:02
31
	struct opt opt = OPTGET_INIT; opt.str = "p"; opt.lops = lops;
30cf081 Jamozed 2020-08-28 23:01:02
32
	for (int o; (o = optget(&opt, av, 0)) != -1;) switch (o) {
29dd2d0 Jamozed 2020-07-06 03:21:49
33
	case 'p': { pflag = true; break; }
30cf081 Jamozed 2020-08-28 23:01:02
34
	case 256: { hlp(); return 0; }
30cf081 Jamozed 2020-08-28 23:01:02
35
	case 257: { ver(); return 0; }
29dd2d0 Jamozed 2020-07-06 03:21:49
36
	default: { return 1; }
29dd2d0 Jamozed 2020-07-06 03:21:49
37
	}
29dd2d0 Jamozed 2020-07-06 03:21:49
38
	
29dd2d0 Jamozed 2020-07-06 03:21:49
39
	struct tms tm; clock_t cl = times(&tm);
29dd2d0 Jamozed 2020-07-06 03:21:49
40
	
29dd2d0 Jamozed 2020-07-06 03:21:49
41
	pid_t pid = fork();
a87087e Jamozed 2020-07-06 23:43:24
42
	if (pid == -1) { error(1, "%s", serr()); }
29dd2d0 Jamozed 2020-07-06 03:21:49
43
	else if (pid == 0) { 
29dd2d0 Jamozed 2020-07-06 03:21:49
44
		execvp(av[opt.ind], &av[opt.ind]);
a87087e Jamozed 2020-07-06 23:43:24
45
		warn("%s: %s", av[opt.ind], serr());
29dd2d0 Jamozed 2020-07-06 03:21:49
46
		return errno = ENOENT ? 127 : 126;
29dd2d0 Jamozed 2020-07-06 03:21:49
47
	}
29dd2d0 Jamozed 2020-07-06 03:21:49
48
	
29dd2d0 Jamozed 2020-07-06 03:21:49
49
	int status; waitpid(pid, &status, 0);
29dd2d0 Jamozed 2020-07-06 03:21:49
50
	cl = times(&tm) - cl;
29dd2d0 Jamozed 2020-07-06 03:21:49
51
	
29dd2d0 Jamozed 2020-07-06 03:21:49
52
	double tick = (double)sysconf(_SC_CLK_TCK);
29dd2d0 Jamozed 2020-07-06 03:21:49
53
	double real = (double)cl / tick;
29dd2d0 Jamozed 2020-07-06 03:21:49
54
	double user = (double)(tm.tms_utime + tm.tms_cutime) / tick;
29dd2d0 Jamozed 2020-07-06 03:21:49
55
	double sys  = (double)(tm.tms_stime + tm.tms_cstime) / tick;
29dd2d0 Jamozed 2020-07-06 03:21:49
56
	
29dd2d0 Jamozed 2020-07-06 03:21:49
57
	if (pflag) {
29dd2d0 Jamozed 2020-07-06 03:21:49
58
		fprintf(stderr, "real %0.2f\n", real);
29dd2d0 Jamozed 2020-07-06 03:21:49
59
		fprintf(stderr, "user %0.2f\n", user);
29dd2d0 Jamozed 2020-07-06 03:21:49
60
		fprintf(stderr, "sys %0.2f\n", sys);
29dd2d0 Jamozed 2020-07-06 03:21:49
61
	}
29dd2d0 Jamozed 2020-07-06 03:21:49
62
	else {
29dd2d0 Jamozed 2020-07-06 03:21:49
63
		fprintf(stderr, "real  %0.2fs\n", real);
29dd2d0 Jamozed 2020-07-06 03:21:49
64
		fprintf(stderr, "user  %0.2fs\n", user);
29dd2d0 Jamozed 2020-07-06 03:21:49
65
		fprintf(stderr, "sys   %0.2fs\n", sys);
29dd2d0 Jamozed 2020-07-06 03:21:49
66
	}
29dd2d0 Jamozed 2020-07-06 03:21:49
67
	
29dd2d0 Jamozed 2020-07-06 03:21:49
68
	return WEXITSTATUS(status);
29dd2d0 Jamozed 2020-07-06 03:21:49
69
}
29dd2d0 Jamozed 2020-07-06 03:21:49
70
30cf081 Jamozed 2020-08-28 23:01:02
71
static void hlp(void) {
29dd2d0 Jamozed 2020-07-06 03:21:49
72
	puts("time - time a simple command\n");
29dd2d0 Jamozed 2020-07-06 03:21:49
73
	puts("usage: time [-p] command [argument...]\n");
29dd2d0 Jamozed 2020-07-06 03:21:49
74
	puts("options:");
29dd2d0 Jamozed 2020-07-06 03:21:49
75
	puts("  -p            Use portable format");
29dd2d0 Jamozed 2020-07-06 03:21:49
76
	puts("  --help        Display help information");
29dd2d0 Jamozed 2020-07-06 03:21:49
77
	puts("  --version     Display version information");
29dd2d0 Jamozed 2020-07-06 03:21:49
78
}
29dd2d0 Jamozed 2020-07-06 03:21:49
79
30cf081 Jamozed 2020-08-28 23:01:02
80
static void ver(void) {
29dd2d0 Jamozed 2020-07-06 03:21:49
81
	puts("OMKOV coreutils time, version " VERSION);
29dd2d0 Jamozed 2020-07-06 03:21:49
82
	puts("Copyright (C) 2020, Jakob Wakeling");
e2140ec Jamozed 2022-03-06 15:27:45
83
	puts("MIT Licence (https://opensource.org/licenses/MIT)");
29dd2d0 Jamozed 2020-07-06 03:21:49
84
}
85