|
|
1.1 ! root 1: #ifndef lint ! 2: static char sccsid[] = "@(#)zdump.c 1.2 (Berkeley) 10/22/87"; ! 3: #endif ! 4: ! 5: #include "stdio.h" ! 6: ! 7: #include "sys/types.h" ! 8: #include "tzfile.h" ! 9: #include "time.h" ! 10: ! 11: #ifndef TRUE ! 12: #define TRUE 1 ! 13: #define FALSE 0 ! 14: #endif ! 15: ! 16: extern char * asctime(); ! 17: extern char ** environ; ! 18: extern struct tm * gmtime(); ! 19: extern char * imalloc(); ! 20: extern char * optarg; ! 21: extern int optind; ! 22: extern long time(); ! 23: extern char * tzname[2]; ! 24: extern void tzset(); ! 25: ! 26: /* ! 27: ** For the benefit of cyntax... ! 28: */ ! 29: ! 30: static long tzdecode(); ! 31: static readerr(); ! 32: static show(); ! 33: ! 34: static int longest; ! 35: ! 36: static long ! 37: tzdecode(codep) ! 38: char * codep; ! 39: { ! 40: register int i; ! 41: register long result; ! 42: ! 43: result = 0; ! 44: for (i = 0; i < 4; ++i) ! 45: result = (result << 8) | (codep[i] & 0xff); ! 46: return result; ! 47: } ! 48: ! 49: main(argc, argv) ! 50: int argc; ! 51: char * argv[]; ! 52: { ! 53: register FILE * fp; ! 54: register int i, j, c; ! 55: register int vflag; ! 56: register char * cutoff; ! 57: register int cutyear; ! 58: register long cuttime; ! 59: time_t now; ! 60: time_t t; ! 61: long timecnt; ! 62: char buf[BUFSIZ]; ! 63: ! 64: vflag = 0; ! 65: cutoff = NULL; ! 66: while ((c = getopt(argc, argv, "c:v")) == 'c' || c == 'v') ! 67: if (c == 'v') ! 68: vflag = 1; ! 69: else cutoff = optarg; ! 70: if (c != EOF || optind == argc - 1 && strcmp(argv[optind], "=") == 0) { ! 71: (void) fprintf(stderr, "%s: usage is %s [ -v ] zonename ...\n", ! 72: argv[0], argv[0]); ! 73: exit(1); ! 74: } ! 75: if (cutoff != NULL) ! 76: cutyear = atoi(cutoff); ! 77: /* ! 78: ** VERY approximate. ! 79: */ ! 80: cuttime = (long) (cutyear - EPOCH_YEAR) * ! 81: SECS_PER_HOUR * HOURS_PER_DAY * DAYS_PER_NYEAR; ! 82: (void) time(&now); ! 83: longest = 0; ! 84: for (i = optind; i < argc; ++i) ! 85: if (strlen(argv[i]) > longest) ! 86: longest = strlen(argv[i]); ! 87: for (i = optind; i < argc; ++i) { ! 88: register char ** saveenv; ! 89: char * tzequals; ! 90: char * fakeenv[2]; ! 91: ! 92: tzequals = imalloc(strlen(argv[i]) + 4); ! 93: if (tzequals == NULL) { ! 94: (void) fprintf(stderr, "%s: can't allocate memory\n", ! 95: argv[0]); ! 96: exit(1); ! 97: } ! 98: (void) sprintf(tzequals, "TZ=%s", argv[i]); ! 99: fakeenv[0] = tzequals; ! 100: fakeenv[1] = NULL; ! 101: saveenv = environ; ! 102: environ = fakeenv; ! 103: (void) tzset(); ! 104: environ = saveenv; ! 105: show(argv[i], now, FALSE); ! 106: if (!vflag) ! 107: continue; ! 108: if (argv[i][0] == '/') ! 109: fp = fopen(argv[i], "r"); ! 110: else { ! 111: j = strlen(TZDIR) + 1 + strlen(argv[i]) + 1; ! 112: if (j > sizeof buf) { ! 113: (void) fprintf(stderr, ! 114: "%s: timezone name %s/%s is too long\n", ! 115: argv[0], TZDIR, argv[i]); ! 116: exit(1); ! 117: } ! 118: (void) sprintf(buf, "%s/%s", TZDIR, argv[i]); ! 119: fp = fopen(buf, "r"); ! 120: } ! 121: if (fp == NULL) { ! 122: (void) fprintf(stderr, "%s: Can't open ", argv[0]); ! 123: perror(argv[i]); ! 124: exit(1); ! 125: } ! 126: { ! 127: char code[4]; ! 128: ! 129: (void) fseek(fp, (long) sizeof ((struct tzhead *) 0)->tzh_reserved, 0); ! 130: if (fread((char *) code, sizeof code, 1, fp) != 1) ! 131: readerr(fp, argv[0], argv[i]); ! 132: timecnt = tzdecode(code); ! 133: (void) fseek(fp, (long) (2 * sizeof code), 1); ! 134: } ! 135: t = 0x80000000; ! 136: if (t > 0) /* time_t is unsigned */ ! 137: t = 0; ! 138: show(argv[i], t, TRUE); ! 139: t += SECS_PER_HOUR * HOURS_PER_DAY; ! 140: show(argv[i], t, TRUE); ! 141: while (timecnt-- > 0) { ! 142: char code[4]; ! 143: ! 144: if (fread((char *) code, sizeof code, 1, fp) != 1) ! 145: readerr(fp, argv[0], argv[i]); ! 146: t = tzdecode(code); ! 147: if (cutoff != NULL && t > cuttime) ! 148: break; ! 149: show(argv[i], t - 1, TRUE); ! 150: show(argv[i], t, TRUE); ! 151: } ! 152: if (fclose(fp)) { ! 153: (void) fprintf(stderr, "%s: Error closing ", argv[0]); ! 154: perror(argv[i]); ! 155: exit(1); ! 156: } ! 157: t = 0xffffffff; ! 158: if (t < 0) /* time_t is signed */ ! 159: t = 0x7fffffff ; ! 160: t -= SECS_PER_HOUR * HOURS_PER_DAY; ! 161: show(argv[i], t, TRUE); ! 162: t += SECS_PER_HOUR * HOURS_PER_DAY; ! 163: show(argv[i], t, TRUE); ! 164: free(tzequals); ! 165: } ! 166: if (fflush(stdout) || ferror(stdout)) { ! 167: (void) fprintf(stderr, "%s: Error writing standard output ", ! 168: argv[0]); ! 169: perror("standard output"); ! 170: exit(1); ! 171: } ! 172: return 0; ! 173: } ! 174: ! 175: static ! 176: show(zone, t, v) ! 177: char * zone; ! 178: time_t t; ! 179: { ! 180: struct tm * tmp; ! 181: extern struct tm * localtime(); ! 182: ! 183: (void) printf("%-*s ", longest, zone); ! 184: if (v) ! 185: (void) printf("%.24s GMT = ", asctime(gmtime(&t))); ! 186: tmp = localtime(&t); ! 187: (void) printf("%.24s", asctime(tmp)); ! 188: if (*tzname[tmp->tm_isdst] != '\0') ! 189: (void) printf(" %s", tzname[tmp->tm_isdst]); ! 190: if (v) { ! 191: (void) printf(" isdst=%d", tmp->tm_isdst); ! 192: (void) printf(" gmtoff=%ld", tmp->tm_gmtoff); ! 193: } ! 194: (void) printf("\n"); ! 195: } ! 196: ! 197: static ! 198: readerr(fp, progname, filename) ! 199: FILE * fp; ! 200: char * progname; ! 201: char * filename; ! 202: { ! 203: (void) fprintf(stderr, "%s: Error reading ", progname); ! 204: if (ferror(fp)) ! 205: perror(filename); ! 206: else (void) fprintf(stderr, "%s: Premature EOF\n", filename); ! 207: exit(1); ! 208: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.