| 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 "copyright.h" | 
| 28 | + |  | 
| 29 |  | #include  <stdio.h> | 
| 30 | + | #include  <string.h> | 
| 31 | + | #include  <time.h> | 
| 32 |  | #include  <ctype.h> | 
| 33 |  |  | 
| 34 | + | #include  "standard.h" | 
| 35 | + |  | 
| 36 |  | #define  MAXLINE        512 | 
| 37 |  |  | 
| 31 | – | #ifndef BSD | 
| 32 | – | #define  index  strchr | 
| 33 | – | #endif | 
| 34 | – |  | 
| 35 | – | extern char  *index(); | 
| 36 | – |  | 
| 38 |  | char  HDRSTR[] = "#?";          /* information header magic number */ | 
| 39 |  |  | 
| 40 |  | char  FMTSTR[] = "FORMAT=";     /* format identifier */ | 
| 41 |  |  | 
| 42 | + | char  TMSTR[] = "CAPDATE=";     /* capture date identifier */ | 
| 43 |  |  | 
| 44 | + | static int mycheck(); | 
| 45 | + |  | 
| 46 | + |  | 
| 47 | + | void | 
| 48 |  | newheader(s, fp)                /* identifying line of information header */ | 
| 49 |  | char  *s; | 
| 50 |  | register FILE  *fp; | 
| 63 |  |  | 
| 64 |  | while (*cp) if (*cp++ != *s++) return(0); | 
| 65 |  | if (r == NULL) return(1); | 
| 66 | < | while (*s) *r++ = *s++; | 
| 66 | > | while (*s && !isspace(*s)) *r++ = *s++; | 
| 67 |  | *r = '\0'; | 
| 68 |  | return(1); | 
| 69 |  | } | 
| 77 |  | } | 
| 78 |  |  | 
| 79 |  |  | 
| 80 | + | int | 
| 81 | + | dateval(tloc, s)                /* get capture date value */ | 
| 82 | + | time_t  *tloc; | 
| 83 | + | char    *s; | 
| 84 | + | { | 
| 85 | + | struct tm       tms; | 
| 86 | + | register char  *cp = TMSTR; | 
| 87 | + |  | 
| 88 | + | while (*cp) if (*cp++ != *s++) return(0); | 
| 89 | + | while (isspace(*s)) s++; | 
| 90 | + | if (!*s) return(0); | 
| 91 | + | if (sscanf(s, "%d:%d:%d %d:%d:%d", | 
| 92 | + | &tms.tm_year, &tms.tm_mon, &tms.tm_mday, | 
| 93 | + | &tms.tm_hour, &tms.tm_min, &tms.tm_sec) != 6) | 
| 94 | + | return(0); | 
| 95 | + | if (tloc == NULL) | 
| 96 | + | return(1); | 
| 97 | + | tms.tm_mon--; | 
| 98 | + | tms.tm_year -= 1900; | 
| 99 | + | tms.tm_isdst = -1;      /* ask mktime() to figure out DST */ | 
| 100 | + | *tloc = mktime(&tms); | 
| 101 | + | return(1); | 
| 102 | + | } | 
| 103 | + |  | 
| 104 | + |  | 
| 105 | + | int | 
| 106 | + | isdate(s)                       /* is the given line a capture date? */ | 
| 107 | + | char *s; | 
| 108 | + | { | 
| 109 | + | return(dateval(NULL, s)); | 
| 110 | + | } | 
| 111 | + |  | 
| 112 | + |  | 
| 113 | + | void | 
| 114 | + | fputdate(tv, fp)                /* write out the given time value */ | 
| 115 | + | time_t  tv; | 
| 116 | + | FILE    *fp; | 
| 117 | + | { | 
| 118 | + | struct tm       *tm = localtime(&tv); | 
| 119 | + | if (tm == NULL) | 
| 120 | + | return; | 
| 121 | + | fprintf(fp, "%s %04d:%02d:%02d %02d:%02d:%02d\n", TMSTR, | 
| 122 | + | tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday, | 
| 123 | + | tm->tm_hour, tm->tm_min, tm->tm_sec); | 
| 124 | + | } | 
| 125 | + |  | 
| 126 | + |  | 
| 127 | + | void | 
| 128 | + | fputnow(fp)                     /* write out the current time */ | 
| 129 | + | FILE    *fp; | 
| 130 | + | { | 
| 131 | + | time_t  tv; | 
| 132 | + | time(&tv); | 
| 133 | + | fputdate(tv, fp); | 
| 134 | + | } | 
| 135 | + |  | 
| 136 | + |  | 
| 137 | + | void | 
| 138 |  | printargs(ac, av, fp)           /* print arguments to a file */ | 
| 139 |  | int  ac; | 
| 140 |  | char  **av; | 
| 141 | < | register FILE  *fp; | 
| 141 | > | FILE  *fp; | 
| 142 |  | { | 
| 79 | – | int  quote; | 
| 80 | – |  | 
| 143 |  | while (ac-- > 0) { | 
| 144 | < | if (index(*av, ' ') != NULL) {          /* quote it */ | 
| 145 | < | 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); | 
| 144 | > | fputword(*av++, fp); | 
| 145 | > | fputc(ac ? ' ' : '\n', fp); | 
| 146 |  | } | 
| 94 | – | putc('\n', fp); | 
| 147 |  | } | 
| 148 |  |  | 
| 149 |  |  | 
| 174 |  | } | 
| 175 |  |  | 
| 176 |  |  | 
| 177 | + | void | 
| 178 |  | fputformat(s, fp)               /* put out a format value */ | 
| 179 |  | char  *s; | 
| 180 |  | FILE  *fp; | 
| 207 |  | ungetc(buf[MAXLINE-2], fp);     /* prevent false end */ | 
| 208 |  | buf[MAXLINE-2] = '\0'; | 
| 209 |  | } | 
| 210 | < | if (f != NULL) | 
| 211 | < | (*f)(buf, p); | 
| 210 | > | if (f != NULL && (*f)(buf, p) < 0) | 
| 211 | > | return(-1); | 
| 212 |  | } | 
| 213 |  | } | 
| 214 |  |  | 
| 219 |  | }; | 
| 220 |  |  | 
| 221 |  |  | 
| 222 | < | static | 
| 222 | > | static int | 
| 223 |  | mycheck(s, cp)                  /* check a header line for format info. */ | 
| 224 |  | char  *s; | 
| 225 |  | register struct check  *cp; | 
| 226 |  | { | 
| 227 |  | if (!formatval(cp->fs, s) && cp->fp != NULL) | 
| 228 |  | fputs(s, cp->fp); | 
| 229 | + | return(0); | 
| 230 |  | } | 
| 231 |  |  | 
| 232 |  |  | 
| 233 |  | int | 
| 234 | < | globmatch(pat, str)             /* check for glob match of str against pat */ | 
| 235 | < | char    *pat, *str; | 
| 234 | > | globmatch(p, s)                 /* check for match of s against pattern p */ | 
| 235 | > | register char   *p, *s; | 
| 236 |  | { | 
| 237 | < | register char   *p = pat, *s = str; | 
| 237 | > | int     setmatch; | 
| 238 |  |  | 
| 239 |  | do { | 
| 240 |  | switch (*p) { | 
| 250 |  | return(1); | 
| 251 |  | while (*s++); | 
| 252 |  | return(0); | 
| 253 | + | 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 |  | case '\\':                      /* literal next */ | 
| 272 |  | p++; | 
| 273 |  | /* fall through */ | 
| 290 |  | * 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 | < | * Be sure that fmt is big enough to hold the match in such cases! | 
| 293 | > | * 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 |  | * The input header (minus any format lines) is copied to fout | 
| 296 |  | * if fout is not NULL. | 
| 297 |  | */ | 
| 303 |  | FILE  *fout; | 
| 304 |  | { | 
| 305 |  | struct check    cdat; | 
| 306 | + | register char   *cp; | 
| 307 |  |  | 
| 308 |  | cdat.fp = fout; | 
| 309 |  | cdat.fs[0] = '\0'; | 
| 311 |  | return(-1); | 
| 312 |  | if (!cdat.fs[0]) | 
| 313 |  | return(0); | 
| 314 | < | if (globmatch(fmt, cdat.fs)) { | 
| 315 | < | strcpy(fmt, cdat.fs); | 
| 316 | < | return(1); | 
| 317 | < | } | 
| 318 | < | return(-1); | 
| 314 | > | 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 |  | } |