1 |
– |
/* Copyright (c) 1994 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 |
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 |
+ |
* 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 |
|
* printargs(ac,av,fp) print an argument list to fp, followed by '\n' |
17 |
|
* 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 |
< |
* copymatch(pat, str) copy str into pat if glob match |
21 |
> |
* globmatch(pat, str) check for glob match of str against pat |
22 |
|
* 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 |
|
*/ |
26 |
|
|
27 |
+ |
#include "copyright.h" |
28 |
+ |
|
29 |
|
#include <stdio.h> |
30 |
+ |
#include <string.h> |
31 |
+ |
#include <time.h> |
32 |
|
#include <ctype.h> |
33 |
|
|
34 |
|
#define MAXLINE 512 |
43 |
|
|
44 |
|
char FMTSTR[] = "FORMAT="; /* format identifier */ |
45 |
|
|
46 |
+ |
char TMSTR[] = "CAPDATE="; /* capture date identifier */ |
47 |
|
|
48 |
+ |
static int mycheck(); |
49 |
+ |
|
50 |
+ |
|
51 |
+ |
void |
52 |
|
newheader(s, fp) /* identifying line of information header */ |
53 |
|
char *s; |
54 |
|
register FILE *fp; |
67 |
|
|
68 |
|
while (*cp) if (*cp++ != *s++) return(0); |
69 |
|
if (r == NULL) return(1); |
70 |
< |
while (*s) *r++ = *s++; |
70 |
> |
while (*s && !isspace(*s)) *r++ = *s++; |
71 |
|
*r = '\0'; |
72 |
|
return(1); |
73 |
|
} |
81 |
|
} |
82 |
|
|
83 |
|
|
84 |
+ |
int |
85 |
+ |
dateval(tloc, s) /* get capture date value */ |
86 |
+ |
time_t *tloc; |
87 |
+ |
char *s; |
88 |
+ |
{ |
89 |
+ |
struct tm tms; |
90 |
+ |
register char *cp = TMSTR; |
91 |
+ |
|
92 |
+ |
while (*cp) if (*cp++ != *s++) return(0); |
93 |
+ |
while (isspace(*s)) s++; |
94 |
+ |
if (!*s) return(0); |
95 |
+ |
if (sscanf(s, "%d:%d:%d %d:%d:%d", |
96 |
+ |
&tms.tm_year, &tms.tm_mon, &tms.tm_mday, |
97 |
+ |
&tms.tm_hour, &tms.tm_min, &tms.tm_sec) != 6) |
98 |
+ |
return(0); |
99 |
+ |
if (tloc == NULL) |
100 |
+ |
return(1); |
101 |
+ |
tms.tm_mon--; |
102 |
+ |
tms.tm_year -= 1900; |
103 |
+ |
tms.tm_isdst = -1; /* ask mktime() to figure out DST */ |
104 |
+ |
*tloc = mktime(&tms); |
105 |
+ |
return(1); |
106 |
+ |
} |
107 |
+ |
|
108 |
+ |
|
109 |
+ |
int |
110 |
+ |
isdate(s) /* is the given line a capture date? */ |
111 |
+ |
char *s; |
112 |
+ |
{ |
113 |
+ |
return(dateval(NULL, s)); |
114 |
+ |
} |
115 |
+ |
|
116 |
+ |
|
117 |
+ |
void |
118 |
+ |
fputdate(tv, fp) /* write out the given time value */ |
119 |
+ |
time_t tv; |
120 |
+ |
FILE *fp; |
121 |
+ |
{ |
122 |
+ |
struct tm *tm = localtime(&tv); |
123 |
+ |
if (tm == NULL) |
124 |
+ |
return; |
125 |
+ |
fprintf(fp, "%s %04d:%02d:%02d %02d:%02d:%02d\n", TMSTR, |
126 |
+ |
tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday, |
127 |
+ |
tm->tm_hour, tm->tm_min, tm->tm_sec); |
128 |
+ |
} |
129 |
+ |
|
130 |
+ |
|
131 |
+ |
void |
132 |
+ |
fputnow(fp) /* write out the current time */ |
133 |
+ |
FILE *fp; |
134 |
+ |
{ |
135 |
+ |
time_t tv; |
136 |
+ |
time(&tv); |
137 |
+ |
fputdate(tv, fp); |
138 |
+ |
} |
139 |
+ |
|
140 |
+ |
|
141 |
+ |
void |
142 |
|
printargs(ac, av, fp) /* print arguments to a file */ |
143 |
|
int ac; |
144 |
|
char **av; |
145 |
< |
register FILE *fp; |
145 |
> |
FILE *fp; |
146 |
|
{ |
147 |
|
int quote; |
148 |
|
|
149 |
|
while (ac-- > 0) { |
150 |
< |
if (index(*av, ' ') != NULL) { /* quote it */ |
151 |
< |
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(' ', fp); |
150 |
> |
fputword(*av++, fp); |
151 |
> |
fputc(ac ? ' ' : '\n', fp); |
152 |
|
} |
94 |
– |
putc('\n', fp); |
153 |
|
} |
154 |
|
|
155 |
|
|
180 |
|
} |
181 |
|
|
182 |
|
|
183 |
+ |
void |
184 |
|
fputformat(s, fp) /* put out a format value */ |
185 |
|
char *s; |
186 |
|
FILE *fp; |
213 |
|
ungetc(buf[MAXLINE-2], fp); /* prevent false end */ |
214 |
|
buf[MAXLINE-2] = '\0'; |
215 |
|
} |
216 |
< |
if (f != NULL) |
217 |
< |
(*f)(buf, p); |
216 |
> |
if (f != NULL && (*f)(buf, p) < 0) |
217 |
> |
return(-1); |
218 |
|
} |
219 |
|
} |
220 |
|
|
225 |
|
}; |
226 |
|
|
227 |
|
|
228 |
< |
static |
228 |
> |
static int |
229 |
|
mycheck(s, cp) /* check a header line for format info. */ |
230 |
|
char *s; |
231 |
|
register struct check *cp; |
232 |
|
{ |
233 |
|
if (!formatval(cp->fs, s) && cp->fp != NULL) |
234 |
|
fputs(s, cp->fp); |
235 |
+ |
return(0); |
236 |
|
} |
237 |
|
|
238 |
|
|
179 |
– |
/* |
180 |
– |
* Copymatch(pat,str) checks pat for wildcards, and |
181 |
– |
* copies str into pat if there is a match (returning true). |
182 |
– |
*/ |
183 |
– |
|
239 |
|
int |
240 |
< |
copymatch(pat, str) |
241 |
< |
char *pat, *str; |
240 |
> |
globmatch(p, s) /* check for match of s against pattern p */ |
241 |
> |
register char *p, *s; |
242 |
|
{ |
243 |
< |
int docopy = 0; |
189 |
< |
register char *p = pat, *s = str; |
243 |
> |
int setmatch; |
244 |
|
|
245 |
|
do { |
246 |
|
switch (*p) { |
247 |
|
case '?': /* match any character */ |
248 |
|
if (!*s++) |
249 |
|
return(0); |
196 |
– |
docopy++; |
250 |
|
break; |
251 |
|
case '*': /* match any string */ |
252 |
|
while (p[1] == '*') p++; |
253 |
|
do |
254 |
< |
if ( (p[1]=='?' || p[1]==*s) |
255 |
< |
&& copymatch(p+1,s) ) { |
203 |
< |
strcpy(pat, str); |
254 |
> |
if ( (p[1]=='?' || p[1]==*s) && |
255 |
> |
globmatch(p+1,s) ) |
256 |
|
return(1); |
205 |
– |
} |
257 |
|
while (*s++); |
258 |
|
return(0); |
259 |
+ |
case '[': /* character set */ |
260 |
+ |
setmatch = *s == *++p; |
261 |
+ |
if (!*p) |
262 |
+ |
return(0); |
263 |
+ |
while (*++p != ']') { |
264 |
+ |
if (!*p) |
265 |
+ |
return(0); |
266 |
+ |
if (*p == '-') { |
267 |
+ |
setmatch += p[-1] <= *s && *s <= p[1]; |
268 |
+ |
if (!*++p) |
269 |
+ |
break; |
270 |
+ |
} else |
271 |
+ |
setmatch += *p == *s; |
272 |
+ |
} |
273 |
+ |
if (!setmatch) |
274 |
+ |
return(0); |
275 |
+ |
s++; |
276 |
+ |
break; |
277 |
|
case '\\': /* literal next */ |
278 |
|
p++; |
279 |
|
/* fall through */ |
284 |
|
break; |
285 |
|
} |
286 |
|
} while (*p++); |
218 |
– |
if (docopy) |
219 |
– |
strcpy(pat, str); |
287 |
|
return(1); |
288 |
|
} |
289 |
|
|
296 |
|
* if any input format is found (or there is an error), and 0 otherwise. |
297 |
|
* If fmt contains any '*' or '?' characters, then checkheader |
298 |
|
* does wildcard expansion and copies a matching result into fmt. |
299 |
< |
* Be sure that fmt is big enough to hold the match in such cases! |
299 |
> |
* Be sure that fmt is big enough to hold the match in such cases, |
300 |
> |
* and that it is not a static, read-only string! |
301 |
|
* The input header (minus any format lines) is copied to fout |
302 |
|
* if fout is not NULL. |
303 |
|
*/ |
309 |
|
FILE *fout; |
310 |
|
{ |
311 |
|
struct check cdat; |
312 |
+ |
register char *cp; |
313 |
|
|
314 |
|
cdat.fp = fout; |
315 |
|
cdat.fs[0] = '\0'; |
316 |
|
if (getheader(fin, mycheck, &cdat) < 0) |
317 |
|
return(-1); |
318 |
< |
if (cdat.fs[0] != '\0') |
319 |
< |
return(copymatch(fmt, cdat.fs) ? 1 : -1); |
320 |
< |
return(0); |
318 |
> |
if (!cdat.fs[0]) |
319 |
> |
return(0); |
320 |
> |
for (cp = fmt; *cp; cp++) /* check for globbing */ |
321 |
> |
if (*cp == '?' | *cp == '*') |
322 |
> |
if (globmatch(fmt, cdat.fs)) { |
323 |
> |
strcpy(fmt, cdat.fs); |
324 |
> |
return(1); |
325 |
> |
} else |
326 |
> |
return(-1); |
327 |
> |
return(strcmp(fmt, cdat.fs) ? -1 : 1); /* literal match */ |
328 |
|
} |