-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
114 lines (89 loc) · 1.99 KB
/
main.c
File metadata and controls
114 lines (89 loc) · 1.99 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
#include <stdio.h>
#include "main.h"
#define BUF_SIZE 4096
int starting_day;
int ending_day;
int main()
{
struct time_stamp ts;
FILE* f;
f = open_file("./haproxy.log");
find_start_end_days(f);
printf("STARTING DAY: %d\n", starting_day);
printf("ENDING DAY: %d\n", ending_day);
fclose(f);
return 0;
}
FILE* open_file(char path[])
{
FILE *f;
f = fopen(path, "rb");
setvbuf(f, NULL, _IOFBF, BUF_SIZE);
return f;
}
void find_start_end_days(FILE *f)
{
struct time_stamp ts;
long pos;
char str[BUF_SIZE];
pos = ftell(f);
// Starting day
fseek(f, 0, SEEK_SET);
fread(str, sizeof(char), BUF_SIZE, f);
parse_line(&ts, str);
starting_day = ts.day;
// Ending day
// XXX: Not working.
fseek(f, -BUF_SIZE, SEEK_END);
int offset = fread(str, sizeof(char), BUF_SIZE, f);
printf("str: %s\n", str);
offset -= 2;
printf("offset: %d\n", offset);
line_start_offset(offset, str);
parse_line(&ts, str + offset);
ending_day = ts.day;
}
int line_start_offset(int start, char str[])
{
int i = start;
for (i = start; i > 0; i--)
{
if (str[i] == '\n')
{
i++;
break;
}
}
return i;
}
long ts_to_time(int d, int h, int m, int s)
{
long time = s + (60 * m) + (60 * 60 * h);
if (d == ending_day)
{
time += (24 * 60 * 60);
}
return time;
}
void gen_time_stamp(struct time_stamp *ts, int d, int h, int m, int s)
{
ts->day = d;
ts->hour = h;
ts->month = m;
ts->second = s;
ts->time = ts_to_time(d, h, m, s);
}
void parse_line(struct time_stamp *ts, char str[])
{
int d, h, m, s;
printf("%s\n", str);
sscanf(str, "%*s %d %d:%d:%d", &d, &h, &m, &s);
gen_time_stamp(ts, d, h, m, s);
}
long midpoint(long x, long y)
{
long mid = (x >> 1) + (y >> 1);
// Offset takes into account the situation where both x and y are odd.
long offset = (x & 1) & (y & 1);
return mid + offset;
}