-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
164 lines (135 loc) · 2.96 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include<stdarg.h>
static const char FILE_NAME[] = ".timenator";
struct timespec *ct;
/**
* Printing tool usage instruction.
*/
void print_usage()
{
printf("Usage: timenator (start|stop)\n");
}
void print_log(const char* format, ...)
{
time_t now;
time(&now);
char time_str[sizeof "2011-10-08T07:07:09Z"];
strftime(time_str, sizeof time_str, "%Y-%m-%dT%H:%M:%SZ", gmtime(&now));
va_list arg;
va_start(arg, format);
printf("[%s] ", time_str);
vprintf(format, arg);
puts("");
va_end(arg);
}
/**
* Storing last time.
*/
int store_time(const struct timespec *t)
{
FILE *f = fopen(FILE_NAME, "wb+");
fwrite(t, 1, sizeof(struct timespec), f);
fclose(f);
return 0;
}
/**
* Get current system monotonic time.
*/
struct timespec *get_current_time()
{
struct timespec *t = malloc(sizeof(struct timespec));
clock_gettime(CLOCK_MONOTONIC, t);
return t;
}
/**
* Get time from storage by key.
*
* @return null or *timespec.
*/
struct timespec *retrive_time()
{
FILE *f = fopen(FILE_NAME, "rb");
if (f == NULL) {
return NULL;
}
struct timespec *time = malloc(sizeof(struct timespec));
fread(time, 1, sizeof(struct timespec), f);
fclose(f);
return time;
}
/**
* Delete storage
*/
void clear_storage()
{
remove(FILE_NAME);
}
/**
* Printing time with event
*/
void print_time(const struct timespec *t, char *e)
{
print_log("event=\"%s\" time=%ld.%09ld\n", e, t->tv_sec, t->tv_nsec);
}
/**
* Calculate difference and print event, times and difference.
*/
void print_diff(const struct timespec *current, const struct timespec *t, char *e)
{
struct timespec d;
if ((current->tv_nsec - t->tv_nsec) < 0) {
d.tv_sec = current->tv_sec - t->tv_sec - 1;
d.tv_nsec = current->tv_nsec - t->tv_nsec + 1000000000;
} else {
d.tv_sec = current->tv_sec - t->tv_sec;
d.tv_nsec = current->tv_nsec - t->tv_nsec;
}
print_log(
"event=\"%s\" start=%ld.%09ld stop=%ld.%09ld duration=%ld.%09ld",
e,
t->tv_sec,
t->tv_nsec,
current->tv_sec,
current->tv_nsec,
d.tv_sec,
d.tv_nsec
);
}
int cmd_start(char *event) {
store_time(ct);
print_time(ct, event);
return 0;
}
int cmd_stop(char *event) {
struct timespec *t;
t = retrive_time();
if (t == NULL) {
t = get_current_time();
print_diff(t, ct, event);
} else {
print_diff(ct, t, event);
}
clear_storage();
return 0;
}
int main(int argc, char *argv[])
{
ct = get_current_time();
if (argc < 2) {
print_usage();
exit(0);
}
char *event = NULL;
if (argc > 2) {
event = argv[2];
}
if (strcmp(argv[1], "start") == 0) {
return cmd_start(event);
} else if (strcmp(argv[1], "stop") == 0) {
return cmd_stop(event);
}
print_usage();
}