| 1 |
– |
/* Copyright (c) 1991 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 |
< |
* printargs(ac,av,fp) print an argument list to fp, followed by '\n' |
| 10 |
< |
* isformat(s) returns true if s is of the form "FORMAT=*" |
| 9 |
> |
* newheader(t,fp) start new information header identified by string t |
| 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 |
|
* 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 |
| 19 |
+ |
* globmatch(pat, str) check for glob match of str against pat |
| 20 |
|
* checkheader(i,p,o) check header format from i against p and copy to o |
| 21 |
|
* |
| 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 |
< |
char FMTSTR[] = "FORMAT="; |
| 28 |
< |
int FMTSTRL = 7; |
| 32 |
> |
#define MAXLINE 2048 |
| 33 |
|
|
| 34 |
+ |
const char HDRSTR[] = "#?"; /* information header magic number */ |
| 35 |
|
|
| 36 |
< |
printargs(ac, av, fp) /* print arguments to a file */ |
| 37 |
< |
int ac; |
| 38 |
< |
char **av; |
| 39 |
< |
FILE *fp; |
| 36 |
> |
const char FMTSTR[] = "FORMAT="; /* format identifier */ |
| 37 |
> |
|
| 38 |
> |
const char TMSTR[] = "CAPDATE="; /* capture date identifier */ |
| 39 |
> |
const char GMTSTR[] = "GMT="; /* GMT identifier */ |
| 40 |
> |
|
| 41 |
> |
static gethfunc mycheck; |
| 42 |
> |
|
| 43 |
> |
|
| 44 |
> |
extern void |
| 45 |
> |
newheader( /* identifying line of information header */ |
| 46 |
> |
char *s, |
| 47 |
> |
FILE *fp |
| 48 |
> |
) |
| 49 |
|
{ |
| 50 |
< |
while (ac-- > 0) { |
| 51 |
< |
fputs(*av++, fp); |
| 38 |
< |
putc(' ', fp); |
| 39 |
< |
} |
| 50 |
> |
fputs(HDRSTR, fp); |
| 51 |
> |
fputs(s, fp); |
| 52 |
|
putc('\n', fp); |
| 53 |
|
} |
| 54 |
|
|
| 55 |
|
|
| 56 |
< |
isformat(s) /* is line a format line? */ |
| 57 |
< |
char *s; |
| 56 |
> |
extern int |
| 57 |
> |
headidval( /* get header id (return true if is id) */ |
| 58 |
> |
char *r, |
| 59 |
> |
char *s |
| 60 |
> |
) |
| 61 |
|
{ |
| 62 |
< |
return(!strncmp(s,FMTSTR,FMTSTRL)); |
| 62 |
> |
const char *cp = HDRSTR; |
| 63 |
> |
|
| 64 |
> |
while (*cp) if (*cp++ != *s++) return(0); |
| 65 |
> |
if (r == NULL) return(1); |
| 66 |
> |
while (*s && !isspace(*s)) *r++ = *s++; |
| 67 |
> |
*r = '\0'; |
| 68 |
> |
return(1); |
| 69 |
|
} |
| 70 |
|
|
| 71 |
|
|
| 72 |
< |
formatval(r, s) /* return format value */ |
| 73 |
< |
register char *r; |
| 74 |
< |
register char *s; |
| 72 |
> |
extern int |
| 73 |
> |
dateval( /* convert capture date line to UTC */ |
| 74 |
> |
time_t *tloc, |
| 75 |
> |
char *s |
| 76 |
> |
) |
| 77 |
|
{ |
| 78 |
< |
s += FMTSTRL; |
| 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) { *r = '\0'; return; } |
| 84 |
< |
while(*s) *r++ = *s++; |
| 85 |
< |
while (isspace(r[-1])) r--; |
| 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 |
> |
extern int |
| 99 |
> |
gmtval( /* convert GMT date line to UTC */ |
| 100 |
> |
time_t *tloc, |
| 101 |
> |
char *s |
| 102 |
> |
) |
| 103 |
> |
{ |
| 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 |
> |
fputword(*av++, fp); |
| 164 |
> |
fputc(ac ? ' ' : '\n', fp); |
| 165 |
> |
} |
| 166 |
> |
} |
| 167 |
> |
|
| 168 |
> |
|
| 169 |
> |
extern int |
| 170 |
> |
formatval( /* get format value (return true if format) */ |
| 171 |
> |
char *r, |
| 172 |
> |
char *s |
| 173 |
> |
) |
| 174 |
> |
{ |
| 175 |
> |
const char *cp = FMTSTR; |
| 176 |
> |
|
| 177 |
> |
while (*cp) if (*cp++ != *s++) return(0); |
| 178 |
> |
while (isspace(*s)) s++; |
| 179 |
> |
if (!*s) return(0); |
| 180 |
> |
if (r == NULL) return(1); |
| 181 |
> |
do |
| 182 |
> |
*r++ = *s++; |
| 183 |
> |
while(*s && !isspace(*s)); |
| 184 |
|
*r = '\0'; |
| 185 |
+ |
return(1); |
| 186 |
|
} |
| 187 |
|
|
| 188 |
|
|
| 189 |
< |
fputformat(s, fp) /* put out a format value */ |
| 190 |
< |
char *s; |
| 191 |
< |
FILE *fp; |
| 189 |
> |
extern void |
| 190 |
> |
fputformat( /* put out a format value */ |
| 191 |
> |
char *s, |
| 192 |
> |
FILE *fp |
| 193 |
> |
) |
| 194 |
|
{ |
| 195 |
|
fputs(FMTSTR, fp); |
| 196 |
|
fputs(s, fp); |
| 198 |
|
} |
| 199 |
|
|
| 200 |
|
|
| 201 |
< |
getheader(fp, f, p) /* get header from file */ |
| 202 |
< |
FILE *fp; |
| 203 |
< |
int (*f)(); |
| 204 |
< |
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 |
|
|
| 210 |
|
for ( ; ; ) { |
| 211 |
|
buf[MAXLINE-2] = '\n'; |
| 212 |
< |
if (fgets(buf, sizeof(buf), fp) == NULL) |
| 212 |
> |
if (fgets(buf, MAXLINE, fp) == NULL) |
| 213 |
|
return(-1); |
| 214 |
|
if (buf[0] == '\n') |
| 215 |
|
return(0); |
| 216 |
+ |
#ifdef MSDOS |
| 217 |
+ |
if (buf[0] == '\r' && buf[1] == '\n') |
| 218 |
+ |
return(0); |
| 219 |
+ |
#endif |
| 220 |
|
if (buf[MAXLINE-2] != '\n') { |
| 221 |
|
ungetc(buf[MAXLINE-2], fp); /* prevent false end */ |
| 222 |
|
buf[MAXLINE-2] = '\0'; |
| 223 |
|
} |
| 224 |
< |
if (f != NULL) |
| 225 |
< |
(*f)(buf, p); |
| 224 |
> |
if (f != NULL && (*f)(buf, p) < 0) |
| 225 |
> |
return(-1); |
| 226 |
|
} |
| 227 |
|
} |
| 228 |
|
|
| 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 (!strncmp(s,FMTSTR,FMTSTRL)) |
| 243 |
< |
formatval(cp->fs, s); |
| 244 |
< |
else if (cp->fp != NULL) /* don't copy format info. */ |
| 245 |
< |
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 |
< |
/* |
| 251 |
< |
* Copymatch(pat,str) checks pat for wildcards, and |
| 252 |
< |
* copies str into pat if there is a match (returning true). |
| 253 |
< |
*/ |
| 254 |
< |
|
| 120 |
< |
#ifdef COPYMATCH |
| 121 |
< |
copymatch(pat, str) |
| 122 |
< |
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 |
< |
int docopy = 0; |
| 125 |
< |
register char *p = pat, *s = str; |
| 256 |
> |
int setmatch; |
| 257 |
|
|
| 258 |
|
do { |
| 259 |
|
switch (*p) { |
| 260 |
|
case '?': /* match any character */ |
| 261 |
|
if (!*s++) |
| 262 |
|
return(0); |
| 132 |
– |
docopy++; |
| 263 |
|
break; |
| 264 |
|
case '*': /* match any string */ |
| 265 |
|
while (p[1] == '*') p++; |
| 266 |
|
do |
| 267 |
< |
if ( (p[1]=='?' || p[1]==*s) |
| 268 |
< |
&& copymatch(p+1,s) ) { |
| 139 |
< |
strcpy(pat, str); |
| 267 |
> |
if ( (p[1]=='?' || p[1]==*s) && |
| 268 |
> |
globmatch(p+1,s) ) |
| 269 |
|
return(1); |
| 141 |
– |
} |
| 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 */ |
| 297 |
|
break; |
| 298 |
|
} |
| 299 |
|
} while (*p++); |
| 154 |
– |
if (docopy) |
| 155 |
– |
strcpy(pat, str); |
| 300 |
|
return(1); |
| 301 |
|
} |
| 158 |
– |
#else |
| 159 |
– |
#define copymatch(pat, s) (!strcmp(pat, s)) |
| 160 |
– |
#endif |
| 302 |
|
|
| 303 |
|
|
| 304 |
|
/* |
| 309 |
|
* if any input format is found (or there is an error), and 0 otherwise. |
| 310 |
|
* If fmt contains any '*' or '?' characters, then checkheader |
| 311 |
|
* does wildcard expansion and copies a matching result into fmt. |
| 312 |
< |
* Be sure that fmt is big enough to hold the match in such cases! |
| 312 |
> |
* Be sure that fmt is big enough to hold the match in such cases, |
| 313 |
> |
* and that it is not a static, read-only string! |
| 314 |
|
* The input header (minus any format lines) is copied to fout |
| 315 |
|
* if fout is not NULL. |
| 316 |
|
*/ |
| 317 |
|
|
| 318 |
< |
checkheader(fin, fmt, fout) |
| 319 |
< |
FILE *fin; |
| 320 |
< |
char *fmt; |
| 321 |
< |
FILE *fout; |
| 318 |
> |
extern int |
| 319 |
> |
checkheader( |
| 320 |
> |
FILE *fin, |
| 321 |
> |
char *fmt, |
| 322 |
> |
FILE *fout |
| 323 |
> |
) |
| 324 |
|
{ |
| 325 |
|
struct check cdat; |
| 326 |
+ |
char *cp; |
| 327 |
|
|
| 328 |
|
cdat.fp = fout; |
| 329 |
|
cdat.fs[0] = '\0'; |
| 330 |
|
if (getheader(fin, mycheck, &cdat) < 0) |
| 331 |
|
return(-1); |
| 332 |
< |
if (cdat.fs[0] != '\0') |
| 333 |
< |
return(copymatch(fmt, cdat.fs) ? 1 : -1); |
| 334 |
< |
return(0); |
| 332 |
> |
if (!cdat.fs[0]) |
| 333 |
> |
return(0); |
| 334 |
> |
for (cp = fmt; *cp; cp++) /* check for globbing */ |
| 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 |
|
} |