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