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/queue.h>
51 #define _PATH_ZONETAB "/usr/share/zoneinfo/zone.tab"
52 #define _PATH_ISO3166 "/usr/share/misc/iso3166"
53 #define _PATH_ZONEINFO "/usr/share/zoneinfo"
54 #define _PATH_LOCALTIME "/etc/localtime"
55 #define _PATH_WALL_CMOS_CLOCK "/etc/wall_cmos_clock"
57 static int reallydoit = 1;
59 static void usage(void);
60 static int continent_country_menu(dialogMenuItem *);
61 static int set_zone_multi(dialogMenuItem *);
62 static int set_zone_whole_country(dialogMenuItem *);
63 static int set_zone_menu(dialogMenuItem *);
72 static struct continent africa, america, antarctica, arctic, asia, atlantic;
73 static struct continent australia, europe, indian, pacific;
75 static struct continent_names {
77 struct continent *continent;
78 } continent_names[] = {
79 { "Africa", &africa },
80 { "America", &america },
81 { "Antarctica", &antarctica },
82 { "Arctic", &arctic },
84 { "Atlantic", &atlantic },
85 { "Australia", &australia },
86 { "Europe", &europe },
87 { "Indian", &indian },
88 { "Pacific", &pacific }
91 static struct continent_items {
94 } continent_items[] = {
96 { "2", "America -- North and South" },
97 { "3", "Antarctica" },
98 { "4", "Arctic Ocean" },
100 { "6", "Atlantic Ocean" },
101 { "7", "Australia" },
103 { "9", "Indian Ocean" },
104 { "0", "Pacific Ocean" }
107 #define NCONTINENTS \
108 (int)((sizeof(continent_items)) / (sizeof(continent_items[0])))
109 static dialogMenuItem continents[NCONTINENTS];
111 #define OCEANP(x) ((x) == 3 || (x) == 5 || (x) == 8 || (x) == 9)
114 continent_country_menu(dialogMenuItem *continent)
116 char title[64], prompt[64];
117 struct continent *contp = continent->data;
118 int isocean = OCEANP(continent - continents);
122 /* Short cut -- if there's only one country, don't post a menu. */
123 if (contp->nitems == 1)
124 return (contp->menu[0].fire(&contp->menu[0]));
126 /* It's amazing how much good grammar really matters... */
128 snprintf(title, sizeof(title), "Countries in %s",
130 snprintf(prompt, sizeof(prompt), "Select a country or region");
132 snprintf(title, sizeof(title), "Islands and groups in the %s",
134 snprintf(prompt, sizeof(prompt), "Select an island or group");
137 menulen = contp->nitems < 16 ? contp->nitems : 16;
138 rv = dialog_menu(title, prompt, -1, -1, menulen, -contp->nitems,
139 contp->menu, 0, &contp->ch, &contp->sc);
141 return (DITEM_LEAVE_MENU);
142 return (DITEM_RECREATE);
145 static struct continent *
146 find_continent(const char *name)
150 for (i = 0; i < NCONTINENTS; i++)
151 if (strcmp(name, continent_names[i].name) == 0)
152 return (continent_names[i].continent);
160 char *filename; /* use iff nzones < 0 */
161 struct continent *continent; /* use iff nzones < 0 */
162 TAILQ_HEAD(, zone) zones; /* use iff nzones > 0 */
163 dialogMenuItem *submenu; /* use iff nzones > 0 */
167 TAILQ_ENTRY(zone) link;
170 struct continent *continent;
174 * This is the easiest organization... we use ISO 3166 country codes,
175 * of the two-letter variety, so we just size this array to suit.
176 * Beats worrying about dynamic allocation.
178 #define NCOUNTRIES (26 * 26)
179 static struct country countries[NCOUNTRIES];
181 #define CODE2INT(s) ((s[0] - 'A') * 26 + (s[1] - 'A'))
184 * Read the ISO 3166 country code database in _PATH_ISO3166
185 * (/usr/share/misc/iso3166). On error, exit via err(3).
188 read_iso3166_table(void)
196 fp = fopen(_PATH_ISO3166, "r");
198 err(1, _PATH_ISO3166);
201 while ((s = fgetln(fp, &len)) != 0) {
203 if (s[len - 1] != '\n')
204 errx(1, _PATH_ISO3166 ":%d: invalid format", lineno);
206 if (s[0] == '#' || strspn(s, " \t") == len - 1)
209 /* Isolate the two-letter code. */
210 t = strsep(&s, "\t");
211 if (t == 0 || strlen(t) != 2)
212 errx(1, _PATH_ISO3166 ":%d: invalid format", lineno);
213 if (t[0] < 'A' || t[0] > 'Z' || t[1] < 'A' || t[1] > 'Z')
214 errx(1, _PATH_ISO3166 ":%d: invalid code `%s'",
217 /* Now skip past the three-letter and numeric codes. */
218 name = strsep(&s, "\t"); /* 3-let */
219 if (name == 0 || strlen(name) != 3)
220 errx(1, _PATH_ISO3166 ":%d: invalid format", lineno);
221 name = strsep(&s, "\t"); /* numeric */
222 if (name == 0 || strlen(name) != 3)
223 errx(1, _PATH_ISO3166 ":%d: invalid format", lineno);
227 cp = &countries[CODE2INT(t)];
229 errx(1, _PATH_ISO3166
230 ":%d: country code `%s' multiply defined: %s",
231 lineno, t, cp->name);
232 cp->name = strdup(name);
233 if (cp->name == NULL)
234 errx(1, "malloc failed");
237 errx(1, "malloc failed");
244 add_zone_to_country(int lineno, const char *tlc, const char *descr,
245 const char *file, struct continent *cont)
250 if (tlc[0] < 'A' || tlc[0] > 'Z' || tlc[1] < 'A' || tlc[1] > 'Z')
251 errx(1, _PATH_ZONETAB ":%d: country code `%s' invalid",
254 cp = &countries[CODE2INT(tlc)];
256 errx(1, _PATH_ZONETAB ":%d: country code `%s' unknown",
261 errx(1, _PATH_ZONETAB
262 ":%d: conflicting zone definition", lineno);
264 zp = malloc(sizeof(*zp));
266 errx(1, "malloc(%zu)", sizeof(*zp));
269 TAILQ_INIT(&cp->zones);
271 zp->descr = strdup(descr);
272 if (zp->descr == NULL)
273 errx(1, "malloc failed");
274 zp->filename = strdup(file);
275 if (zp->filename == NULL)
276 errx(1, "malloc failed");
277 zp->continent = cont;
278 TAILQ_INSERT_TAIL(&cp->zones, zp, link);
282 errx(1, _PATH_ZONETAB
283 ":%d: zone must have description", lineno);
285 errx(1, _PATH_ZONETAB
286 ":%d: zone multiply defined", lineno);
288 cp->filename = strdup(file);
289 if (cp->filename == NULL)
290 errx(1, "malloc failed");
291 cp->continent = cont;
296 * This comparison function intentionally sorts all of the null-named
297 * ``countries''---i.e., the codes that don't correspond to a real
298 * country---to the end. Everything else is lexical by country name.
301 compare_countries(const void *xa, const void *xb)
303 const struct country *a = xa, *b = xb;
305 if (a->name == 0 && b->name == 0)
307 if (a->name == 0 && b->name != 0)
312 return (strcmp(a->name, b->name));
316 * This must be done AFTER all zone descriptions are read, since it breaks
323 qsort(countries, NCOUNTRIES, sizeof(countries[0]), compare_countries);
331 struct continent *cont;
333 char *line, *tlc, *coord, *file, *descr, *p;
336 fp = fopen(_PATH_ZONETAB, "r");
338 err(1, _PATH_ZONETAB);
341 while ((line = fgetln(fp, &len)) != 0) {
343 if (line[len - 1] != '\n')
344 errx(1, _PATH_ZONETAB ":%d: invalid format", lineno);
345 line[len - 1] = '\0';
349 tlc = strsep(&line, "\t");
350 if (strlen(tlc) != 2)
351 errx(1, _PATH_ZONETAB ":%d: invalid country code `%s'",
353 coord = strsep(&line, "\t");
354 file = strsep(&line, "\t");
355 p = strchr(file, '/');
357 errx(1, _PATH_ZONETAB ":%d: invalid zone name `%s'",
360 strncat(contbuf, file, p - file);
361 cont = find_continent(contbuf);
363 errx(1, _PATH_ZONETAB ":%d: invalid region `%s'",
366 descr = (line != NULL && *line != '\0') ? line : NULL;
368 add_zone_to_country(lineno, tlc, descr, file, cont);
377 struct zone *zp, *zp2;
378 struct continent *cont;
383 * First, count up all the countries in each continent/ocean.
384 * Be careful to count those countries which have multiple zones
385 * only once for each. NB: some countries are in multiple
388 for (cp = countries; cp->name; cp++) {
391 if (cp->nzones < 0) {
392 cp->continent->nitems++;
394 TAILQ_FOREACH(zp, &cp->zones, link) {
395 cont = zp->continent;
396 for (zp2 = TAILQ_FIRST(&cp->zones);
397 zp2->continent != cont;
398 zp2 = TAILQ_NEXT(zp2, link))
401 zp->continent->nitems++;
407 * Now allocate memory for the country menus and initialize
408 * continent menus. We set nitems back to zero so that we can
409 * use it for counting again when we actually build the menus.
411 memset(continents, 0, sizeof(continents));
412 for (i = 0; i < NCONTINENTS; i++) {
413 continent_names[i].continent->menu =
414 malloc(sizeof(dialogMenuItem) *
415 continent_names[i].continent->nitems);
416 if (continent_names[i].continent->menu == 0)
417 errx(1, "malloc for continent menu");
418 continent_names[i].continent->nitems = 0;
419 continents[i].prompt = continent_items[i].prompt;
420 continents[i].title = continent_items[i].title;
421 continents[i].fire = continent_country_menu;
422 continents[i].data = continent_names[i].continent;
426 * Now that memory is allocated, create the menu items for
427 * each continent. For multiple-zone countries, also create
428 * the country's zone submenu.
430 for (cp = countries; cp->name; cp++) {
433 if (cp->nzones < 0) {
434 dmi = &cp->continent->menu[cp->continent->nitems];
435 memset(dmi, 0, sizeof(*dmi));
436 asprintf(&dmi->prompt, "%d", ++cp->continent->nitems);
437 dmi->title = cp->name;
439 dmi->fire = set_zone_whole_country;
443 cp->submenu = malloc(cp->nzones * sizeof(*dmi));
444 if (cp->submenu == 0)
445 errx(1, "malloc for submenu");
447 TAILQ_FOREACH(zp, &cp->zones, link) {
448 cont = zp->continent;
449 dmi = &cp->submenu[cp->nzones];
450 memset(dmi, 0, sizeof(*dmi));
451 asprintf(&dmi->prompt, "%d", ++cp->nzones);
452 dmi->title = zp->descr;
454 dmi->fire = set_zone_multi;
458 for (zp2 = TAILQ_FIRST(&cp->zones);
459 zp2->continent != cont;
460 zp2 = TAILQ_NEXT(zp2, link))
465 dmi = &cont->menu[cont->nitems];
466 memset(dmi, 0, sizeof(*dmi));
467 asprintf(&dmi->prompt, "%d", ++cont->nitems);
468 dmi->title = cp->name;
470 dmi->fire = set_zone_menu;
479 set_zone_menu(dialogMenuItem *dmi)
481 char title[64], prompt[64];
482 struct country *cp = dmi->data;
486 snprintf(title, sizeof(title), "%s Time Zones", cp->name);
487 snprintf(prompt, sizeof(prompt),
488 "Select a zone which observes the same time as your locality.");
489 menulen = cp->nzones < 16 ? cp->nzones : 16;
490 rv = dialog_menu(title, prompt, -1, -1, menulen, -cp->nzones,
491 cp->submenu, 0, 0, 0);
493 return (DITEM_RECREATE);
494 return (DITEM_LEAVE_MENU);
498 install_zone_file(const char *filename)
501 char title[64], prompt[64];
504 int fd1, fd2, copymode;
506 if (lstat(_PATH_LOCALTIME, &sb) < 0) {
507 /* Nothing there yet... */
509 } else if (S_ISLNK(sb.st_mode))
516 snprintf(prompt, sizeof(prompt),
517 "Copying %s to " _PATH_LOCALTIME, filename);
519 snprintf(prompt, sizeof(prompt),
520 "Creating symbolic link " _PATH_LOCALTIME " to %s",
522 dialog_notify(prompt);
527 fd1 = open(filename, O_RDONLY, 0);
529 snprintf(title, sizeof(title), "Error");
530 snprintf(prompt, sizeof(prompt),
531 "Could not open %s: %s", filename,
533 dialog_mesgbox(title, prompt, 8, 72);
534 return (DITEM_FAILURE | DITEM_RECREATE);
537 unlink(_PATH_LOCALTIME);
538 fd2 = open(_PATH_LOCALTIME, O_CREAT | O_EXCL | O_WRONLY,
539 S_IRUSR | S_IRGRP | S_IROTH);
541 snprintf(title, sizeof(title), "Error");
542 snprintf(prompt, sizeof(prompt),
543 "Could not open " _PATH_LOCALTIME ": %s",
545 dialog_mesgbox(title, prompt, 8, 72);
546 return (DITEM_FAILURE | DITEM_RECREATE);
549 while ((len = read(fd1, buf, sizeof(buf))) > 0)
550 len = write(fd2, buf, len);
553 snprintf(title, sizeof(title), "Error");
554 snprintf(prompt, sizeof(prompt),
555 "Error copying %s to " _PATH_LOCALTIME
556 ": %s", filename, strerror(errno));
557 dialog_mesgbox(title, prompt, 8, 72);
558 /* Better to leave none than a corrupt one. */
559 unlink(_PATH_LOCALTIME);
560 return (DITEM_FAILURE | DITEM_RECREATE);
565 if (access(filename, R_OK) != 0) {
566 snprintf(title, sizeof(title), "Error");
567 snprintf(prompt, sizeof(prompt),
568 "Cannot access %s: %s", filename,
570 dialog_mesgbox(title, prompt, 8, 72);
571 return (DITEM_FAILURE | DITEM_RECREATE);
573 unlink(_PATH_LOCALTIME);
574 if (symlink(filename, _PATH_LOCALTIME) < 0) {
575 snprintf(title, sizeof(title), "Error");
576 snprintf(prompt, sizeof(prompt),
577 "Cannot create symbolic link "
578 _PATH_LOCALTIME " to %s: %s", filename,
580 dialog_mesgbox(title, prompt, 8, 72);
581 return (DITEM_FAILURE | DITEM_RECREATE);
587 snprintf(title, sizeof(title), "Done");
589 snprintf(prompt, sizeof(prompt),
590 "Copied timezone file from %s to " _PATH_LOCALTIME,
593 snprintf(prompt, sizeof(prompt), "Created symbolic link from "
594 _PATH_LOCALTIME " to %s", filename);
595 dialog_mesgbox(title, prompt, 8, 72);
597 return (DITEM_LEAVE_MENU);
601 confirm_zone(const char *filename)
603 char title[64], prompt[64];
608 setenv("TZ", filename, 1);
612 snprintf(title, sizeof(title), "Confirmation");
613 snprintf(prompt, sizeof(prompt),
614 "Does the abbreviation `%s' look reasonable?", tm->tm_zone);
615 rv = !dialog_yesno(title, prompt, 5, 72);
620 set_zone_multi(dialogMenuItem *dmi)
622 struct zone *zp = dmi->data;
626 if (!confirm_zone(zp->filename))
627 return (DITEM_FAILURE | DITEM_RECREATE);
629 asprintf(&fn, "%s/%s", _PATH_ZONEINFO, zp->filename);
630 rv = install_zone_file(fn);
636 set_zone_whole_country(dialogMenuItem *dmi)
638 struct country *cp = dmi->data;
642 if (!confirm_zone(cp->filename))
643 return (DITEM_FAILURE | DITEM_RECREATE);
645 asprintf(&fn, "%s/%s", _PATH_ZONEINFO, cp->filename);
646 rv = install_zone_file(fn);
655 fprintf(stderr, "usage: tzsetup [-ns]\n");
659 #if defined(__sparc64__)
660 #define DIALOG_UTC dialog_yesno
662 #define DIALOG_UTC dialog_noyes
666 main(int argc, char **argv)
668 char title[64], prompt[128];
672 while ((c = getopt(argc, argv, "ns")) != -1) {
685 if (argc - optind > 1)
688 /* Override the user-supplied umask. */
689 (void)umask(S_IWGRP | S_IWOTH);
691 read_iso3166_table();
698 snprintf(title, sizeof(title),
699 "Select local or UTC (Greenwich Mean Time) clock");
700 snprintf(prompt, sizeof(prompt),
701 "Is this machine's CMOS clock set to UTC? "
702 "If it is set to local time,\n"
703 "or you don't know, please choose NO here!");
704 if (!DIALOG_UTC(title, prompt, 7, 72)) {
706 unlink(_PATH_WALL_CMOS_CLOCK);
709 fd = open(_PATH_WALL_CMOS_CLOCK,
710 O_WRONLY | O_CREAT | O_TRUNC,
711 S_IRUSR | S_IRGRP | S_IROTH);
714 _PATH_WALL_CMOS_CLOCK);
718 dialog_clear_norefresh();
720 if (optind == argc - 1) {
721 snprintf(title, sizeof(title), "Default timezone provided");
722 snprintf(prompt, sizeof(prompt),
723 "\nUse the default `%s' zone?", argv[optind]);
724 if (!dialog_yesno(title, prompt, 7, 72)) {
725 install_zone_file(argv[optind]);
730 dialog_clear_norefresh();
732 snprintf(title, sizeof(title), "Time Zone Selector");
733 snprintf(prompt, sizeof(prompt), "Select a region");
734 dialog_menu(title, prompt, -1, -1, NCONTINENTS, -NCONTINENTS,
735 continents, 0, NULL, NULL);