Import of original zoneinfo code and database - tzcode
[minix3.git] / commands / zoneinfo / date.c
blobf89259bf8886b7c193f521c3e33c243ea800fbf0
1 #ifndef lint
2 #ifndef NOID
3 static char elsieid[] = "@(#)date.c 7.45";
4 /*
5 ** Modified from the UCB version with the SCCS ID appearing below.
6 */
7 #endif /* !defined NOID */
8 #endif /* !defined lint */
11 * Copyright (c) 1985, 1987, 1988 The Regents of the University of California.
12 * All rights reserved.
14 * Redistribution and use in source and binary forms are permitted
15 * provided that the above copyright notice and this paragraph are
16 * duplicated in all such forms and that any documentation,
17 * advertising materials, and other materials related to such
18 * distribution and use acknowledge that the software was developed
19 * by the University of California, Berkeley. The name of the
20 * University may not be used to endorse or promote products derived
21 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
24 * WARRANTIES OF MERCHANT[A]BILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27 #ifndef lint
28 char copyright[] =
29 "@(#) Copyright (c) 1985, 1987, 1988 The Regents of the University of California.\n\
30 All rights reserved.\n";
31 #endif /* not lint */
33 #ifndef lint
34 static char sccsid[] = "@(#)date.c 4.23 (Berkeley) 9/20/88";
35 #endif /* not lint */
37 #include "private.h"
38 #if HAVE_ADJTIME || HAVE_SETTIMEOFDAY
39 #include "sys/time.h" /* for struct timeval, struct timezone */
40 #endif /* HAVE_ADJTIME || HAVE_SETTIMEOFDAY */
41 #include "locale.h"
42 #include "utmp.h" /* for OLD_TIME (or its absence) */
43 #if HAVE_UTMPX_H
44 #include "utmpx.h"
45 #endif
47 #ifndef OTIME_MSG
48 #define OTIME_MSG "old time"
49 #endif
50 #ifndef NTIME_MSG
51 #define NTIME_MSG "new time"
52 #endif
55 ** The two things date knows about time are. . .
58 #ifndef TM_YEAR_BASE
59 #define TM_YEAR_BASE 1900
60 #endif /* !defined TM_YEAR_BASE */
62 #ifndef SECSPERMIN
63 #define SECSPERMIN 60
64 #endif /* !defined SECSPERMIN */
66 extern double atof();
67 extern char ** environ;
68 extern char * getlogin();
69 extern time_t mktime();
70 extern char * optarg;
71 extern int optind;
72 extern char * strchr();
73 extern time_t time();
74 extern char * tzname[2];
76 static int retval = EXIT_SUCCESS;
78 static void checkfinal P((const char *, int, time_t, time_t));
79 static int comptm P((const struct tm *, const struct tm *));
80 static time_t convert P((const char *, int, time_t));
81 static void display P((const char *));
82 static void dogmt P((void));
83 static void errensure P((void));
84 static void iffy P((time_t, time_t, const char *, const char *));
85 int main P((int, char**));
86 static const char * nondigit P((const char *));
87 static void oops P((const char *));
88 static void reset P((time_t, int));
89 static void timeout P((FILE *, const char *, const struct tm *));
90 static void usage P((void));
91 static void wildinput P((const char *, const char *,
92 const char *));
94 int
95 main(argc, argv)
96 const int argc;
97 char * argv[];
99 register const char * format;
100 register const char * value;
101 register const char * cp;
102 register int ch;
103 register int dousg;
104 register int aflag = 0;
105 register int dflag = 0;
106 register int nflag = 0;
107 register int tflag = 0;
108 register int minuteswest;
109 register int dsttime;
110 register double adjust;
111 time_t now;
112 time_t t;
114 INITIALIZE(dousg);
115 INITIALIZE(minuteswest);
116 INITIALIZE(dsttime);
117 INITIALIZE(adjust);
118 INITIALIZE(t);
119 #ifdef LC_ALL
120 (void) setlocale(LC_ALL, "");
121 #endif /* defined(LC_ALL) */
122 #if HAVE_GETTEXT
123 #ifdef TZ_DOMAINDIR
124 (void) bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR);
125 #endif /* defined(TEXTDOMAINDIR) */
126 (void) textdomain(TZ_DOMAIN);
127 #endif /* HAVE_GETTEXT */
128 (void) time(&now);
129 format = value = NULL;
130 while ((ch = getopt(argc, argv, "ucnd:t:a:")) != EOF && ch != -1) {
131 switch (ch) {
132 default:
133 usage();
134 case 'u': /* do it in UTC */
135 case 'c':
136 dogmt();
137 break;
138 case 'n': /* don't set network */
139 nflag = 1;
140 break;
141 case 'd': /* daylight saving time */
142 if (dflag) {
143 (void) fprintf(stderr,
144 _("date: error: multiple -d's used"));
145 usage();
147 dflag = 1;
148 cp = optarg;
149 dsttime = atoi(cp);
150 if (*cp == '\0' || *nondigit(cp) != '\0')
151 wildinput(_("-t value"), optarg,
152 _("must be a non-negative number"));
153 break;
154 case 't': /* minutes west of UTC */
155 if (tflag) {
156 (void) fprintf(stderr,
157 _("date: error: multiple -t's used"));
158 usage();
160 tflag = 1;
161 cp = optarg;
162 minuteswest = atoi(cp);
163 if (*cp == '+' || *cp == '-')
164 ++cp;
165 if (*cp == '\0' || *nondigit(cp) != '\0')
166 wildinput(_("-d value"), optarg,
167 _("must be a number"));
168 break;
169 case 'a': /* adjustment */
170 if (aflag) {
171 (void) fprintf(stderr,
172 _("date: error: multiple -a's used"));
173 usage();
175 aflag = 1;
176 cp = optarg;
177 adjust = atof(cp);
178 if (*cp == '+' || *cp == '-')
179 ++cp;
180 if (*cp == '\0' || strcmp(cp, ".") == 0)
181 wildinput(_("-a value"), optarg,
182 _("must be a number"));
183 cp = nondigit(cp);
184 if (*cp == '.')
185 ++cp;
186 if (*nondigit(cp) != '\0')
187 wildinput(_("-a value"), optarg,
188 _("must be a number"));
189 break;
192 while (optind < argc) {
193 cp = argv[optind++];
194 if (*cp == '+')
195 if (format == NULL)
196 format = cp + 1;
197 else {
198 (void) fprintf(stderr,
199 _("date: error: multiple formats in command line\n"));
200 usage();
202 else if (value == NULL)
203 value = cp;
204 else {
205 (void) fprintf(stderr,
206 _("date: error: multiple values in command line\n"));
207 usage();
210 if (value != NULL) {
212 ** This order ensures that "reasonable" twelve-digit inputs
213 ** (such as 120203042006) won't be misinterpreted
214 ** even if time_t's range all the way back to the thirteenth
215 ** century. Do not change the order.
217 t = convert(value, (dousg = TRUE), now);
218 if (t == -1)
219 t = convert(value, (dousg = FALSE), now);
220 if (t == -1) {
222 ** Out of range values,
223 ** or time that falls in a DST transition hole?
225 if ((cp = strchr(value, '.')) != NULL) {
227 ** Ensure that the failure of
228 ** TZ=America/New_York date 8712312359.60
229 ** doesn't get misdiagnosed. (It was
230 ** TZ=America/New_York date 8712311859.60
231 ** when the leap second was inserted.)
232 ** The normal check won't work since
233 ** the given time is valid in UTC.
235 if (atoi(cp + 1) >= SECSPERMIN)
236 wildinput(_("time"), value,
237 _("out of range seconds given"));
239 dogmt();
240 t = convert(value, FALSE, now);
241 if (t == -1)
242 t = convert(value, TRUE, now);
243 wildinput(_("time"), value,
244 (t == -1) ?
245 _("out of range value given") :
246 _("time skipped when clock springs forward"));
250 ** Entire command line has now been checked.
252 if (aflag) {
253 #if HAVE_ADJTIME
254 struct timeval tv;
256 tv.tv_sec = (int) adjust;
257 tv.tv_usec = (int) ((adjust - tv.tv_sec) * 1000000L);
258 if (adjtime(&tv, (struct timeval *) NULL) != 0)
259 oops("adjtime");
260 #endif /* HAVE_ADJTIME */
261 #if !HAVE_ADJTIME
262 reset((time_t) (now + adjust), nflag);
263 #endif /* !HAVE_ADJTIME */
265 ** Sun silently ignores everything else; we follow suit.
267 exit(retval);
269 if (dflag || tflag) {
270 #if HAVE_SETTIMEOFDAY == 2
271 struct timezone tz;
273 if (!dflag || !tflag)
274 if (gettimeofday((struct timeval *) NULL, &tz) != 0)
275 oops("gettimeofday");
276 if (dflag)
277 tz.tz_dsttime = dsttime;
278 if (tflag)
279 tz.tz_minuteswest = minuteswest;
280 if (settimeofday((struct timeval *) NULL, &tz) != 0)
281 oops("settimeofday");
282 #endif /* HAVE_SETTIMEOFDAY == 2 */
283 #if HAVE_SETTIMEOFDAY != 2
284 (void) fprintf(stderr,
285 _("date: warning: kernel doesn't keep -d/-t information, option ignored\n"));
286 #endif /* HAVE_SETTIMEOFDAY != 2 */
289 if (value == NULL)
290 display(format);
292 reset(t, nflag);
294 checkfinal(value, dousg, t, now);
296 #ifdef EBUG
298 struct tm tm;
300 tm = *localtime(&t);
301 timeout(stdout, "%c\n", &tm);
302 exit(retval);
304 #endif /* defined EBUG */
306 display(format);
308 /* gcc -Wall pacifier */
309 for ( ; ; )
310 continue;
313 static void
314 dogmt()
316 static char ** fakeenv;
318 if (fakeenv == NULL) {
319 register int from;
320 register int to;
321 register int n;
322 static char tzegmt0[] = "TZ=GMT0";
324 for (n = 0; environ[n] != NULL; ++n)
325 continue;
326 fakeenv = (char **) malloc((size_t) (n + 2) * sizeof *fakeenv);
327 if (fakeenv == NULL) {
328 (void) perror(_("Memory exhausted"));
329 errensure();
330 exit(retval);
332 to = 0;
333 fakeenv[to++] = tzegmt0;
334 for (from = 1; environ[from] != NULL; ++from)
335 if (strncmp(environ[from], "TZ=", 3) != 0)
336 fakeenv[to++] = environ[from];
337 fakeenv[to] = NULL;
338 environ = fakeenv;
342 #ifdef OLD_TIME
345 ** We assume we're on a System-V-based system,
346 ** should use stime,
347 ** should write System-V-format utmp entries,
348 ** and don't have network notification to worry about.
351 #include "fcntl.h" /* for O_WRONLY, O_APPEND */
353 /*ARGSUSED*/
354 static void
355 #if __STDC__
356 reset(const time_t newt, const int nflag)
357 #else /* !__STDC__ */
358 reset(newt, nflag)
359 const time_t newt;
360 const int nflag;
361 #endif /* !__STDC__ */
363 register int fid;
364 time_t oldt;
365 static struct {
366 struct utmp before;
367 struct utmp after;
368 } s;
369 #if HAVE_UTMPX_H
370 static struct {
371 struct utmpx before;
372 struct utmpx after;
373 } sx;
374 #endif
377 ** Wouldn't it be great if stime returned the old time?
379 (void) time(&oldt);
380 if (stime(&newt) != 0)
381 oops("stime");
382 s.before.ut_type = OLD_TIME;
383 s.before.ut_time = oldt;
384 (void) strcpy(s.before.ut_line, OTIME_MSG);
385 s.after.ut_type = NEW_TIME;
386 s.after.ut_time = newt;
387 (void) strcpy(s.after.ut_line, NTIME_MSG);
388 fid = open(WTMP_FILE, O_WRONLY | O_APPEND);
389 if (fid < 0)
390 oops(_("log file open"));
391 if (write(fid, (char *) &s, sizeof s) != sizeof s)
392 oops(_("log file write"));
393 if (close(fid) != 0)
394 oops(_("log file close"));
395 #if !HAVE_UTMPX_H
396 pututline(&s.before);
397 pututline(&s.after);
398 #endif /* !HAVE_UTMPX_H */
399 #if HAVE_UTMPX_H
400 sx.before.ut_type = OLD_TIME;
401 sx.before.ut_tv.tv_sec = oldt;
402 (void) strcpy(sx.before.ut_line, OTIME_MSG);
403 sx.after.ut_type = NEW_TIME;
404 sx.after.ut_tv.tv_sec = newt;
405 (void) strcpy(sx.after.ut_line, NTIME_MSG);
406 #if !SUPPRESS_WTMPX_FILE_UPDATE
407 /* In Solaris 2.5 (and presumably other systems),
408 `date' does not update /var/adm/wtmpx.
409 This must be a bug. If you'd like to reproduce the bug,
410 define SUPPRESS_WTMPX_FILE_UPDATE to be nonzero. */
411 fid = open(WTMPX_FILE, O_WRONLY | O_APPEND);
412 if (fid < 0)
413 oops(_("log file open"));
414 if (write(fid, (char *) &sx, sizeof sx) != sizeof sx)
415 oops(_("log file write"));
416 if (close(fid) != 0)
417 oops(_("log file close"));
418 #endif /* !SUPPRESS_WTMPX_FILE_UPDATE */
419 pututxline(&sx.before);
420 pututxline(&sx.after);
421 #endif /* HAVE_UTMPX_H */
424 #endif /* defined OLD_TIME */
425 #ifndef OLD_TIME
428 ** We assume we're on a BSD-based system,
429 ** should use settimeofday,
430 ** should write BSD-format utmp entries (using logwtmp),
431 ** and may get to worry about network notification.
432 ** The "time name" changes between 4.3-tahoe and 4.4;
433 ** we include sys/param.h to determine which we should use.
436 #ifndef TIME_NAME
437 #include "sys/param.h"
438 #ifdef BSD4_4
439 #define TIME_NAME "date"
440 #endif /* defined BSD4_4 */
441 #ifndef BSD4_4
442 #define TIME_NAME ""
443 #endif /* !defined BSD4_4 */
444 #endif /* !defined TIME_NAME */
446 #include "syslog.h"
447 #include "sys/socket.h"
448 #include "netinet/in.h"
449 #include "netdb.h"
450 #define TSPTYPES
451 #include "protocols/timed.h"
453 extern int logwtmp();
455 #if HAVE_SETTIMEOFDAY == 1
456 #define settimeofday(t, tz) (settimeofday)(t)
457 #endif /* HAVE_SETTIMEOFDAY == 1 */
459 #ifndef TSP_SETDATE
460 /*ARGSUSED*/
461 #endif /* !defined TSP_SETDATE */
462 static void
463 reset(newt, nflag)
464 const time_t newt;
465 const int nflag;
467 register const char * username;
468 static struct timeval tv; /* static so tv_usec is 0 */
470 #ifdef EBUG
471 return;
472 #endif /* defined EBUG */
473 username = getlogin();
474 if (username == NULL || *username == '\0') /* single-user or no tty */
475 username = "root";
476 tv.tv_sec = newt;
477 #ifdef TSP_SETDATE
478 if (nflag || !netsettime(tv))
479 #endif /* defined TSP_SETDATE */
482 ** "old" entry is always written, for compatibility.
484 logwtmp("|", TIME_NAME, "");
485 if (settimeofday(&tv, (struct timezone *) NULL) == 0) {
486 logwtmp("{", TIME_NAME, ""); /* } */
487 syslog(LOG_AUTH | LOG_NOTICE, _("date set by %s"),
488 username);
489 } else oops("settimeofday");
493 #endif /* !defined OLD_TIME */
495 static void
496 wildinput(item, value, reason)
497 const char * const item;
498 const char * const value;
499 const char * const reason;
501 (void) fprintf(stderr,
502 _("date: error: bad command line %s \"%s\", %s\n"),
503 item, value, reason);
504 usage();
507 static void
508 errensure P((void))
510 if (retval == EXIT_SUCCESS)
511 retval = EXIT_FAILURE;
514 static const char *
515 nondigit(cp)
516 register const char * cp;
518 while (is_digit(*cp))
519 ++cp;
520 return cp;
523 static void
524 usage P((void))
526 (void) fprintf(stderr, _("date: usage is date [-u] [-c] [-n] [-d dst] \
527 [-t min-west] [-a sss.fff] [[yyyy]mmddhhmm[yyyy][.ss]] [+format]\n"));
528 errensure();
529 exit(retval);
532 static void
533 oops(string)
534 const char * const string;
536 int e = errno;
538 (void) fprintf(stderr, _("date: error: "));
539 errno = e;
540 (void) perror(string);
541 errensure();
542 display((char *) NULL);
545 static void
546 display(format)
547 const char * const format;
549 struct tm tm;
550 time_t now;
552 (void) time(&now);
553 tm = *localtime(&now);
554 timeout(stdout, format ? format : "%+", &tm);
555 (void) putchar('\n');
556 (void) fflush(stdout);
557 (void) fflush(stderr);
558 if (ferror(stdout) || ferror(stderr)) {
559 (void) fprintf(stderr,
560 _("date: error: couldn't write results\n"));
561 errensure();
563 exit(retval);
566 extern size_t strftime();
568 #define INCR 1024
570 static void
571 timeout(fp, format, tmp)
572 FILE * const fp;
573 const char * const format;
574 const struct tm * const tmp;
576 char * cp;
577 size_t result;
578 size_t size;
580 if (*format == '\0')
581 return;
582 size = INCR;
583 cp = malloc((size_t) size);
584 for ( ; ; ) {
585 if (cp == NULL) {
586 (void) fprintf(stderr,
587 _("date: error: can't get memory\n"));
588 errensure();
589 exit(retval);
591 cp[0] = '\1';
592 result = strftime(cp, size, format, tmp);
593 if (result != 0 || cp[0] == '\0')
594 break;
595 size += INCR;
596 cp = realloc(cp, (size_t) size);
598 (void) fwrite(cp, 1, result, fp);
599 free(cp);
602 static int
603 comptm(atmp, btmp)
604 register const struct tm * const atmp;
605 register const struct tm * const btmp;
607 register int result;
609 if ((result = (atmp->tm_year - btmp->tm_year)) == 0 &&
610 (result = (atmp->tm_mon - btmp->tm_mon)) == 0 &&
611 (result = (atmp->tm_mday - btmp->tm_mday)) == 0 &&
612 (result = (atmp->tm_hour - btmp->tm_hour)) == 0 &&
613 (result = (atmp->tm_min - btmp->tm_min)) == 0)
614 result = atmp->tm_sec - btmp->tm_sec;
615 return result;
619 ** convert --
620 ** convert user's input into a time_t.
623 #define ATOI2(ar) (ar[0] - '0') * 10 + (ar[1] - '0'); ar += 2;
625 static time_t
626 #if __STDC__
627 convert(register const char * const value, const int dousg, const time_t t)
628 #else /* !__STDC__ */
629 convert(value, dousg, t)
630 register const char * const value;
631 const int dousg;
632 const time_t t;
633 #endif /* !__STDC__ */
635 register const char * cp;
636 register const char * dotp;
637 register int cent, year_in_cent, month, hour, day, mins, secs;
638 struct tm tm, outtm;
639 time_t outt;
641 tm = *localtime(&t);
642 #define DIVISOR 100
643 year_in_cent = tm.tm_year % DIVISOR + TM_YEAR_BASE % DIVISOR;
644 cent = tm.tm_year / DIVISOR + TM_YEAR_BASE / DIVISOR +
645 year_in_cent / DIVISOR;
646 year_in_cent %= DIVISOR;
647 if (year_in_cent < 0) {
648 year_in_cent += DIVISOR;
649 --cent;
651 month = tm.tm_mon + 1;
652 day = tm.tm_mday;
653 hour = tm.tm_hour;
654 mins = tm.tm_min;
655 secs = 0;
657 dotp = strchr(value, '.');
658 for (cp = value; *cp != '\0'; ++cp)
659 if (!is_digit(*cp) && cp != dotp)
660 wildinput(_("time"), value, _("contains a nondigit"));
662 if (dotp == NULL)
663 dotp = strchr(value, '\0');
664 else {
665 cp = dotp + 1;
666 if (strlen(cp) != 2)
667 wildinput(_("time"), value,
668 _("seconds part is not two digits"));
669 secs = ATOI2(cp);
672 cp = value;
673 switch (dotp - cp) {
674 default:
675 wildinput(_("time"), value,
676 _("main part is wrong length"));
677 case 12:
678 if (!dousg) {
679 cent = ATOI2(cp);
680 year_in_cent = ATOI2(cp);
682 month = ATOI2(cp);
683 day = ATOI2(cp);
684 hour = ATOI2(cp);
685 mins = ATOI2(cp);
686 if (dousg) {
687 cent = ATOI2(cp);
688 year_in_cent = ATOI2(cp);
690 break;
691 case 8: /* mmddhhmm */
692 month = ATOI2(cp);
693 /* fall through to. . . */
694 case 6: /* ddhhmm */
695 day = ATOI2(cp);
696 /* fall through to. . . */
697 case 4: /* hhmm */
698 hour = ATOI2(cp);
699 mins = ATOI2(cp);
700 break;
701 case 10:
702 if (!dousg) {
703 year_in_cent = ATOI2(cp);
705 month = ATOI2(cp);
706 day = ATOI2(cp);
707 hour = ATOI2(cp);
708 mins = ATOI2(cp);
709 if (dousg) {
710 year_in_cent = ATOI2(cp);
712 break;
715 tm.tm_year = cent * 100 + year_in_cent - TM_YEAR_BASE;
716 tm.tm_mon = month - 1;
717 tm.tm_mday = day;
718 tm.tm_hour = hour;
719 tm.tm_min = mins;
720 tm.tm_sec = secs;
721 tm.tm_isdst = -1;
722 outtm = tm;
723 outt = mktime(&outtm);
724 return (comptm(&tm, &outtm) == 0) ? outt : -1;
728 ** Code from here on out is either based on code provided by UCB
729 ** or is only called just before the program exits.
733 ** Check for iffy input.
736 static void
737 #if __STDC__
738 checkfinal(const char * const value,
739 const int didusg,
740 const time_t t,
741 const time_t oldnow)
742 #else /* !__STDC__ */
743 checkfinal(value, didusg, t, oldnow)
744 const char * const value;
745 const int didusg;
746 const time_t t;
747 const time_t oldnow;
748 #endif /* !__STDC__ */
750 time_t othert;
751 struct tm tm;
752 struct tm othertm;
753 register int pass;
754 register long offset;
757 ** See if there's both a USG and a BSD interpretation.
759 othert = convert(value, !didusg, oldnow);
760 if (othert != -1 && othert != t)
761 iffy(t, othert, value, _("year could be at start or end"));
763 ** See if there's both a DST and a STD version.
765 tm = *localtime(&t);
766 othertm = tm;
767 othertm.tm_isdst = !tm.tm_isdst;
768 othert = mktime(&othertm);
769 if (othert != -1 && othertm.tm_isdst != tm.tm_isdst &&
770 comptm(&tm, &othertm) == 0)
771 iffy(t, othert, value,
772 _("both standard and summer time versions exist"));
774 ** Final check.
776 ** If a jurisdiction shifts time *without* shifting whether time is
777 ** summer or standard (as Hawaii, the United Kingdom, and Saudi Arabia
778 ** have done), routine checks for iffy times may not work.
779 ** So we perform this final check, deferring it until after the time has
780 ** been set--it may take a while, and we don't want to introduce an unnecessary
781 ** lag between the time the user enters their command and the time that
782 ** stime/settimeofday is called.
784 ** We just check nearby times to see if any have the same representation
785 ** as the time that convert returned. We work our way out from the center
786 ** for quick response in solar time situations. We only handle common cases--
787 ** offsets of at most a minute, and offsets of exact numbers of minutes
788 ** and at most an hour.
790 for (offset = 1; offset <= 60; ++offset)
791 for (pass = 1; pass <= 4; ++pass) {
792 if (pass == 1)
793 othert = t + offset;
794 else if (pass == 2)
795 othert = t - offset;
796 else if (pass == 3)
797 othert = t + 60 * offset;
798 else othert = t - 60 * offset;
799 othertm = *localtime(&othert);
800 if (comptm(&tm, &othertm) == 0)
801 iffy(t, othert, value,
802 _("multiple matching times exist"));
806 static void
807 #if __STDC__
808 iffy(const time_t thist, const time_t thatt,
809 const char * const value, const char * const reason)
810 #else /* !__STDC__ */
811 iffy(thist, thatt, value, reason)
812 const time_t thist;
813 const time_t thatt;
814 const char * const value;
815 const char * const reason;
816 #endif /* !__STDC__ */
818 struct tm tm;
820 (void) fprintf(stderr, _("date: warning: ambiguous time \"%s\", %s.\n"),
821 value, reason);
822 tm = *gmtime(&thist);
824 ** Avoid running afoul of SCCS!
826 timeout(stderr, _("Time was set as if you used\n\tdate -u %m%d%H\
828 %Y.%S\n"), &tm);
829 tm = *localtime(&thist);
830 timeout(stderr, _("to get %c"), &tm);
831 (void) fprintf(stderr, _(" (%s). Use\n"),
832 tm.tm_isdst ? _("summer time") : _("standard time"));
833 tm = *gmtime(&thatt);
834 timeout(stderr, _("\tdate -u %m%d%H\
836 %Y.%S\n"), &tm);
837 tm = *localtime(&thatt);
838 timeout(stderr, _("to get %c"), &tm);
839 (void) fprintf(stderr, _(" (%s).\n"),
840 tm.tm_isdst ? _("summer time") : _("standard time"));
841 errensure();
842 exit(retval);
845 #ifdef TSP_SETDATE
846 #define WAITACK 2 /* seconds */
847 #define WAITDATEACK 5 /* seconds */
850 * Set the date in the machines controlled by timedaemons
851 * by communicating the new date to the local timedaemon.
852 * If the timedaemon is in the master state, it performs the
853 * correction on all slaves. If it is in the slave state, it
854 * notifies the master that a correction is needed.
855 * Returns 1 on success, 0 on failure.
857 netsettime(ntv)
858 struct timeval ntv;
860 int s, length, port, timed_ack, found, err;
861 long waittime;
862 fd_set ready;
863 char hostname[MAXHOSTNAMELEN];
864 struct timeval tout;
865 struct servent *sp;
866 struct tsp msg;
867 struct sockaddr_in sin, dest, from;
869 sp = getservbyname("timed", "udp");
870 if (sp == 0) {
871 fputs(_("udp/timed: unknown service\n"), stderr);
872 retval = 2;
873 return (0);
875 dest.sin_port = sp->s_port;
876 dest.sin_family = AF_INET;
877 dest.sin_addr.s_addr = htonl((u_long)INADDR_ANY);
878 s = socket(AF_INET, SOCK_DGRAM, 0);
879 if (s < 0) {
880 if (errno != EPROTONOSUPPORT)
881 perror("date: socket");
882 goto bad;
884 bzero((char *)&sin, sizeof (sin));
885 sin.sin_family = AF_INET;
886 for (port = IPPORT_RESERVED - 1; port > IPPORT_RESERVED / 2; port--) {
887 sin.sin_port = htons((u_short)port);
888 if (bind(s, (struct sockaddr *)&sin, sizeof (sin)) >= 0)
889 break;
890 if (errno != EADDRINUSE) {
891 if (errno != EADDRNOTAVAIL)
892 perror("date: bind");
893 goto bad;
896 if (port == IPPORT_RESERVED / 2) {
897 fputs(_("date: All ports in use\n"), stderr);
898 goto bad;
900 msg.tsp_type = TSP_SETDATE;
901 msg.tsp_vers = TSPVERSION;
902 if (gethostname(hostname, sizeof (hostname))) {
903 perror("gethostname");
904 goto bad;
906 (void) strncpy(msg.tsp_name, hostname, sizeof (hostname));
907 msg.tsp_seq = htons((u_short)0);
908 msg.tsp_time.tv_sec = htonl((u_long)ntv.tv_sec);
909 msg.tsp_time.tv_usec = htonl((u_long)ntv.tv_usec);
910 length = sizeof (struct sockaddr_in);
911 if (connect(s, &dest, length) < 0) {
912 perror("date: connect");
913 goto bad;
915 if (send(s, (char *)&msg, sizeof (struct tsp), 0) < 0) {
916 if (errno != ECONNREFUSED)
917 perror("date: send");
918 goto bad;
920 timed_ack = -1;
921 waittime = WAITACK;
922 loop:
923 tout.tv_sec = waittime;
924 tout.tv_usec = 0;
925 FD_ZERO(&ready);
926 FD_SET(s, &ready);
927 found = select(FD_SETSIZE, &ready, (fd_set *)0, (fd_set *)0, &tout);
928 length = sizeof err;
929 if (getsockopt(s, SOL_SOCKET, SO_ERROR, (char *)&err, &length) == 0
930 && err) {
931 errno = err;
932 if (errno != ECONNREFUSED)
933 perror(_("date: send (delayed error)"));
934 goto bad;
936 if (found > 0 && FD_ISSET(s, &ready)) {
937 length = sizeof (struct sockaddr_in);
938 if (recvfrom(s, (char *)&msg, sizeof (struct tsp), 0, &from,
939 &length) < 0) {
940 if (errno != ECONNREFUSED)
941 perror("date: recvfrom");
942 goto bad;
944 msg.tsp_seq = ntohs(msg.tsp_seq);
945 msg.tsp_time.tv_sec = ntohl(msg.tsp_time.tv_sec);
946 msg.tsp_time.tv_usec = ntohl(msg.tsp_time.tv_usec);
947 switch (msg.tsp_type) {
949 case TSP_ACK:
950 timed_ack = TSP_ACK;
951 waittime = WAITDATEACK;
952 goto loop;
954 case TSP_DATEACK:
955 (void)close(s);
956 return (1);
958 default:
959 fprintf(stderr,
960 _("date: Wrong ack received from timed: %s\n"),
961 tsptype[msg.tsp_type]);
962 timed_ack = -1;
963 break;
966 if (timed_ack == -1)
967 fputs(_("date: Can't reach time daemon, time set locally.\n"),
968 stderr);
969 bad:
970 (void)close(s);
971 retval = 2;
972 return (0);
974 #endif /* defined TSP_SETDATE */