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 |
24 |
|
* To copy header from input to output, use getheader(fin, fputs, fout) |
25 |
|
*/ |
26 |
|
|
27 |
< |
#include <stdio.h> |
27 |
> |
#include "copyright.h" |
28 |
> |
|
29 |
> |
#include <time.h> |
30 |
|
#include <ctype.h> |
31 |
|
|
32 |
+ |
#include "rtio.h" |
33 |
+ |
#include "resolu.h" |
34 |
+ |
|
35 |
|
#define MAXLINE 512 |
36 |
|
|
31 |
– |
#ifndef BSD |
32 |
– |
#define index strchr |
33 |
– |
#endif |
34 |
– |
|
35 |
– |
extern char *index(); |
36 |
– |
|
37 |
|
char HDRSTR[] = "#?"; /* information header magic number */ |
38 |
|
|
39 |
|
char FMTSTR[] = "FORMAT="; /* format identifier */ |
40 |
|
|
41 |
+ |
char TMSTR[] = "CAPDATE="; /* capture date identifier */ |
42 |
|
|
43 |
+ |
static int mycheck(); |
44 |
+ |
|
45 |
+ |
|
46 |
+ |
void |
47 |
|
newheader(s, fp) /* identifying line of information header */ |
48 |
|
char *s; |
49 |
|
register FILE *fp; |
62 |
|
|
63 |
|
while (*cp) if (*cp++ != *s++) return(0); |
64 |
|
if (r == NULL) return(1); |
65 |
< |
while (*s) *r++ = *s++; |
65 |
> |
while (*s && !isspace(*s)) *r++ = *s++; |
66 |
|
*r = '\0'; |
67 |
|
return(1); |
68 |
|
} |
76 |
|
} |
77 |
|
|
78 |
|
|
79 |
+ |
int |
80 |
+ |
dateval(tloc, s) /* get capture date value */ |
81 |
+ |
time_t *tloc; |
82 |
+ |
char *s; |
83 |
+ |
{ |
84 |
+ |
struct tm tms; |
85 |
+ |
register char *cp = TMSTR; |
86 |
+ |
|
87 |
+ |
while (*cp) if (*cp++ != *s++) return(0); |
88 |
+ |
while (isspace(*s)) s++; |
89 |
+ |
if (!*s) return(0); |
90 |
+ |
if (sscanf(s, "%d:%d:%d %d:%d:%d", |
91 |
+ |
&tms.tm_year, &tms.tm_mon, &tms.tm_mday, |
92 |
+ |
&tms.tm_hour, &tms.tm_min, &tms.tm_sec) != 6) |
93 |
+ |
return(0); |
94 |
+ |
if (tloc == NULL) |
95 |
+ |
return(1); |
96 |
+ |
tms.tm_mon--; |
97 |
+ |
tms.tm_year -= 1900; |
98 |
+ |
tms.tm_isdst = -1; /* ask mktime() to figure out DST */ |
99 |
+ |
*tloc = mktime(&tms); |
100 |
+ |
return(1); |
101 |
+ |
} |
102 |
+ |
|
103 |
+ |
|
104 |
+ |
int |
105 |
+ |
isdate(s) /* is the given line a capture date? */ |
106 |
+ |
char *s; |
107 |
+ |
{ |
108 |
+ |
return(dateval(NULL, s)); |
109 |
+ |
} |
110 |
+ |
|
111 |
+ |
|
112 |
+ |
void |
113 |
+ |
fputdate(tv, fp) /* write out the given time value */ |
114 |
+ |
time_t tv; |
115 |
+ |
FILE *fp; |
116 |
+ |
{ |
117 |
+ |
struct tm *tm = localtime(&tv); |
118 |
+ |
if (tm == NULL) |
119 |
+ |
return; |
120 |
+ |
fprintf(fp, "%s %04d:%02d:%02d %02d:%02d:%02d\n", TMSTR, |
121 |
+ |
tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday, |
122 |
+ |
tm->tm_hour, tm->tm_min, tm->tm_sec); |
123 |
+ |
} |
124 |
+ |
|
125 |
+ |
|
126 |
+ |
void |
127 |
+ |
fputnow(fp) /* write out the current time */ |
128 |
+ |
FILE *fp; |
129 |
+ |
{ |
130 |
+ |
time_t tv; |
131 |
+ |
time(&tv); |
132 |
+ |
fputdate(tv, fp); |
133 |
+ |
} |
134 |
+ |
|
135 |
+ |
|
136 |
+ |
void |
137 |
|
printargs(ac, av, fp) /* print arguments to a file */ |
138 |
|
int ac; |
139 |
|
char **av; |
140 |
< |
register FILE *fp; |
140 |
> |
FILE *fp; |
141 |
|
{ |
79 |
– |
int quote; |
80 |
– |
|
142 |
|
while (ac-- > 0) { |
143 |
< |
if (index(*av, ' ') != NULL) { /* quote it */ |
144 |
< |
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); |
143 |
> |
fputword(*av++, fp); |
144 |
> |
fputc(ac ? ' ' : '\n', fp); |
145 |
|
} |
94 |
– |
putc('\n', fp); |
146 |
|
} |
147 |
|
|
148 |
|
|
173 |
|
} |
174 |
|
|
175 |
|
|
176 |
+ |
void |
177 |
|
fputformat(s, fp) /* put out a format value */ |
178 |
|
char *s; |
179 |
|
FILE *fp; |
187 |
|
int |
188 |
|
getheader(fp, f, p) /* get header from file */ |
189 |
|
FILE *fp; |
190 |
< |
int (*f)(); |
190 |
> |
int (*f)(char *, char *); |
191 |
|
char *p; |
192 |
|
{ |
193 |
|
char buf[MAXLINE]; |
206 |
|
ungetc(buf[MAXLINE-2], fp); /* prevent false end */ |
207 |
|
buf[MAXLINE-2] = '\0'; |
208 |
|
} |
209 |
< |
if (f != NULL) |
210 |
< |
(*f)(buf, p); |
209 |
> |
if (f != NULL && (*f)(buf, p) < 0) |
210 |
> |
return(-1); |
211 |
|
} |
212 |
|
} |
213 |
|
|
218 |
|
}; |
219 |
|
|
220 |
|
|
221 |
< |
static |
221 |
> |
static int |
222 |
|
mycheck(s, cp) /* check a header line for format info. */ |
223 |
|
char *s; |
224 |
|
register struct check *cp; |
225 |
|
{ |
226 |
|
if (!formatval(cp->fs, s) && cp->fp != NULL) |
227 |
|
fputs(s, cp->fp); |
228 |
+ |
return(0); |
229 |
|
} |
230 |
|
|
231 |
|
|
232 |
|
int |
233 |
< |
globmatch(pat, str) /* check for glob match of str against pat */ |
234 |
< |
char *pat, *str; |
233 |
> |
globmatch(p, s) /* check for match of s against pattern p */ |
234 |
> |
register char *p, *s; |
235 |
|
{ |
236 |
< |
register char *p = pat, *s = str; |
236 |
> |
int setmatch; |
237 |
|
|
238 |
|
do { |
239 |
|
switch (*p) { |
249 |
|
return(1); |
250 |
|
while (*s++); |
251 |
|
return(0); |
252 |
+ |
case '[': /* character set */ |
253 |
+ |
setmatch = *s == *++p; |
254 |
+ |
if (!*p) |
255 |
+ |
return(0); |
256 |
+ |
while (*++p != ']') { |
257 |
+ |
if (!*p) |
258 |
+ |
return(0); |
259 |
+ |
if (*p == '-') { |
260 |
+ |
setmatch += p[-1] <= *s && *s <= p[1]; |
261 |
+ |
if (!*++p) |
262 |
+ |
break; |
263 |
+ |
} else |
264 |
+ |
setmatch += *p == *s; |
265 |
+ |
} |
266 |
+ |
if (!setmatch) |
267 |
+ |
return(0); |
268 |
+ |
s++; |
269 |
+ |
break; |
270 |
|
case '\\': /* literal next */ |
271 |
|
p++; |
272 |
|
/* fall through */ |
289 |
|
* if any input format is found (or there is an error), and 0 otherwise. |
290 |
|
* If fmt contains any '*' or '?' characters, then checkheader |
291 |
|
* does wildcard expansion and copies a matching result into fmt. |
292 |
< |
* Be sure that fmt is big enough to hold the match in such cases! |
292 |
> |
* Be sure that fmt is big enough to hold the match in such cases, |
293 |
> |
* and that it is not a static, read-only string! |
294 |
|
* The input header (minus any format lines) is copied to fout |
295 |
|
* if fout is not NULL. |
296 |
|
*/ |
302 |
|
FILE *fout; |
303 |
|
{ |
304 |
|
struct check cdat; |
305 |
+ |
register char *cp; |
306 |
|
|
307 |
|
cdat.fp = fout; |
308 |
|
cdat.fs[0] = '\0'; |
309 |
< |
if (getheader(fin, mycheck, &cdat) < 0) |
309 |
> |
if (getheader(fin, mycheck, (char *)&cdat) < 0) |
310 |
|
return(-1); |
311 |
|
if (!cdat.fs[0]) |
312 |
|
return(0); |
313 |
< |
if (globmatch(fmt, cdat.fs)) { |
314 |
< |
strcpy(fmt, cdat.fs); |
315 |
< |
return(1); |
316 |
< |
} |
317 |
< |
return(-1); |
313 |
> |
for (cp = fmt; *cp; cp++) /* check for globbing */ |
314 |
> |
if ((*cp == '?') | (*cp == '*')) { |
315 |
> |
if (globmatch(fmt, cdat.fs)) { |
316 |
> |
strcpy(fmt, cdat.fs); |
317 |
> |
return(1); |
318 |
> |
} else |
319 |
> |
return(-1); |
320 |
> |
} |
321 |
> |
return(strcmp(fmt, cdat.fs) ? -1 : 1); /* literal match */ |
322 |
|
} |