--- ray/src/common/header.c 2004/01/02 11:35:17 2.22 +++ ray/src/common/header.c 2018/08/02 18:33:42 2.32 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: header.c,v 2.22 2004/01/02 11:35:17 schorsch Exp $"; +static const char RCSid[] = "$Id: header.c,v 2.32 2018/08/02 18:33:42 greg Exp $"; #endif /* * header.c - routines for reading and writing information headers. @@ -7,14 +7,12 @@ static const char RCSid[] = "$Id: header.c,v 2.22 2004 * Externals declared in resolu.h * * newheader(t,fp) start new information header identified by string t - * isheadid(s) returns true if s is a header id line * headidval(r,s) copy header identifier value in s to r - * dateval(t,s) get capture date value - * isdate(s) returns true if s is a date line - * fputdate(t,fp) put out the given capture date and time + * dateval(t,s) get capture date value as UTC + * gmtval(t,s) get GMT as UTC + * fputdate(t,fp) put out the given UTC * fputnow(fp) put out the current date and time * printargs(ac,av,fp) print an argument list to fp, followed by '\n' - * isformat(s) returns true if s is of the form "FORMAT=*" * formatval(r,s) copy the format value in s to r * fputformat(s,fp) write "FORMAT=%s" to fp * getheader(fp,f,p) read header from fp, calling f(s,p) on each line @@ -26,27 +24,29 @@ static const char RCSid[] = "$Id: header.c,v 2.22 2004 #include "copyright.h" -#include #include #include "rtio.h" #include "resolu.h" -#define MAXLINE 512 +#define MAXLINE 2048 -char HDRSTR[] = "#?"; /* information header magic number */ +extern time_t timegm(struct tm *tm); -char FMTSTR[] = "FORMAT="; /* format identifier */ +const char HDRSTR[] = "#?"; /* information header magic number */ -char TMSTR[] = "CAPDATE="; /* capture date identifier */ +const char FMTSTR[] = "FORMAT="; /* format identifier */ +const char TMSTR[] = "CAPDATE="; /* capture date identifier */ +const char GMTSTR[] = "GMT="; /* GMT identifier */ + static gethfunc mycheck; -extern void +void newheader( /* identifying line of information header */ - char *s, - register FILE *fp + const char *s, + FILE *fp ) { fputs(HDRSTR, fp); @@ -55,13 +55,13 @@ newheader( /* identifying line of information header } -extern int +int headidval( /* get header id (return true if is id) */ - register char *r, - register char *s + char *r, + const char *s ) { - register char *cp = HDRSTR; + const char *cp = HDRSTR; while (*cp) if (*cp++ != *s++) return(0); if (r == NULL) return(1); @@ -71,23 +71,14 @@ headidval( /* get header id (return true if is id) * } -extern int -isheadid( /* check to see if line is header id */ - char *s -) -{ - return(headidval(NULL, s)); -} - - -extern int -dateval( /* get capture date value */ +int +dateval( /* convert capture date line to UTC */ time_t *tloc, - char *s + const char *s ) { struct tm tms; - register char *cp = TMSTR; + const char *cp = TMSTR; while (*cp) if (*cp++ != *s++) return(0); while (isspace(*s)) s++; @@ -106,31 +97,53 @@ dateval( /* get capture date value */ } -extern int -isdate( /* is the given line a capture date? */ - char *s +int +gmtval( /* convert GMT date line to UTC */ + time_t *tloc, + const char *s ) { - return(dateval(NULL, s)); + struct tm tms; + const char *cp = GMTSTR; + + while (*cp) if (*cp++ != *s++) return(0); + while (isspace(*s)) s++; + if (!*s) return(0); + if (sscanf(s, "%d:%d:%d %d:%d:%d", + &tms.tm_year, &tms.tm_mon, &tms.tm_mday, + &tms.tm_hour, &tms.tm_min, &tms.tm_sec) != 6) + return(0); + if (tloc == NULL) + return(1); + tms.tm_mon--; + tms.tm_year -= 1900; + *tloc = timegm(&tms); + return(1); } -extern void -fputdate( /* write out the given time value */ +void +fputdate( /* write out the given time value (local & GMT) */ time_t tv, FILE *fp ) { - struct tm *tm = localtime(&tv); - if (tm == NULL) - return; - fprintf(fp, "%s %04d:%02d:%02d %02d:%02d:%02d\n", TMSTR, - tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday, - tm->tm_hour, tm->tm_min, tm->tm_sec); + struct tm *tms; + + tms = localtime(&tv); + if (tms != NULL) + fprintf(fp, "%s %04d:%02d:%02d %02d:%02d:%02d\n", TMSTR, + tms->tm_year+1900, tms->tm_mon+1, tms->tm_mday, + tms->tm_hour, tms->tm_min, tms->tm_sec); + tms = gmtime(&tv); + if (tms != NULL) + fprintf(fp, "%s %04d:%02d:%02d %02d:%02d:%02d\n", GMTSTR, + tms->tm_year+1900, tms->tm_mon+1, tms->tm_mday, + tms->tm_hour, tms->tm_min, tms->tm_sec); } -extern void +void fputnow( /* write out the current time */ FILE *fp ) @@ -141,7 +154,7 @@ fputnow( /* write out the current time */ } -extern void +void printargs( /* print arguments to a file */ int ac, char **av, @@ -155,13 +168,14 @@ printargs( /* print arguments to a file */ } -extern int +int formatval( /* get format value (return true if format) */ - register char *r, - register char *s + char fmt[MAXFMTLEN], + const char *s ) { - register char *cp = FMTSTR; + const char *cp = FMTSTR; + char *r = fmt; while (*cp) if (*cp++ != *s++) return(0); while (isspace(*s)) s++; @@ -169,24 +183,15 @@ formatval( /* get format value (return true if forma if (r == NULL) return(1); do *r++ = *s++; - while(*s && !isspace(*s)); + while (*s && !isspace(*s) && r-fmt < MAXFMTLEN-1); *r = '\0'; return(1); } -extern int -isformat( /* is line a format line? */ - char *s -) -{ - return(formatval(NULL, s)); -} - - -extern void +void fputformat( /* put out a format value */ - char *s, + const char *s, FILE *fp ) { @@ -196,38 +201,37 @@ fputformat( /* put out a format value */ } -extern int +int getheader( /* get header from file */ FILE *fp, gethfunc *f, void *p ) { + int rtotal = 0; char buf[MAXLINE]; for ( ; ; ) { + int rval = 0; buf[MAXLINE-2] = '\n'; if (fgets(buf, MAXLINE, fp) == NULL) return(-1); - if (buf[0] == '\n') - return(0); -#ifdef MSDOS - if (buf[0] == '\r' && buf[1] == '\n') - return(0); -#endif + if (buf[buf[0]=='\r'] == '\n') + return(rtotal); if (buf[MAXLINE-2] != '\n') { ungetc(buf[MAXLINE-2], fp); /* prevent false end */ buf[MAXLINE-2] = '\0'; } - if (f != NULL && (*f)(buf, p) < 0) + if (f != NULL && (rval = (*f)(buf, p)) < 0) return(-1); + rtotal += rval; } } struct check { FILE *fp; - char fs[64]; + char fs[MAXFMTLEN]; }; @@ -245,10 +249,10 @@ mycheck( /* check a header line for format info. */ } -extern int +int globmatch( /* check for match of s against pattern p */ - register char *p, - register char *s + const char *p, + const char *s ) { int setmatch; @@ -262,7 +266,7 @@ globmatch( /* check for match of s against pattern p case '*': /* match any string */ while (p[1] == '*') p++; do - if ( (p[1]=='?' || p[1]==*s) && + if ( (p[1]=='?') | (p[1]==*s) && globmatch(p+1,s) ) return(1); while (*s++); @@ -275,11 +279,11 @@ globmatch( /* check for match of s against pattern p if (!*p) return(0); if (*p == '-') { - setmatch += p[-1] <= *s && *s <= p[1]; + setmatch += (p[-1] <= *s && *s <= p[1]); if (!*++p) break; } else - setmatch += *p == *s; + setmatch += (*p == *s); } if (!setmatch) return(0); @@ -313,15 +317,15 @@ globmatch( /* check for match of s against pattern p * if fout is not NULL. */ -extern int +int checkheader( FILE *fin, - char *fmt, + char fmt[MAXFMTLEN], FILE *fout ) { struct check cdat; - register char *cp; + char *cp; cdat.fp = fout; cdat.fs[0] = '\0';