2 * Copyright 1996 Massachusetts Institute of Technology
4 * Permission to use, copy, modify, and distribute this software and
5 * its documentation for any purpose and without fee is hereby
6 * granted, provided that both the above copyright notice and this
7 * permission notice appear in all copies, that both the above
8 * copyright notice and this permission notice appear in all
9 * supporting documentation, and that the name of M.I.T. not be used
10 * in advertising or publicity pertaining to distribution of the
11 * software without specific, written prior permission. M.I.T. makes
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied
16 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS
17 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
20 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * Second attempt at a `tzmenu' program, using the separate description
32 * files provided in newer tzdata releases.
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
47 #include <sys/fcntl.h>
48 #include <sys/param.h>
49 #include <sys/queue.h>
52 #define _PATH_ZONETAB "/usr/share/zoneinfo/zone.tab"
53 #define _PATH_ISO3166 "/usr/share/misc/iso3166"
54 #define _PATH_ZONEINFO "/usr/share/zoneinfo"
55 #define _PATH_LOCALTIME "/etc/localtime"
56 #define _PATH_DB "/var/db/zoneinfo"
57 #define _PATH_WALL_CMOS_CLOCK "/etc/wall_cmos_clock"
59 static char path_zonetab[MAXPATHLEN], path_iso3166[MAXPATHLEN],
60 path_zoneinfo[MAXPATHLEN], path_localtime[MAXPATHLEN],
61 path_db[MAXPATHLEN], path_wall_cmos_clock[MAXPATHLEN];
63 static int reallydoit = 1;
64 static int reinstall = 0;
65 static int usedialog = 1;
66 static char *chrootenv = NULL;
68 static void usage(void);
69 static int confirm_zone(const char *filename);
70 static int continent_country_menu(dialogMenuItem *);
71 static int install_zoneinfo_file(const char *zoneinfo_file);
72 static int set_zone_multi(dialogMenuItem *);
73 static int set_zone_whole_country(dialogMenuItem *);
74 static int set_zone_menu(dialogMenuItem *);
75 static int set_zone_utc(void);
84 static struct continent africa, america, antarctica, arctic, asia, atlantic;
85 static struct continent australia, europe, indian, pacific, utc;
87 static struct continent_names {
89 struct continent *continent;
90 } continent_names[] = {
91 { "Africa", &africa },
92 { "America", &america },
93 { "Antarctica", &antarctica },
94 { "Arctic", &arctic },
96 { "Atlantic", &atlantic },
97 { "Australia", &australia },
98 { "Europe", &europe },
99 { "Indian", &indian },
100 { "Pacific", &pacific },
104 static struct continent_items {
107 } continent_items[] = {
109 { "2", "America -- North and South" },
110 { "3", "Antarctica" },
111 { "4", "Arctic Ocean" },
113 { "6", "Atlantic Ocean" },
114 { "7", "Australia" },
116 { "9", "Indian Ocean" },
117 { "0", "Pacific Ocean" },
121 #define NCONTINENTS \
122 (int)((sizeof(continent_items)) / (sizeof(continent_items[0])))
123 static dialogMenuItem continents[NCONTINENTS];
125 #define OCEANP(x) ((x) == 3 || (x) == 5 || (x) == 8 || (x) == 9)
128 continent_country_menu(dialogMenuItem *continent)
130 char title[64], prompt[64];
131 struct continent *contp = continent->data;
132 int isocean = OCEANP(continent - continents);
136 if (strcmp(continent->title, "UTC") == 0)
137 return set_zone_utc();
139 /* Short cut -- if there's only one country, don't post a menu. */
140 if (contp->nitems == 1)
141 return (contp->menu[0].fire(&contp->menu[0]));
143 /* It's amazing how much good grammar really matters... */
145 snprintf(title, sizeof(title), "Countries in %s",
147 snprintf(prompt, sizeof(prompt), "Select a country or region");
149 snprintf(title, sizeof(title), "Islands and groups in the %s",
151 snprintf(prompt, sizeof(prompt), "Select an island or group");
154 menulen = contp->nitems < 16 ? contp->nitems : 16;
155 rv = dialog_menu(title, prompt, -1, -1, menulen, -contp->nitems,
156 contp->menu, 0, &contp->ch, &contp->sc);
158 return (DITEM_LEAVE_MENU);
159 return (DITEM_RECREATE);
162 static struct continent *
163 find_continent(const char *name)
167 for (i = 0; i < NCONTINENTS; i++)
168 if (strcmp(name, continent_names[i].name) == 0)
169 return (continent_names[i].continent);
177 char *filename; /* use iff nzones < 0 */
178 struct continent *continent; /* use iff nzones < 0 */
179 TAILQ_HEAD(, zone) zones; /* use iff nzones > 0 */
180 dialogMenuItem *submenu; /* use iff nzones > 0 */
184 TAILQ_ENTRY(zone) link;
187 struct continent *continent;
191 * This is the easiest organization... we use ISO 3166 country codes,
192 * of the two-letter variety, so we just size this array to suit.
193 * Beats worrying about dynamic allocation.
195 #define NCOUNTRIES (26 * 26)
196 static struct country countries[NCOUNTRIES];
198 #define CODE2INT(s) ((s[0] - 'A') * 26 + (s[1] - 'A'))
201 * Read the ISO 3166 country code database in _PATH_ISO3166
202 * (/usr/share/misc/iso3166). On error, exit via err(3).
205 read_iso3166_table(void)
213 fp = fopen(path_iso3166, "r");
215 err(1, "%s", path_iso3166);
218 while ((s = fgetln(fp, &len)) != 0) {
220 if (s[len - 1] != '\n')
221 errx(1, "%s:%d: invalid format", path_iso3166, lineno);
223 if (s[0] == '#' || strspn(s, " \t") == len - 1)
226 /* Isolate the two-letter code. */
227 t = strsep(&s, "\t");
228 if (t == 0 || strlen(t) != 2)
229 errx(1, "%s:%d: invalid format", path_iso3166, lineno);
230 if (t[0] < 'A' || t[0] > 'Z' || t[1] < 'A' || t[1] > 'Z')
231 errx(1, "%s:%d: invalid code `%s'", path_iso3166,
234 /* Now skip past the three-letter and numeric codes. */
235 name = strsep(&s, "\t"); /* 3-let */
236 if (name == 0 || strlen(name) != 3)
237 errx(1, "%s:%d: invalid format", path_iso3166, lineno);
238 name = strsep(&s, "\t"); /* numeric */
239 if (name == 0 || strlen(name) != 3)
240 errx(1, "%s:%d: invalid format", path_iso3166, lineno);
244 cp = &countries[CODE2INT(t)];
246 errx(1, "%s:%d: country code `%s' multiply defined: %s",
247 path_iso3166, lineno, t, cp->name);
248 cp->name = strdup(name);
249 if (cp->name == NULL)
250 errx(1, "malloc failed");
253 errx(1, "malloc failed");
260 add_zone_to_country(int lineno, const char *tlc, const char *descr,
261 const char *file, struct continent *cont)
266 if (tlc[0] < 'A' || tlc[0] > 'Z' || tlc[1] < 'A' || tlc[1] > 'Z')
267 errx(1, "%s:%d: country code `%s' invalid", path_zonetab,
270 cp = &countries[CODE2INT(tlc)];
272 errx(1, "%s:%d: country code `%s' unknown", path_zonetab,
277 errx(1, "%s:%d: conflicting zone definition",
278 path_zonetab, lineno);
280 zp = malloc(sizeof(*zp));
282 errx(1, "malloc(%zu)", sizeof(*zp));
285 TAILQ_INIT(&cp->zones);
287 zp->descr = strdup(descr);
288 if (zp->descr == NULL)
289 errx(1, "malloc failed");
290 zp->filename = strdup(file);
291 if (zp->filename == NULL)
292 errx(1, "malloc failed");
293 zp->continent = cont;
294 TAILQ_INSERT_TAIL(&cp->zones, zp, link);
298 errx(1, "%s:%d: zone must have description",
299 path_zonetab, lineno);
301 errx(1, "%s:%d: zone multiply defined",
302 path_zonetab, lineno);
304 cp->filename = strdup(file);
305 if (cp->filename == NULL)
306 errx(1, "malloc failed");
307 cp->continent = cont;
312 * This comparison function intentionally sorts all of the null-named
313 * ``countries''---i.e., the codes that don't correspond to a real
314 * country---to the end. Everything else is lexical by country name.
317 compare_countries(const void *xa, const void *xb)
319 const struct country *a = xa, *b = xb;
321 if (a->name == 0 && b->name == 0)
323 if (a->name == 0 && b->name != 0)
328 return (strcmp(a->name, b->name));
332 * This must be done AFTER all zone descriptions are read, since it breaks
339 qsort(countries, NCOUNTRIES, sizeof(countries[0]), compare_countries);
347 struct continent *cont;
349 char *line, *tlc, *coord, *file, *descr, *p;
352 fp = fopen(path_zonetab, "r");
354 err(1, "%s", path_zonetab);
357 while ((line = fgetln(fp, &len)) != 0) {
359 if (line[len - 1] != '\n')
360 errx(1, "%s:%d: invalid format", path_zonetab, lineno);
361 line[len - 1] = '\0';
365 tlc = strsep(&line, "\t");
366 if (strlen(tlc) != 2)
367 errx(1, "%s:%d: invalid country code `%s'",
368 path_zonetab, lineno, tlc);
369 coord = strsep(&line, "\t"); /* Unused */
370 file = strsep(&line, "\t");
371 p = strchr(file, '/');
373 errx(1, "%s:%d: invalid zone name `%s'", path_zonetab,
376 strncat(contbuf, file, p - file);
377 cont = find_continent(contbuf);
379 errx(1, "%s:%d: invalid region `%s'", path_zonetab,
382 descr = (line != NULL && *line != '\0') ? line : NULL;
384 add_zone_to_country(lineno, tlc, descr, file, cont);
393 struct zone *zp, *zp2;
394 struct continent *cont;
399 * First, count up all the countries in each continent/ocean.
400 * Be careful to count those countries which have multiple zones
401 * only once for each. NB: some countries are in multiple
404 for (cp = countries; cp->name; cp++) {
407 if (cp->nzones < 0) {
408 cp->continent->nitems++;
410 TAILQ_FOREACH(zp, &cp->zones, link) {
411 cont = zp->continent;
412 for (zp2 = TAILQ_FIRST(&cp->zones);
413 zp2->continent != cont;
414 zp2 = TAILQ_NEXT(zp2, link))
417 zp->continent->nitems++;
423 * Now allocate memory for the country menus and initialize
424 * continent menus. We set nitems back to zero so that we can
425 * use it for counting again when we actually build the menus.
427 memset(continents, 0, sizeof(continents));
428 for (i = 0; i < NCONTINENTS; i++) {
429 continent_names[i].continent->menu =
430 malloc(sizeof(dialogMenuItem) *
431 continent_names[i].continent->nitems);
432 if (continent_names[i].continent->menu == 0)
433 errx(1, "malloc for continent menu");
434 continent_names[i].continent->nitems = 0;
435 continents[i].prompt = continent_items[i].prompt;
436 continents[i].title = continent_items[i].title;
437 continents[i].fire = continent_country_menu;
438 continents[i].data = continent_names[i].continent;
442 * Now that memory is allocated, create the menu items for
443 * each continent. For multiple-zone countries, also create
444 * the country's zone submenu.
446 for (cp = countries; cp->name; cp++) {
449 if (cp->nzones < 0) {
450 dmi = &cp->continent->menu[cp->continent->nitems];
451 memset(dmi, 0, sizeof(*dmi));
452 asprintf(&dmi->prompt, "%d", ++cp->continent->nitems);
453 dmi->title = cp->name;
455 dmi->fire = set_zone_whole_country;
459 cp->submenu = malloc(cp->nzones * sizeof(*dmi));
460 if (cp->submenu == 0)
461 errx(1, "malloc for submenu");
463 TAILQ_FOREACH(zp, &cp->zones, link) {
464 cont = zp->continent;
465 dmi = &cp->submenu[cp->nzones];
466 memset(dmi, 0, sizeof(*dmi));
467 asprintf(&dmi->prompt, "%d", ++cp->nzones);
468 dmi->title = zp->descr;
470 dmi->fire = set_zone_multi;
474 for (zp2 = TAILQ_FIRST(&cp->zones);
475 zp2->continent != cont;
476 zp2 = TAILQ_NEXT(zp2, link))
481 dmi = &cont->menu[cont->nitems];
482 memset(dmi, 0, sizeof(*dmi));
483 asprintf(&dmi->prompt, "%d", ++cont->nitems);
484 dmi->title = cp->name;
486 dmi->fire = set_zone_menu;
495 set_zone_menu(dialogMenuItem *dmi)
497 char title[64], prompt[64];
498 struct country *cp = dmi->data;
502 snprintf(title, sizeof(title), "%s Time Zones", cp->name);
503 snprintf(prompt, sizeof(prompt),
504 "Select a zone which observes the same time as your locality.");
505 menulen = cp->nzones < 16 ? cp->nzones : 16;
506 rv = dialog_menu(title, prompt, -1, -1, menulen, -cp->nzones,
507 cp->submenu, 0, 0, 0);
509 return (DITEM_RECREATE);
510 return (DITEM_LEAVE_MENU);
516 if (!confirm_zone(NULL))
517 return (DITEM_FAILURE | DITEM_RECREATE);
519 return (install_zoneinfo_file(NULL));
523 install_zoneinfo_file(const char *zoneinfo_file)
526 char title[64], prompt[64];
529 int fd1, fd2, copymode;
531 if (lstat(path_localtime, &sb) < 0) {
532 /* Nothing there yet... */
534 } else if (S_ISLNK(sb.st_mode))
541 snprintf(prompt, sizeof(prompt),
542 "Copying %s to %s", zoneinfo_file, path_localtime);
544 snprintf(prompt, sizeof(prompt),
545 "Creating symbolic link %s to %s",
547 zoneinfo_file == NULL ? "(UTC)" : zoneinfo_file);
549 dialog_notify(prompt);
551 fprintf(stderr, "%s\n", prompt);
555 if (zoneinfo_file == NULL) {
556 if (unlink(path_localtime) < 0 && errno != ENOENT) {
557 snprintf(title, sizeof(title), "Error");
558 snprintf(prompt, sizeof(prompt),
559 "Could not delete %s: %s", path_localtime,
562 dialog_mesgbox(title, prompt, 8, 72);
564 fprintf(stderr, "%s\n", prompt);
566 return (DITEM_FAILURE | DITEM_RECREATE);
568 return (DITEM_LEAVE_MENU);
572 fd1 = open(zoneinfo_file, O_RDONLY, 0);
574 snprintf(title, sizeof(title), "Error");
575 snprintf(prompt, sizeof(prompt),
576 "Could not open %s: %s", zoneinfo_file,
579 dialog_mesgbox(title, prompt, 8, 72);
581 fprintf(stderr, "%s\n", prompt);
582 return (DITEM_FAILURE | DITEM_RECREATE);
585 unlink(path_localtime);
586 fd2 = open(path_localtime, O_CREAT | O_EXCL | O_WRONLY,
587 S_IRUSR | S_IRGRP | S_IROTH);
589 snprintf(title, sizeof(title), "Error");
590 snprintf(prompt, sizeof(prompt),
591 "Could not open %s: %s",
592 path_localtime, strerror(errno));
594 dialog_mesgbox(title, prompt, 8, 72);
596 fprintf(stderr, "%s\n", prompt);
597 return (DITEM_FAILURE | DITEM_RECREATE);
600 while ((len = read(fd1, buf, sizeof(buf))) > 0)
601 if ((len = write(fd2, buf, len)) < 0)
605 snprintf(title, sizeof(title), "Error");
606 snprintf(prompt, sizeof(prompt),
607 "Error copying %s to %s %s", zoneinfo_file,
608 path_localtime, strerror(errno));
610 dialog_mesgbox(title, prompt, 8, 72);
612 fprintf(stderr, "%s\n", prompt);
613 /* Better to leave none than a corrupt one. */
614 unlink(path_localtime);
615 return (DITEM_FAILURE | DITEM_RECREATE);
620 if (access(zoneinfo_file, R_OK) != 0) {
621 snprintf(title, sizeof(title), "Error");
622 snprintf(prompt, sizeof(prompt),
623 "Cannot access %s: %s", zoneinfo_file,
626 dialog_mesgbox(title, prompt, 8, 72);
628 fprintf(stderr, "%s\n", prompt);
629 return (DITEM_FAILURE | DITEM_RECREATE);
631 unlink(path_localtime);
632 if (symlink(zoneinfo_file, path_localtime) < 0) {
633 snprintf(title, sizeof(title), "Error");
634 snprintf(prompt, sizeof(prompt),
635 "Cannot create symbolic link %s to %s: %s",
636 path_localtime, zoneinfo_file,
639 dialog_mesgbox(title, prompt, 8, 72);
641 fprintf(stderr, "%s\n", prompt);
642 return (DITEM_FAILURE | DITEM_RECREATE);
648 snprintf(title, sizeof(title), "Done");
650 snprintf(prompt, sizeof(prompt),
651 "Copied timezone file from %s to %s", zoneinfo_file,
654 snprintf(prompt, sizeof(prompt),
655 "Created symbolic link from %s to %s", zoneinfo_file,
658 dialog_mesgbox(title, prompt, 8, 72);
660 fprintf(stderr, "%s\n", prompt);
663 return (DITEM_LEAVE_MENU);
667 install_zoneinfo(const char *zoneinfo)
671 char path_zoneinfo_file[MAXPATHLEN];
673 sprintf(path_zoneinfo_file, "%s/%s", path_zoneinfo, zoneinfo);
674 rv = install_zoneinfo_file(path_zoneinfo_file);
676 /* Save knowledge for later */
677 if ((f = fopen(path_db, "w")) != NULL) {
678 fprintf(f, "%s\n", zoneinfo);
686 confirm_zone(const char *filename)
688 char title[64], prompt[64];
693 setenv("TZ", filename == NULL ? "" : filename, 1);
697 snprintf(title, sizeof(title), "Confirmation");
698 snprintf(prompt, sizeof(prompt),
699 "Does the abbreviation `%s' look reasonable?", tm->tm_zone);
700 rv = !dialog_yesno(title, prompt, 5, 72);
705 set_zone_multi(dialogMenuItem *dmi)
707 struct zone *zp = dmi->data;
710 if (!confirm_zone(zp->filename))
711 return (DITEM_FAILURE | DITEM_RECREATE);
713 rv = install_zoneinfo(zp->filename);
718 set_zone_whole_country(dialogMenuItem *dmi)
720 struct country *cp = dmi->data;
723 if (!confirm_zone(cp->filename))
724 return (DITEM_FAILURE | DITEM_RECREATE);
726 rv = install_zoneinfo(cp->filename);
734 fprintf(stderr, "usage: tzsetup [-nrs] [-C chroot_directory]"
735 " [zoneinfo_file | zoneinfo_name]\n");
739 #if defined(__sparc64__)
740 #define DIALOG_UTC dialog_yesno
742 #define DIALOG_UTC dialog_noyes
746 main(int argc, char **argv)
748 char title[64], prompt[128];
749 int c, fd, rv, skiputc;
752 while ((c = getopt(argc, argv, "C:nrs")) != -1) {
772 if (argc - optind > 1)
775 if (chrootenv == NULL) {
776 strcpy(path_zonetab, _PATH_ZONETAB);
777 strcpy(path_iso3166, _PATH_ISO3166);
778 strcpy(path_zoneinfo, _PATH_ZONEINFO);
779 strcpy(path_localtime, _PATH_LOCALTIME);
780 strcpy(path_db, _PATH_DB);
781 strcpy(path_wall_cmos_clock, _PATH_WALL_CMOS_CLOCK);
783 sprintf(path_zonetab, "%s/%s", chrootenv, _PATH_ZONETAB);
784 sprintf(path_iso3166, "%s/%s", chrootenv, _PATH_ISO3166);
785 sprintf(path_zoneinfo, "%s/%s", chrootenv, _PATH_ZONEINFO);
786 sprintf(path_localtime, "%s/%s", chrootenv, _PATH_LOCALTIME);
787 sprintf(path_db, "%s/%s", chrootenv, _PATH_DB);
788 sprintf(path_wall_cmos_clock, "%s/%s", chrootenv,
789 _PATH_WALL_CMOS_CLOCK);
793 /* Override the user-supplied umask. */
794 (void)umask(S_IWGRP | S_IWOTH);
796 read_iso3166_table();
801 if (reinstall == 1) {
803 char zonefile[MAXPATHLEN];
804 char path_db[MAXPATHLEN];
808 if (chrootenv != NULL) {
809 sprintf(zonefile, "%s/", chrootenv);
810 sprintf(path_db, "%s/", chrootenv);
812 strcat(zonefile, _PATH_ZONEINFO);
813 strcat(zonefile, "/");
814 strcat(path_db, _PATH_DB);
816 if ((f = fopen(path_db, "r")) != NULL) {
817 if (fgets(zonefile, sizeof(zonefile), f) != NULL) {
818 zonefile[sizeof(zonefile) - 1] = 0;
819 if (strlen(zonefile) > 0) {
820 zonefile[strlen(zonefile) - 1] = 0;
821 rv = install_zoneinfo(zonefile);
822 exit(rv & ~DITEM_LEAVE_MENU);
824 errx(1, "Error reading %s.\n", path_db);
828 "Unable to determine earlier installed zoneinfo "
829 "file. Check %s", path_db);
831 errx(1, "Cannot open %s for reading. Does it exist?", path_db);
835 * If the arguments on the command-line do not specify a file,
836 * then interpret it as a zoneinfo name
838 if (optind == argc - 1) {
841 if (stat(argv[optind], &sb) != 0) {
843 rv = install_zoneinfo(argv[optind]);
844 exit(rv & ~DITEM_LEAVE_MENU);
851 snprintf(title, sizeof(title),
852 "Select local or UTC (Greenwich Mean Time) clock");
853 snprintf(prompt, sizeof(prompt),
854 "Is this machine's CMOS clock set to UTC? "
855 "If it is set to local time,\n"
856 "or you don't know, please choose NO here!");
857 if (!DIALOG_UTC(title, prompt, 7, 72)) {
859 unlink(path_wall_cmos_clock);
862 fd = open(path_wall_cmos_clock,
863 O_WRONLY | O_CREAT | O_TRUNC,
864 S_IRUSR | S_IRGRP | S_IROTH);
868 path_wall_cmos_clock);
873 dialog_clear_norefresh();
875 if (optind == argc - 1) {
876 snprintf(title, sizeof(title), "Default timezone provided");
877 snprintf(prompt, sizeof(prompt),
878 "\nUse the default `%s' zone?", argv[optind]);
879 if (!dialog_yesno(title, prompt, 7, 72)) {
880 rv = install_zoneinfo_file(argv[optind]);
883 exit(rv & ~DITEM_LEAVE_MENU);
885 dialog_clear_norefresh();
887 snprintf(title, sizeof(title), "Time Zone Selector");
888 snprintf(prompt, sizeof(prompt), "Select a region");
889 dialog_menu(title, prompt, -1, -1, NCONTINENTS, -NCONTINENTS,
890 continents, 0, NULL, NULL);