-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdifftime.c
More file actions
350 lines (306 loc) · 7.94 KB
/
Copy pathdifftime.c
File metadata and controls
350 lines (306 loc) · 7.94 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#include <errno.h>
#include <getopt.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
const char * help_msg = "Help \n\
version: %s \n\
usage: %s \
[-a|--atime] \
[-c|--ctime] \
[-m|--mtime] \
[-f]--field YMDhms] \
[-A|--absolute] \
[-D|--days] \
[-H|--hours] \
[-M|--minutes] \
[-P|--print] \
[-h|--help] \
[-v|--version] \
file1 [file2] \
\n\
\n\
difftime is a simple utility to calculate and print out the delta time from \n\
two files or a file and now. It's usefull to make a gate in a script, based on time. \n\
\n\
Options: \n\
-a, --atime file compare last access time\n\
-c, --ctime file compare change time\n\
-m, --mtime file compare modification time\n\
-f, --field Y|M|D|h|m|s diff only a part of the date \n\
-A, --absolute output without sign\n\
-D, --days output in days\n\
-H, --hours output in hours\n\
-M, --minutes output in minutes\n\
-P, --print add newline character after the number\n\
-h, --help display this help message \n\
-v, --version display the software version \n\
\n\
Example: \n\
difftime -P /var/spool/older /var/spool/newer\n\
difftime -P /var/spool/file \n\
difftime -P -D -c /var/spool/file -a file2 \n\
difftime -P -f D -c /var/spool/file -a file2 \n\
";
static struct option const long_options[] = {
{"atime", required_argument, NULL, 'a'},
{"ctime", required_argument, NULL, 'c'},
{"mtime", required_argument, NULL, 'm'},
{"field", required_argument, NULL, 'f'},
{"days", no_argument, NULL, 'D'},
{"hours", no_argument, NULL, 'H'},
{"minutes", no_argument, NULL, 'M'},
{"absolute", no_argument, NULL, 'A'},
{"print", no_argument, NULL, 'P'},
{"help", no_argument, NULL, 'h'},
{"version", no_argument, NULL, 'v'},
{NULL, 0, NULL, 0}
};
enum TimeField {
ATIME,
CTIME,
MTIME
};
enum OutputUnit {
SECONDS,
MINUTES,
HOURS,
DAYS
};
struct {
bool print;
bool absolute;
enum TimeField from_time;
enum TimeField to_time;
enum OutputUnit output_unit;
char field;
} config;
void config_init(void);
#include "config.h"
void print_help(char *name);
void print_version(char *name);
void die(const char *prefix, const char *msg);
/*
* time difference using config
*/
time_t timediff(time_t a, time_t b);
/* Get the time from st according to tf */
time_t extract_time(struct stat *st, enum TimeField tf);
/* convenient functions */
void config_time(char *arg, char **f, char **t, enum TimeField tm);
int config_field(char *arg);
int main(int argc, char *argv[])
{
time_t tfrom, tto, tdiff;
char *ffrom, *fto; /* filenames */
struct stat st;
int c;
char fmt[10];
config_init();
/* manage options */
ffrom = NULL;
fto = NULL;
while ((c = getopt_long(argc, argv, "a:c:m:f:ADHMPhv", long_options, NULL)) > 0){
switch(c){
case 'a': /* atime */
config_time(optarg, &ffrom, &fto, ATIME);
break;
case 'c': /* ctime */
config_time(optarg, &ffrom, &fto, CTIME);
break;
case 'm': /* mtime */
config_time(optarg, &ffrom, &fto, MTIME);
break;
case 'f': /* field */
if (config_field(optarg) < 0){
fprintf(stderr, "invalid field: %s\n", optarg);
exit(EXIT_FAILURE);
}
break;
case 'h': /* help */
print_help(argv[0]);
exit(EXIT_SUCCESS);
break;
case 'v': /* version */
print_version(argv[0]);
exit(EXIT_SUCCESS);
break;
case 'A': /* absolute */
config.absolute = true;
break;
case 'D': /* days */
config.output_unit = DAYS;
break;
case 'H': /* hours */
config.output_unit = HOURS;
break;
case 'M': /* minutes */
config.output_unit = MINUTES;
break;
case 'P': /* print */
config.print = true;
break;
default:
fprintf(stderr, "%s --help for usage\n", argv[0]);
exit(EXIT_FAILURE);
}
}
/* Extract the times */
errno = 0;
if (ffrom == NULL){
if (optind >= argc)
die("First file","filename required. See -h");
ffrom = argv[optind++];
}
if (stat(ffrom, &st) < 0)
die(ffrom, strerror(errno));
tfrom = extract_time(&st, config.from_time);
/* second argument can be set with option (like -c), wihout option
* or not set (use time instead)
*/
if (fto == NULL && optind < argc){ /* set without option */
fto = argv[optind++]; /* get from argv and continue */
}
if (fto != NULL){ /* with filename */
if (stat(fto, &st) < 0)
die(fto,strerror(errno));
tto = extract_time(&st, config.to_time);
} else { /* not provided, use the current time */
if (time(&tto) < 0)
die("Get current time",strerror(errno));
}
/* OUTPUT */
if (config.print){
strcpy(fmt, "%ld\n");
} else {
strcpy(fmt, "%ld");
}
tdiff = timediff(tto, tfrom);
if (config.field == '\0'){
/* apply the conversion only when diff whole time */
switch (config.output_unit){
case SECONDS:
/* nothing to do */
break;
case MINUTES:
tdiff /= 60; /* seconds in 1 minute */
break;
case HOURS:
tdiff /= 3600; /* seconds in 1 hour */
break;
case DAYS:
tdiff /= 86400; /* seconds in 1 day */
break;
}
}
printf(fmt, tdiff);
return 0;
}
void print_help(char *name)
{
fprintf(stderr, help_msg, VERSION, name);
}
void print_version(char *name)
{
fprintf(stdout, "%s: %s\n", name, VERSION);
}
void die(const char *prefix, const char *msg)
{
if (prefix){
fprintf(stderr, "%s: ", prefix);
} else {
fprintf(stderr, "FATAL: ");
}
fprintf(stderr, "%s\n", msg);
exit(EXIT_FAILURE);
}
time_t timediff(time_t a, time_t b)
{
time_t d;
struct tm *t;
switch (config.field){
case 's':
t = gmtime(&a);
d = t->tm_sec;
t = gmtime(&b);
d -= t->tm_sec;
break;
case 'm':
t = gmtime(&a);
d = t->tm_min;
t = gmtime(&b);
d -= t->tm_min;
break;
case 'h':
t = gmtime(&a);
d = t->tm_hour;
t = gmtime(&b);
d -= t->tm_hour;
break;
case 'D':
t = gmtime(&a);
d = t->tm_mday;
t = gmtime(&b);
d -= t->tm_mday;
break;
case 'M':
t = gmtime(&a);
d = t->tm_mon;
t = gmtime(&b);
d -= t->tm_mon;
break;
case 'Y':
t = gmtime(&a);
d = t->tm_year;
t = gmtime(&b);
d -= t->tm_year;
break;
default:
d = difftime(a, b);
break;
}
if (config.absolute)
d = labs(d);
return d;
}
time_t extract_time(struct stat *st, enum TimeField tf)
{
time_t tm;
switch (tf){
case ATIME:
tm = st->st_atime;
break;
case CTIME:
tm = st->st_ctime;
break;
case MTIME:
tm = st->st_mtime;
break;
}
return tm;
}
void config_time(char *arg, char **f, char **t, enum TimeField tm)
{
if (*f == NULL){
config.from_time = tm;
*f = arg;
} else {
config.to_time = tm;
*t = arg;
}
}
int config_field(char *arg)
{
if (arg == NULL)
return -1;
if (strlen(arg) > 1)
return -1;
if (strstr("YMDhms", arg) == NULL)
return -1;
config.field = arg[0];
return 0;
}