1 |
– |
/* Copyright (c) 1996 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* header.c - routines for reading and writing information headers. |
6 |
|
* |
7 |
< |
* 8/19/88 |
7 |
> |
* Externals declared in resolu.h |
8 |
|
* |
9 |
|
* newheader(t,fp) start new information header identified by string t |
13 |
– |
* isheadid(s) returns true if s is a header id line |
10 |
|
* headidval(r,s) copy header identifier value in s to r |
11 |
+ |
* dateval(t,s) get capture date value as UTC |
12 |
+ |
* gmtval(t,s) get GMT as UTC |
13 |
+ |
* fputdate(t,fp) put out the given UTC |
14 |
+ |
* fputnow(fp) put out the current date and time |
15 |
|
* printargs(ac,av,fp) print an argument list to fp, followed by '\n' |
16 |
– |
* isformat(s) returns true if s is of the form "FORMAT=*" |
16 |
|
* formatval(r,s) copy the format value in s to r |
17 |
|
* fputformat(s,fp) write "FORMAT=%s" to fp |
18 |
|
* getheader(fp,f,p) read header from fp, calling f(s,p) on each line |
22 |
|
* To copy header from input to output, use getheader(fin, fputs, fout) |
23 |
|
*/ |
24 |
|
|
25 |
< |
#include <stdio.h> |
25 |
> |
#include "copyright.h" |
26 |
> |
|
27 |
|
#include <ctype.h> |
28 |
|
|
29 |
< |
#define MAXLINE 512 |
29 |
> |
#include "rtio.h" |
30 |
> |
#include "resolu.h" |
31 |
|
|
32 |
< |
#ifndef BSD |
32 |
< |
#define index strchr |
33 |
< |
#endif |
32 |
> |
#define MAXLINE 2048 |
33 |
|
|
34 |
< |
extern char *index(); |
34 |
> |
const char HDRSTR[] = "#?"; /* information header magic number */ |
35 |
|
|
36 |
< |
char HDRSTR[] = "#?"; /* information header magic number */ |
36 |
> |
const char FMTSTR[] = "FORMAT="; /* format identifier */ |
37 |
|
|
38 |
< |
char FMTSTR[] = "FORMAT="; /* format identifier */ |
38 |
> |
const char TMSTR[] = "CAPDATE="; /* capture date identifier */ |
39 |
> |
const char GMTSTR[] = "GMT="; /* GMT identifier */ |
40 |
|
|
41 |
+ |
static gethfunc mycheck; |
42 |
|
|
43 |
< |
newheader(s, fp) /* identifying line of information header */ |
44 |
< |
char *s; |
45 |
< |
register FILE *fp; |
43 |
> |
|
44 |
> |
extern void |
45 |
> |
newheader( /* identifying line of information header */ |
46 |
> |
char *s, |
47 |
> |
FILE *fp |
48 |
> |
) |
49 |
|
{ |
50 |
|
fputs(HDRSTR, fp); |
51 |
|
fputs(s, fp); |
53 |
|
} |
54 |
|
|
55 |
|
|
56 |
< |
int |
57 |
< |
headidval(r,s) /* get header id (return true if is id) */ |
58 |
< |
register char *r, *s; |
56 |
> |
extern int |
57 |
> |
headidval( /* get header id (return true if is id) */ |
58 |
> |
char *r, |
59 |
> |
char *s |
60 |
> |
) |
61 |
|
{ |
62 |
< |
register char *cp = HDRSTR; |
62 |
> |
const char *cp = HDRSTR; |
63 |
|
|
64 |
|
while (*cp) if (*cp++ != *s++) return(0); |
65 |
|
if (r == NULL) return(1); |
69 |
|
} |
70 |
|
|
71 |
|
|
72 |
< |
int |
73 |
< |
isheadid(s) /* check to see if line is header id */ |
74 |
< |
char *s; |
72 |
> |
extern int |
73 |
> |
dateval( /* convert capture date line to UTC */ |
74 |
> |
time_t *tloc, |
75 |
> |
char *s |
76 |
> |
) |
77 |
|
{ |
78 |
< |
return(headidval(NULL, s)); |
78 |
> |
struct tm tms; |
79 |
> |
const char *cp = TMSTR; |
80 |
> |
|
81 |
> |
while (*cp) if (*cp++ != *s++) return(0); |
82 |
> |
while (isspace(*s)) s++; |
83 |
> |
if (!*s) return(0); |
84 |
> |
if (sscanf(s, "%d:%d:%d %d:%d:%d", |
85 |
> |
&tms.tm_year, &tms.tm_mon, &tms.tm_mday, |
86 |
> |
&tms.tm_hour, &tms.tm_min, &tms.tm_sec) != 6) |
87 |
> |
return(0); |
88 |
> |
if (tloc == NULL) |
89 |
> |
return(1); |
90 |
> |
tms.tm_mon--; |
91 |
> |
tms.tm_year -= 1900; |
92 |
> |
tms.tm_isdst = -1; /* ask mktime() to figure out DST */ |
93 |
> |
*tloc = mktime(&tms); |
94 |
> |
return(1); |
95 |
|
} |
96 |
|
|
97 |
|
|
98 |
< |
printargs(ac, av, fp) /* print arguments to a file */ |
99 |
< |
int ac; |
100 |
< |
char **av; |
101 |
< |
register FILE *fp; |
98 |
> |
extern int |
99 |
> |
gmtval( /* convert GMT date line to UTC */ |
100 |
> |
time_t *tloc, |
101 |
> |
char *s |
102 |
> |
) |
103 |
|
{ |
104 |
< |
int quote; |
104 |
> |
struct tm tms; |
105 |
> |
const char *cp = GMTSTR; |
106 |
|
|
107 |
+ |
while (*cp) if (*cp++ != *s++) return(0); |
108 |
+ |
while (isspace(*s)) s++; |
109 |
+ |
if (!*s) return(0); |
110 |
+ |
if (sscanf(s, "%d:%d:%d %d:%d:%d", |
111 |
+ |
&tms.tm_year, &tms.tm_mon, &tms.tm_mday, |
112 |
+ |
&tms.tm_hour, &tms.tm_min, &tms.tm_sec) != 6) |
113 |
+ |
return(0); |
114 |
+ |
if (tloc == NULL) |
115 |
+ |
return(1); |
116 |
+ |
tms.tm_mon--; |
117 |
+ |
tms.tm_year -= 1900; |
118 |
+ |
*tloc = timegm(&tms); |
119 |
+ |
return(1); |
120 |
+ |
} |
121 |
+ |
|
122 |
+ |
|
123 |
+ |
extern void |
124 |
+ |
fputdate( /* write out the given time value (local & GMT) */ |
125 |
+ |
time_t tv, |
126 |
+ |
FILE *fp |
127 |
+ |
) |
128 |
+ |
{ |
129 |
+ |
struct tm *tms; |
130 |
+ |
|
131 |
+ |
tms = localtime(&tv); |
132 |
+ |
if (tms != NULL) |
133 |
+ |
fprintf(fp, "%s %04d:%02d:%02d %02d:%02d:%02d\n", TMSTR, |
134 |
+ |
tms->tm_year+1900, tms->tm_mon+1, tms->tm_mday, |
135 |
+ |
tms->tm_hour, tms->tm_min, tms->tm_sec); |
136 |
+ |
tms = gmtime(&tv); |
137 |
+ |
if (tms != NULL) |
138 |
+ |
fprintf(fp, "%s %04d:%02d:%02d %02d:%02d:%02d\n", GMTSTR, |
139 |
+ |
tms->tm_year+1900, tms->tm_mon+1, tms->tm_mday, |
140 |
+ |
tms->tm_hour, tms->tm_min, tms->tm_sec); |
141 |
+ |
} |
142 |
+ |
|
143 |
+ |
|
144 |
+ |
extern void |
145 |
+ |
fputnow( /* write out the current time */ |
146 |
+ |
FILE *fp |
147 |
+ |
) |
148 |
+ |
{ |
149 |
+ |
time_t tv; |
150 |
+ |
time(&tv); |
151 |
+ |
fputdate(tv, fp); |
152 |
+ |
} |
153 |
+ |
|
154 |
+ |
|
155 |
+ |
extern void |
156 |
+ |
printargs( /* print arguments to a file */ |
157 |
+ |
int ac, |
158 |
+ |
char **av, |
159 |
+ |
FILE *fp |
160 |
+ |
) |
161 |
+ |
{ |
162 |
|
while (ac-- > 0) { |
163 |
< |
if (index(*av, ' ') != NULL) { /* quote it */ |
164 |
< |
if (index(*av, '\'') != NULL) |
84 |
< |
quote = '"'; |
85 |
< |
else |
86 |
< |
quote = '\''; |
87 |
< |
putc(quote, fp); |
88 |
< |
fputs(*av++, fp); |
89 |
< |
putc(quote, fp); |
90 |
< |
} else |
91 |
< |
fputs(*av++, fp); |
92 |
< |
putc(ac ? ' ' : '\n', fp); |
163 |
> |
fputword(*av++, fp); |
164 |
> |
fputc(ac ? ' ' : '\n', fp); |
165 |
|
} |
166 |
|
} |
167 |
|
|
168 |
|
|
169 |
< |
int |
170 |
< |
formatval(r, s) /* get format value (return true if format) */ |
171 |
< |
register char *r; |
172 |
< |
register char *s; |
169 |
> |
extern int |
170 |
> |
formatval( /* get format value (return true if format) */ |
171 |
> |
char *r, |
172 |
> |
char *s |
173 |
> |
) |
174 |
|
{ |
175 |
< |
register char *cp = FMTSTR; |
175 |
> |
const char *cp = FMTSTR; |
176 |
|
|
177 |
|
while (*cp) if (*cp++ != *s++) return(0); |
178 |
|
while (isspace(*s)) s++; |
186 |
|
} |
187 |
|
|
188 |
|
|
189 |
< |
int |
190 |
< |
isformat(s) /* is line a format line? */ |
191 |
< |
char *s; |
189 |
> |
extern void |
190 |
> |
fputformat( /* put out a format value */ |
191 |
> |
char *s, |
192 |
> |
FILE *fp |
193 |
> |
) |
194 |
|
{ |
120 |
– |
return(formatval(NULL, s)); |
121 |
– |
} |
122 |
– |
|
123 |
– |
|
124 |
– |
fputformat(s, fp) /* put out a format value */ |
125 |
– |
char *s; |
126 |
– |
FILE *fp; |
127 |
– |
{ |
195 |
|
fputs(FMTSTR, fp); |
196 |
|
fputs(s, fp); |
197 |
|
putc('\n', fp); |
198 |
|
} |
199 |
|
|
200 |
|
|
201 |
< |
int |
202 |
< |
getheader(fp, f, p) /* get header from file */ |
203 |
< |
FILE *fp; |
204 |
< |
int (*f)(); |
205 |
< |
char *p; |
201 |
> |
extern int |
202 |
> |
getheader( /* get header from file */ |
203 |
> |
FILE *fp, |
204 |
> |
gethfunc *f, |
205 |
> |
void *p |
206 |
> |
) |
207 |
|
{ |
208 |
|
char buf[MAXLINE]; |
209 |
|
|
233 |
|
}; |
234 |
|
|
235 |
|
|
236 |
< |
static |
237 |
< |
mycheck(s, cp) /* check a header line for format info. */ |
238 |
< |
char *s; |
239 |
< |
register struct check *cp; |
236 |
> |
static int |
237 |
> |
mycheck( /* check a header line for format info. */ |
238 |
> |
char *s, |
239 |
> |
void *cp |
240 |
> |
) |
241 |
|
{ |
242 |
< |
if (!formatval(cp->fs, s) && cp->fp != NULL) |
243 |
< |
fputs(s, cp->fp); |
242 |
> |
if (!formatval(((struct check*)cp)->fs, s) |
243 |
> |
&& ((struct check*)cp)->fp != NULL) { |
244 |
> |
fputs(s, ((struct check*)cp)->fp); |
245 |
> |
} |
246 |
> |
return(0); |
247 |
|
} |
248 |
|
|
249 |
|
|
250 |
< |
int |
251 |
< |
globmatch(pat, str) /* check for glob match of str against pat */ |
252 |
< |
char *pat, *str; |
250 |
> |
extern int |
251 |
> |
globmatch( /* check for match of s against pattern p */ |
252 |
> |
char *p, |
253 |
> |
char *s |
254 |
> |
) |
255 |
|
{ |
256 |
< |
register char *p = pat, *s = str; |
256 |
> |
int setmatch; |
257 |
|
|
258 |
|
do { |
259 |
|
switch (*p) { |
269 |
|
return(1); |
270 |
|
while (*s++); |
271 |
|
return(0); |
272 |
+ |
case '[': /* character set */ |
273 |
+ |
setmatch = *s == *++p; |
274 |
+ |
if (!*p) |
275 |
+ |
return(0); |
276 |
+ |
while (*++p != ']') { |
277 |
+ |
if (!*p) |
278 |
+ |
return(0); |
279 |
+ |
if (*p == '-') { |
280 |
+ |
setmatch += p[-1] <= *s && *s <= p[1]; |
281 |
+ |
if (!*++p) |
282 |
+ |
break; |
283 |
+ |
} else |
284 |
+ |
setmatch += *p == *s; |
285 |
+ |
} |
286 |
+ |
if (!setmatch) |
287 |
+ |
return(0); |
288 |
+ |
s++; |
289 |
+ |
break; |
290 |
|
case '\\': /* literal next */ |
291 |
|
p++; |
292 |
|
/* fall through */ |
315 |
|
* if fout is not NULL. |
316 |
|
*/ |
317 |
|
|
318 |
< |
int |
319 |
< |
checkheader(fin, fmt, fout) |
320 |
< |
FILE *fin; |
321 |
< |
char *fmt; |
322 |
< |
FILE *fout; |
318 |
> |
extern int |
319 |
> |
checkheader( |
320 |
> |
FILE *fin, |
321 |
> |
char *fmt, |
322 |
> |
FILE *fout |
323 |
> |
) |
324 |
|
{ |
325 |
|
struct check cdat; |
326 |
< |
register char *cp; |
326 |
> |
char *cp; |
327 |
|
|
328 |
|
cdat.fp = fout; |
329 |
|
cdat.fs[0] = '\0'; |
332 |
|
if (!cdat.fs[0]) |
333 |
|
return(0); |
334 |
|
for (cp = fmt; *cp; cp++) /* check for globbing */ |
335 |
< |
if (*cp == '?' | *cp == '*') |
335 |
> |
if ((*cp == '?') | (*cp == '*')) { |
336 |
|
if (globmatch(fmt, cdat.fs)) { |
337 |
|
strcpy(fmt, cdat.fs); |
338 |
|
return(1); |
339 |
|
} else |
340 |
|
return(-1); |
341 |
+ |
} |
342 |
|
return(strcmp(fmt, cdat.fs) ? -1 : 1); /* literal match */ |
343 |
|
} |