adjkerntz.c (882) | adjkerntz.c (1092) |
---|---|
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 * | 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 DEVELOPERS ``AS IS'' AND | 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; | 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 */ | 60 /* Avoid time_t here, can be unsigned long or worse */ |
61 long offset, oldoffset, utcsec, localsec, diff; | 61 long offset, oldoffset, utcsec, localsec, diff; |
62 time_t final_sec; | 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) { | 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, "Incorrect offset in %s\n", storage); | 102 fprintf(stderr, "Misformatted offset in %s\n", 103 storage); |
103 return 1; 104 } 105 (void) fclose(f); 106 } 107 else 108 oldoffset = 0; 109 110/****** Critical section, do all things as fast as possible ******/ 111 112 /* get local CMOS clock and possible kernel offset */ 113 if (gettimeofday(&tv, &tz)) { 114 perror("gettimeofday"); 115 return 1; 116 } 117 118 /* get the actual local timezone difference */ | 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 */ |
119 local = *localtime(&tv.tv_sec); 120 utc = *gmtime(&tv.tv_sec); | 120 initial_sec = tv.tv_sec; 121 local = *localtime(&initial_sec); 122 utc = *gmtime(&initial_sec); |
121 utc.tm_isdst = local.tm_isdst; /* Use current timezone for mktime(), */ 122 /* because it assumed local time */ 123 124 /* calculate local CMOS diff from GMT */ 125 126 utcsec = mktime(&utc); 127 localsec = mktime(&local); 128 if (utcsec == -1 || localsec == -1) { | 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) { |
129 fprintf(stderr, "Wrong initial hour to call\n"); | 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"); |
130 return 1; 131 } 132 offset = utcsec - localsec; 133 134 /* correct the kerneltime for this diffs */ 135 /* subtract kernel offset, if present, old offset too */ 136 137 diff = offset - tz.tz_minuteswest * 60 - oldoffset; --- 8 unchanged lines hidden (view full) --- 146 local = *localtime(&final_sec); 147 utc = *gmtime(&final_sec); 148 utc.tm_isdst = local.tm_isdst; /* Use current timezone for mktime(), */ 149 /* because it assumed local time */ 150 151 utcsec = mktime(&utc); 152 localsec = mktime(&local); 153 if (utcsec == -1 || localsec == -1) { | 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) { |
154 fprintf(stderr, "Wrong final hour to call\n"); | 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"); |
155 return 1; 156 } 157 offset = utcsec - localsec; 158 159 /* correct the kerneltime for this diffs */ 160 /* subtract kernel offset, if present, old offset too */ 161 162 diff = offset - tz.tz_minuteswest * 60 - oldoffset; --- 21 unchanged lines hidden (view full) --- 184 perror("settimeofday"); 185 return 1; 186 } 187 } 188 189/****** End of critical section ******/ 190 191 if (verbose) | 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) |
192 printf("Calculated zone offset diffs: %ld seconds\n", diff); | 205 printf("Calculated zone offset difference: %ld seconds\n", 206 diff); |
193 194 if (offset != oldoffset) { 195 (void) umask(022); 196 /* Save offset for next calls from crontab */ 197 if ((f = fopen(storage, "w")) == NULL) { 198 perror(storage); 199 return 1; 200 } 201 fprintf(f, "%ld\n", offset); 202 if (fclose(f)) { 203 perror(storage); 204 return 1; 205 } 206 } 207 208 return 0; 209} 210 | 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 |