-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_run_time_from_log_file.c
42 lines (35 loc) · 1.31 KB
/
get_run_time_from_log_file.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
/* -------------------------------------------------------------------------- *
* Estimate the run time from the statistics on a log file. *
* *
* Assumes access time is less than the changed time as the access time is *
* set when the log file is opened. *
* -------------------------------------------------------------------------- */
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
struct stat statbuf;
if (argc < 2)
{
fprintf(stderr, "No file name provided\n");
exit(EXIT_FAILURE);
}
if (stat(argv[1], &statbuf) == -1)
{
fprintf(stderr, "stat of %s failed: %m\n", argv[1]);
exit(EXIT_FAILURE);
}
long diff = statbuf.st_mtim.tv_sec - statbuf.st_atim.tv_sec;
int num_days = diff / 86400;
int remaining = diff - 86400 * num_days;
int num_hours = remaining / 3600;
remaining -= 3600*num_hours;
int num_mins = remaining / 60;
int num_secs = remaining - 60*num_mins;
printf( "%3dd %2dh %2dm %2ds\n", num_days, num_hours, num_mins, num_secs );
exit(EXIT_SUCCESS);
}