forked from TLeconte/acarsdec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileout.c
99 lines (89 loc) · 2.32 KB
/
fileout.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
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <sys/types.h>
#include <sys/time.h>
#include <time.h>
#include <errno.h>
extern int hourly,daily;
static char *filename_prefix = NULL;
static char *extension = NULL;
static size_t prefix_len;
static struct tm current_tm;
static FILE *open_outfile() {
char *filename = NULL;
char *fmt = NULL;
size_t tlen = 0;
FILE *fd;
if(hourly || daily) {
time_t t = time(NULL);
gmtime_r(&t, ¤t_tm);
char suffix[16];
if(hourly) {
fmt = "_%Y%m%d_%H";
} else { // daily
fmt = "_%Y%m%d";
}
tlen = strftime(suffix, sizeof(suffix), fmt, ¤t_tm);
if(tlen == 0) {
fprintf(stderr, "*open_outfile(): strfime returned 0\n");
return NULL;
}
filename = calloc(prefix_len + tlen + 2, sizeof(char));
if(filename == NULL) {
fprintf(stderr, "open_outfile(): failed to allocate memory\n");
return NULL;
}
sprintf(filename, "%s%s%s", filename_prefix, suffix, extension);
} else {
filename = strdup(filename_prefix);
}
if((fd = fopen(filename, "a+")) == NULL) {
fprintf(stderr, "Could not open output file %s: %s\n", filename, strerror(errno));
free(filename);
return NULL;
}
free(filename);
return fd;
}
FILE* Fileoutinit(char* logfilename)
{
FILE *fd;
filename_prefix = logfilename;
prefix_len = strlen(filename_prefix);
if(hourly || daily) {
char *basename = strrchr(filename_prefix, '/');
if(basename != NULL) {
basename++;
} else {
basename = filename_prefix;
}
char *ext = strrchr(filename_prefix, '.');
if(ext != NULL && (ext <= basename || ext[1] == '\0')) {
ext = NULL;
}
if(ext) {
extension = strdup(ext);
*ext = '\0';
} else {
extension = strdup("");
}
}
if((fd=open_outfile()) == NULL)
return NULL;
return fd;
}
FILE* Fileoutrotate(FILE *fd)
{
struct tm new_tm;
time_t t = time(NULL);
gmtime_r(&t, &new_tm);
if((hourly && new_tm.tm_hour != current_tm.tm_hour) ||
(daily && new_tm.tm_mday != current_tm.tm_mday)) {
fclose(fd);
return open_outfile();
}
return fd;
}