forked from AndrewHastings/cdctap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutfile.c
164 lines (133 loc) · 3.28 KB
/
outfile.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
/*
* Copyright 2024 Andrew B. Hastings. All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/*
* Output file utility routines.
*/
#include <sys/time.h>
#include <time.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#undef _POSIX_C_SOURCE /* I didn't set it; who did?? */
#include <fnmatch.h>
#include "cdctap.h"
#include "ifmt.h"
#include "pfdump.h"
#include "outfile.h"
int sout = 0;
/* match pattern as "ui/pat" or "un/pat" or "pat" */
/* returns pat if case-free exact match, name if wildcard match, or NULL */
/* uses "name" as scratch buffer to remove '/' from return value */
char *name_match(char *pattern, char *name, int ui)
{
long want_ui;
char *pat, *sp, *ep;
pat = pattern;
sp = strchr(pattern, '/');
/* ui or un specified */
if (sp) {
pat = sp+1;
want_ui = strtoul(pattern, &ep, 8);
/* ui didn't parse, see if valid un */
if (sp != ep)
want_ui = un_to_ui(pattern);
if (ui != want_ui)
return NULL;
}
dprint(("name_match: pat=%s\n", pat));
if (strcasecmp(pat, name) == 0)
strcpy(name, pat);
else if (fnmatch(pat, name, FNM_CASEFOLD) != 0)
return NULL;
/* remove '/' from name */
ep = name;
for (sp = name; *sp; sp++) {
if (*sp != '/')
*ep++ = *sp;
}
*ep = '\0';
if (name[0] == '\0')
strcpy(name, "null");
return name;
}
/* parse date into *tm */
/* returns -1 if parse failure */
int parse_date(char *date, struct tm *tm)
{
int rv;
rv = sscanf(date, " %d/%d/%d", &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
/* Ensure invalid date if parse failed */
if (rv < 3) {
tm->tm_mday = 0;
return -1;
}
tm->tm_mon--; /* months are zero-based */
if (tm->tm_year < 60)
tm->tm_year += 100;
dprint(("parse-date: parsed %s\n", date));
return 0;
}
void set_mtime(char *fname, struct tm *tm)
{
struct timeval times[2];
if (!fname[0] || !tm->tm_mday)
return;
tm->tm_isdst = -1;
times[1].tv_sec = mktime(tm);
if (times[1].tv_sec == -1) {
fprintf(stderr, "%s: mtime invalid\n", fname);
return;
}
times[0].tv_sec = time(NULL);
times[0].tv_usec = times[1].tv_usec = 0;
if (utimes(fname, times) < 0) {
fprintf(stderr, "%s: ", fname);
perror("utimes");
}
}
/* actual file name returned in fname */
FILE *out_open(char *name, char *sfx, char *fname)
{
int i;
FILE *rv;
if (sout) {
fname[0] = '\0';
return stdout;
}
sprintf(fname, "%s.%s", name, sfx);
for (i = 0; i < 100; i++) {
rv = fopen(fname, "wx");
if (rv) {
printf("Extracting to %s\n", fname);
break;
}
if (errno != EEXIST) {
perror(fname);
break;
}
sprintf(fname, "%s.%d.%s", name, i+1, sfx);
}
return rv;
}
void out_close(FILE *of)
{
if (of == stdout)
return;
fclose(of);
}