1/* 2 * Copyright (C) 1993 by Andrew A. Chernov, Moscow, Russia. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * |
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND |
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY --- 29 unchanged lines hidden (view full) --- 52 53int main(argc, argv) 54 int argc; 55 char **argv; 56{ 57 struct tm local, utc; 58 struct timeval tv, *stv; 59 struct timezone tz, *stz; |
60 /* Avoid time_t here, can be unsigned long or worse */ |
61 long offset, oldoffset, utcsec, localsec, diff; |
62 time_t initial_sec, final_sec; |
63 int ch, init = -1, verbose = 0; 64 FILE *f; 65 66 while ((ch = getopt(argc, argv, "aiv")) != EOF) 67 switch((char)ch) { 68 case 'i': /* initial call, save offset */ 69 if (init != -1) 70 goto usage; --- 23 unchanged lines hidden (view full) --- 94 /* Restore saved offset */ 95 96 if (!init) { 97 if ((f = fopen(storage, "r")) == NULL) { 98 perror(storage); 99 return 1; 100 } 101 if (fscanf(f, "%ld", &oldoffset) != 1) { |
102 fprintf(stderr, "Misformatted offset in %s\n", 103 storage); |
104 return 1; 105 } 106 (void) fclose(f); 107 } 108 else 109 oldoffset = 0; 110 111/****** Critical section, do all things as fast as possible ******/ 112 113 /* get local CMOS clock and possible kernel offset */ 114 if (gettimeofday(&tv, &tz)) { 115 perror("gettimeofday"); 116 return 1; 117 } 118 119 /* get the actual local timezone difference */ |
120 initial_sec = tv.tv_sec; 121 local = *localtime(&initial_sec); 122 utc = *gmtime(&initial_sec); |
123 utc.tm_isdst = local.tm_isdst; /* Use current timezone for mktime(), */ 124 /* because it assumed local time */ 125 126 /* calculate local CMOS diff from GMT */ 127 128 utcsec = mktime(&utc); 129 localsec = mktime(&local); 130 if (utcsec == -1 || localsec == -1) { |
131 /* 132 * XXX user can only control local time, and it is 133 * unacceptable to fail here for -i. 2:30 am in the 134 * middle of the nonexistent hour means 3:30 am. 135 */ 136 fprintf(stderr, 137 "Nonexistent local time - try again in an hour\n"); |
138 return 1; 139 } 140 offset = utcsec - localsec; 141 142 /* correct the kerneltime for this diffs */ 143 /* subtract kernel offset, if present, old offset too */ 144 145 diff = offset - tz.tz_minuteswest * 60 - oldoffset; --- 8 unchanged lines hidden (view full) --- 154 local = *localtime(&final_sec); 155 utc = *gmtime(&final_sec); 156 utc.tm_isdst = local.tm_isdst; /* Use current timezone for mktime(), */ 157 /* because it assumed local time */ 158 159 utcsec = mktime(&utc); 160 localsec = mktime(&local); 161 if (utcsec == -1 || localsec == -1) { |
162 /* 163 * XXX as above. The user has even less control, 164 * but perhaps we never get here. 165 */ 166 fprintf(stderr, 167 "Nonexistent (final) local time - try again in an hour\n"); |
168 return 1; 169 } 170 offset = utcsec - localsec; 171 172 /* correct the kerneltime for this diffs */ 173 /* subtract kernel offset, if present, old offset too */ 174 175 diff = offset - tz.tz_minuteswest * 60 - oldoffset; --- 21 unchanged lines hidden (view full) --- 197 perror("settimeofday"); 198 return 1; 199 } 200 } 201 202/****** End of critical section ******/ 203 204 if (verbose) |
205 printf("Calculated zone offset difference: %ld seconds\n", 206 diff); |
207 208 if (offset != oldoffset) { 209 (void) umask(022); 210 /* Save offset for next calls from crontab */ 211 if ((f = fopen(storage, "w")) == NULL) { 212 perror(storage); 213 return 1; 214 } 215 fprintf(f, "%ld\n", offset); 216 if (fclose(f)) { 217 perror(storage); 218 return 1; 219 } 220 } 221 222 return 0; 223} 224 |