1 |
greg |
1.1 |
#ifndef lint |
2 |
greg |
2.13 |
static const char RCSid[] = "$Id: header.c,v 2.12 2003/02/25 02:47:21 greg Exp $"; |
3 |
greg |
1.1 |
#endif |
4 |
|
|
/* |
5 |
|
|
* header.c - routines for reading and writing information headers. |
6 |
|
|
* |
7 |
greg |
2.11 |
* Externals declared in resolu.h |
8 |
greg |
1.3 |
* |
9 |
greg |
2.4 |
* newheader(t,fp) start new information header identified by string t |
10 |
|
|
* isheadid(s) returns true if s is a header id line |
11 |
|
|
* headidval(r,s) copy header identifier value in s to r |
12 |
greg |
2.11 |
* dateval(t,s) get capture date value |
13 |
|
|
* isdate(s) returns true if s is a date line |
14 |
|
|
* fputdate(t,fp) put out the given capture date and time |
15 |
|
|
* fputnow(fp) put out the current date and time |
16 |
greg |
2.3 |
* printargs(ac,av,fp) print an argument list to fp, followed by '\n' |
17 |
greg |
1.3 |
* isformat(s) returns true if s is of the form "FORMAT=*" |
18 |
|
|
* formatval(r,s) copy the format value in s to r |
19 |
|
|
* fputformat(s,fp) write "FORMAT=%s" to fp |
20 |
|
|
* getheader(fp,f,p) read header from fp, calling f(s,p) on each line |
21 |
greg |
2.6 |
* globmatch(pat, str) check for glob match of str against pat |
22 |
greg |
1.3 |
* checkheader(i,p,o) check header format from i against p and copy to o |
23 |
|
|
* |
24 |
|
|
* To copy header from input to output, use getheader(fin, fputs, fout) |
25 |
greg |
1.1 |
*/ |
26 |
|
|
|
27 |
greg |
2.12 |
#include "copyright.h" |
28 |
greg |
2.11 |
|
29 |
greg |
1.1 |
#include <stdio.h> |
30 |
greg |
2.11 |
#include <string.h> |
31 |
|
|
#include <time.h> |
32 |
greg |
1.3 |
#include <ctype.h> |
33 |
greg |
1.1 |
|
34 |
greg |
2.3 |
#define MAXLINE 512 |
35 |
greg |
2.2 |
|
36 |
greg |
2.4 |
char HDRSTR[] = "#?"; /* information header magic number */ |
37 |
greg |
1.2 |
|
38 |
greg |
2.4 |
char FMTSTR[] = "FORMAT="; /* format identifier */ |
39 |
greg |
1.2 |
|
40 |
greg |
2.11 |
char TMSTR[] = "CAPDATE="; /* capture date identifier */ |
41 |
|
|
|
42 |
|
|
static int mycheck(); |
43 |
greg |
2.4 |
|
44 |
greg |
2.11 |
|
45 |
|
|
void |
46 |
greg |
2.4 |
newheader(s, fp) /* identifying line of information header */ |
47 |
|
|
char *s; |
48 |
|
|
register FILE *fp; |
49 |
|
|
{ |
50 |
|
|
fputs(HDRSTR, fp); |
51 |
|
|
fputs(s, fp); |
52 |
|
|
putc('\n', fp); |
53 |
|
|
} |
54 |
|
|
|
55 |
|
|
|
56 |
greg |
2.5 |
int |
57 |
greg |
2.4 |
headidval(r,s) /* get header id (return true if is id) */ |
58 |
|
|
register char *r, *s; |
59 |
|
|
{ |
60 |
|
|
register char *cp = HDRSTR; |
61 |
|
|
|
62 |
|
|
while (*cp) if (*cp++ != *s++) return(0); |
63 |
|
|
if (r == NULL) return(1); |
64 |
gregl |
2.9 |
while (*s && !isspace(*s)) *r++ = *s++; |
65 |
greg |
2.4 |
*r = '\0'; |
66 |
|
|
return(1); |
67 |
|
|
} |
68 |
|
|
|
69 |
|
|
|
70 |
greg |
2.5 |
int |
71 |
greg |
2.4 |
isheadid(s) /* check to see if line is header id */ |
72 |
|
|
char *s; |
73 |
|
|
{ |
74 |
|
|
return(headidval(NULL, s)); |
75 |
|
|
} |
76 |
|
|
|
77 |
|
|
|
78 |
greg |
2.11 |
int |
79 |
|
|
dateval(tloc, s) /* get capture date value */ |
80 |
|
|
time_t *tloc; |
81 |
|
|
char *s; |
82 |
|
|
{ |
83 |
|
|
struct tm tms; |
84 |
|
|
register char *cp = TMSTR; |
85 |
|
|
|
86 |
|
|
while (*cp) if (*cp++ != *s++) return(0); |
87 |
|
|
while (isspace(*s)) s++; |
88 |
|
|
if (!*s) return(0); |
89 |
|
|
if (sscanf(s, "%d:%d:%d %d:%d:%d", |
90 |
|
|
&tms.tm_year, &tms.tm_mon, &tms.tm_mday, |
91 |
|
|
&tms.tm_hour, &tms.tm_min, &tms.tm_sec) != 6) |
92 |
|
|
return(0); |
93 |
|
|
if (tloc == NULL) |
94 |
|
|
return(1); |
95 |
|
|
tms.tm_mon--; |
96 |
|
|
tms.tm_year -= 1900; |
97 |
|
|
tms.tm_isdst = -1; /* ask mktime() to figure out DST */ |
98 |
|
|
*tloc = mktime(&tms); |
99 |
|
|
return(1); |
100 |
|
|
} |
101 |
|
|
|
102 |
|
|
|
103 |
|
|
int |
104 |
|
|
isdate(s) /* is the given line a capture date? */ |
105 |
|
|
char *s; |
106 |
|
|
{ |
107 |
|
|
return(dateval(NULL, s)); |
108 |
|
|
} |
109 |
|
|
|
110 |
|
|
|
111 |
|
|
void |
112 |
|
|
fputdate(tv, fp) /* write out the given time value */ |
113 |
|
|
time_t tv; |
114 |
|
|
FILE *fp; |
115 |
|
|
{ |
116 |
|
|
struct tm *tm = localtime(&tv); |
117 |
|
|
if (tm == NULL) |
118 |
|
|
return; |
119 |
|
|
fprintf(fp, "%s %04d:%02d:%02d %02d:%02d:%02d\n", TMSTR, |
120 |
|
|
tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday, |
121 |
|
|
tm->tm_hour, tm->tm_min, tm->tm_sec); |
122 |
|
|
} |
123 |
|
|
|
124 |
|
|
|
125 |
|
|
void |
126 |
|
|
fputnow(fp) /* write out the current time */ |
127 |
|
|
FILE *fp; |
128 |
|
|
{ |
129 |
|
|
time_t tv; |
130 |
|
|
time(&tv); |
131 |
|
|
fputdate(tv, fp); |
132 |
|
|
} |
133 |
|
|
|
134 |
|
|
|
135 |
|
|
void |
136 |
greg |
1.1 |
printargs(ac, av, fp) /* print arguments to a file */ |
137 |
|
|
int ac; |
138 |
|
|
char **av; |
139 |
greg |
2.11 |
FILE *fp; |
140 |
greg |
1.1 |
{ |
141 |
greg |
2.2 |
int quote; |
142 |
|
|
|
143 |
greg |
1.1 |
while (ac-- > 0) { |
144 |
greg |
2.11 |
fputword(*av++, fp); |
145 |
|
|
fputc(ac ? ' ' : '\n', fp); |
146 |
greg |
1.1 |
} |
147 |
|
|
} |
148 |
|
|
|
149 |
|
|
|
150 |
greg |
2.5 |
int |
151 |
greg |
2.4 |
formatval(r, s) /* get format value (return true if format) */ |
152 |
greg |
1.2 |
register char *r; |
153 |
|
|
register char *s; |
154 |
|
|
{ |
155 |
greg |
2.4 |
register char *cp = FMTSTR; |
156 |
|
|
|
157 |
|
|
while (*cp) if (*cp++ != *s++) return(0); |
158 |
greg |
1.3 |
while (isspace(*s)) s++; |
159 |
greg |
2.4 |
if (!*s) return(0); |
160 |
|
|
if (r == NULL) return(1); |
161 |
greg |
2.5 |
do |
162 |
|
|
*r++ = *s++; |
163 |
|
|
while(*s && !isspace(*s)); |
164 |
greg |
1.2 |
*r = '\0'; |
165 |
greg |
2.4 |
return(1); |
166 |
greg |
1.2 |
} |
167 |
|
|
|
168 |
|
|
|
169 |
greg |
2.5 |
int |
170 |
greg |
2.4 |
isformat(s) /* is line a format line? */ |
171 |
|
|
char *s; |
172 |
|
|
{ |
173 |
|
|
return(formatval(NULL, s)); |
174 |
|
|
} |
175 |
|
|
|
176 |
|
|
|
177 |
greg |
2.11 |
void |
178 |
greg |
1.2 |
fputformat(s, fp) /* put out a format value */ |
179 |
|
|
char *s; |
180 |
greg |
1.1 |
FILE *fp; |
181 |
greg |
1.2 |
{ |
182 |
|
|
fputs(FMTSTR, fp); |
183 |
|
|
fputs(s, fp); |
184 |
|
|
putc('\n', fp); |
185 |
|
|
} |
186 |
|
|
|
187 |
|
|
|
188 |
greg |
2.5 |
int |
189 |
greg |
1.2 |
getheader(fp, f, p) /* get header from file */ |
190 |
|
|
FILE *fp; |
191 |
greg |
1.1 |
int (*f)(); |
192 |
greg |
1.2 |
char *p; |
193 |
greg |
1.1 |
{ |
194 |
|
|
char buf[MAXLINE]; |
195 |
|
|
|
196 |
|
|
for ( ; ; ) { |
197 |
|
|
buf[MAXLINE-2] = '\n'; |
198 |
greg |
2.3 |
if (fgets(buf, MAXLINE, fp) == NULL) |
199 |
greg |
1.1 |
return(-1); |
200 |
|
|
if (buf[0] == '\n') |
201 |
|
|
return(0); |
202 |
greg |
2.3 |
#ifdef MSDOS |
203 |
|
|
if (buf[0] == '\r' && buf[1] == '\n') |
204 |
|
|
return(0); |
205 |
|
|
#endif |
206 |
greg |
1.1 |
if (buf[MAXLINE-2] != '\n') { |
207 |
|
|
ungetc(buf[MAXLINE-2], fp); /* prevent false end */ |
208 |
|
|
buf[MAXLINE-2] = '\0'; |
209 |
|
|
} |
210 |
gwlarson |
2.10 |
if (f != NULL && (*f)(buf, p) < 0) |
211 |
|
|
return(-1); |
212 |
greg |
1.1 |
} |
213 |
|
|
} |
214 |
|
|
|
215 |
|
|
|
216 |
greg |
1.2 |
struct check { |
217 |
|
|
FILE *fp; |
218 |
greg |
1.3 |
char fs[64]; |
219 |
greg |
1.2 |
}; |
220 |
greg |
1.1 |
|
221 |
greg |
1.2 |
|
222 |
greg |
2.11 |
static int |
223 |
greg |
1.2 |
mycheck(s, cp) /* check a header line for format info. */ |
224 |
greg |
1.1 |
char *s; |
225 |
greg |
1.2 |
register struct check *cp; |
226 |
greg |
1.1 |
{ |
227 |
greg |
2.4 |
if (!formatval(cp->fs, s) && cp->fp != NULL) |
228 |
greg |
1.2 |
fputs(s, cp->fp); |
229 |
greg |
2.11 |
return(0); |
230 |
greg |
1.1 |
} |
231 |
|
|
|
232 |
|
|
|
233 |
greg |
2.5 |
int |
234 |
greg |
2.11 |
globmatch(p, s) /* check for match of s against pattern p */ |
235 |
|
|
register char *p, *s; |
236 |
greg |
1.3 |
{ |
237 |
greg |
2.11 |
int setmatch; |
238 |
greg |
1.3 |
|
239 |
|
|
do { |
240 |
|
|
switch (*p) { |
241 |
|
|
case '?': /* match any character */ |
242 |
|
|
if (!*s++) |
243 |
|
|
return(0); |
244 |
|
|
break; |
245 |
|
|
case '*': /* match any string */ |
246 |
|
|
while (p[1] == '*') p++; |
247 |
|
|
do |
248 |
greg |
2.6 |
if ( (p[1]=='?' || p[1]==*s) && |
249 |
|
|
globmatch(p+1,s) ) |
250 |
greg |
1.3 |
return(1); |
251 |
|
|
while (*s++); |
252 |
|
|
return(0); |
253 |
greg |
2.11 |
case '[': /* character set */ |
254 |
|
|
setmatch = *s == *++p; |
255 |
|
|
if (!*p) |
256 |
|
|
return(0); |
257 |
|
|
while (*++p != ']') { |
258 |
|
|
if (!*p) |
259 |
|
|
return(0); |
260 |
|
|
if (*p == '-') { |
261 |
|
|
setmatch += p[-1] <= *s && *s <= p[1]; |
262 |
|
|
if (!*++p) |
263 |
|
|
break; |
264 |
|
|
} else |
265 |
|
|
setmatch += *p == *s; |
266 |
|
|
} |
267 |
|
|
if (!setmatch) |
268 |
|
|
return(0); |
269 |
|
|
s++; |
270 |
|
|
break; |
271 |
greg |
1.3 |
case '\\': /* literal next */ |
272 |
|
|
p++; |
273 |
|
|
/* fall through */ |
274 |
|
|
default: /* normal character */ |
275 |
|
|
if (*p != *s) |
276 |
|
|
return(0); |
277 |
|
|
s++; |
278 |
|
|
break; |
279 |
|
|
} |
280 |
|
|
} while (*p++); |
281 |
|
|
return(1); |
282 |
|
|
} |
283 |
|
|
|
284 |
|
|
|
285 |
|
|
/* |
286 |
|
|
* Checkheader(fin,fmt,fout) returns a value of 1 if the input format |
287 |
|
|
* matches the specification in fmt, 0 if no input format was found, |
288 |
|
|
* and -1 if the input format does not match or there is an |
289 |
greg |
1.4 |
* error reading the header. If fmt is empty, then -1 is returned |
290 |
greg |
1.3 |
* if any input format is found (or there is an error), and 0 otherwise. |
291 |
|
|
* If fmt contains any '*' or '?' characters, then checkheader |
292 |
|
|
* does wildcard expansion and copies a matching result into fmt. |
293 |
greg |
2.7 |
* Be sure that fmt is big enough to hold the match in such cases, |
294 |
|
|
* and that it is not a static, read-only string! |
295 |
greg |
1.3 |
* The input header (minus any format lines) is copied to fout |
296 |
|
|
* if fout is not NULL. |
297 |
|
|
*/ |
298 |
|
|
|
299 |
greg |
2.5 |
int |
300 |
greg |
1.3 |
checkheader(fin, fmt, fout) |
301 |
greg |
1.2 |
FILE *fin; |
302 |
|
|
char *fmt; |
303 |
|
|
FILE *fout; |
304 |
greg |
1.1 |
{ |
305 |
greg |
1.2 |
struct check cdat; |
306 |
greg |
2.7 |
register char *cp; |
307 |
greg |
1.2 |
|
308 |
|
|
cdat.fp = fout; |
309 |
|
|
cdat.fs[0] = '\0'; |
310 |
|
|
if (getheader(fin, mycheck, &cdat) < 0) |
311 |
greg |
1.3 |
return(-1); |
312 |
greg |
2.6 |
if (!cdat.fs[0]) |
313 |
|
|
return(0); |
314 |
greg |
2.7 |
for (cp = fmt; *cp; cp++) /* check for globbing */ |
315 |
|
|
if (*cp == '?' | *cp == '*') |
316 |
|
|
if (globmatch(fmt, cdat.fs)) { |
317 |
|
|
strcpy(fmt, cdat.fs); |
318 |
|
|
return(1); |
319 |
|
|
} else |
320 |
|
|
return(-1); |
321 |
|
|
return(strcmp(fmt, cdat.fs) ? -1 : 1); /* literal match */ |
322 |
greg |
1.1 |
} |