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
29 * $FreeBSD: head/usr.sbin/tzsetup/tzsetup.c 337522 2018-08-09 02:47:22Z delphij $
33 * Second attempt at a `tzmenu' program, using the separate description
34 * files provided in newer tzdata releases.
45 #include <sys/fcntl.h>
46 #include <sys/param.h>
47 #include <sys/queue.h>
49 #include <sys/sysctl.h>
55 #define _PATH_ZONETAB "/usr/share/zoneinfo/zone.tab"
56 #define _PATH_ISO3166 "/usr/share/misc/iso3166"
57 #define _PATH_ZONEINFO "/usr/share/zoneinfo"
58 #define _PATH_LOCALTIME "/etc/localtime"
59 #define _PATH_DB "/var/db/zoneinfo"
60 #define _PATH_WALL_CMOS_CLOCK "/etc/wall_cmos_clock"
63 #define SILLY_BUFFER_SIZE 2*PATH_MAX
65 #warning "Somebody needs to fix this to dynamically size this buffer."
66 #define SILLY_BUFFER_SIZE 2048
69 /* special return codes for `fire' actions */
70 #define DITEM_FAILURE 1
72 /* flags - returned in upper 16 bits of return status */
73 #define DITEM_LEAVE_MENU (1 << 16)
74 #define DITEM_RECREATE (1 << 18)
76 static char path_zonetab[MAXPATHLEN], path_iso3166[MAXPATHLEN],
77 path_zoneinfo[MAXPATHLEN], path_localtime[MAXPATHLEN],
78 path_db[MAXPATHLEN], path_wall_cmos_clock[MAXPATHLEN];
80 static int reallydoit = 1;
81 static int reinstall = 0;
82 static char *chrootenv = NULL;
84 static void usage(void);
85 static int install_zoneinfo(const char *zoneinfo);
86 static int install_zoneinfo_file(const char *zoneinfo_file);
89 /* for use in describing more exotic behaviors */
90 typedef struct dialogMenuItem {
93 int (*fire)(struct dialogMenuItem *self);
98 xdialog_count_rows(const char *p)
102 while ((p = strchr(p, '\n')) != NULL) {
109 return (rows ? rows : 1);
113 xdialog_count_columns(const char *p)
119 for (; (q = strchr(p, '\n')) != NULL; p = q + 1) {
121 max_len = MAX(max_len, len);
125 max_len = MAX(max_len, len);
130 xdialog_menu(const char *title, const char *cprompt, int height, int width,
131 int menu_height, int item_no, dialogMenuItem *ditems)
133 int i, result, choice = 0;
134 DIALOG_LISTITEM *listitems;
135 DIALOG_VARS save_vars;
137 dlg_save_vars(&save_vars);
139 /* initialize list items */
140 listitems = dlg_calloc(DIALOG_LISTITEM, item_no + 1);
141 assert_ptr(listitems, "xdialog_menu");
142 for (i = 0; i < item_no; i++) {
143 listitems[i].name = ditems[i].prompt;
144 listitems[i].text = ditems[i].title;
147 /* calculate height */
149 height = xdialog_count_rows(cprompt) + menu_height + 4 + 2;
153 /* calculate width */
157 for (i = 0; i < item_no; i++) {
160 l = strlen(listitems[i].name);
161 for (j = 0; j < item_no; j++) {
162 int k = strlen(listitems[j].text);
163 tag_x = MAX(tag_x, l + k + 2);
166 width = MAX(xdialog_count_columns(cprompt), title != NULL ?
167 xdialog_count_columns(title) : 0);
168 width = MAX(width, tag_x + 4) + 4;
170 width = MAX(width, 24);
175 dialog_vars.default_item = listitems[choice].name;
176 result = dlg_menu(title, cprompt, height, width,
177 menu_height, item_no, listitems, &choice, NULL);
183 if (ditems[choice].fire != NULL) {
186 status = ditems[choice].fire(ditems + choice);
187 if (status & DITEM_RECREATE) {
194 case DLG_EXIT_CANCEL:
201 dlg_restore_vars(&save_vars);
205 static int usedialog = 1;
207 static int confirm_zone(const char *filename);
208 static int continent_country_menu(dialogMenuItem *);
209 static int set_zone_multi(dialogMenuItem *);
210 static int set_zone_whole_country(dialogMenuItem *);
211 static int set_zone_menu(dialogMenuItem *);
212 static int set_zone_utc(void);
215 dialogMenuItem *menu;
219 static struct continent africa, america, antarctica, arctic, asia, atlantic;
220 static struct continent australia, europe, indian, pacific, utc;
222 static struct continent_names {
224 struct continent *continent;
225 } continent_names[] = {
226 { "Africa", &africa },
227 { "America", &america },
228 { "Antarctica", &antarctica },
229 { "Arctic", &arctic },
231 { "Atlantic", &atlantic },
232 { "Australia", &australia },
233 { "Europe", &europe },
234 { "Indian", &indian },
235 { "Pacific", &pacific },
239 static struct continent_items {
242 } continent_items[] = {
244 { "2", "America -- North and South" },
245 { "3", "Antarctica" },
246 { "4", "Arctic Ocean" },
248 { "6", "Atlantic Ocean" },
249 { "7", "Australia" },
251 { "9", "Indian Ocean" },
252 { "0", "Pacific Ocean" },
256 #define NCONTINENTS \
257 (int)((sizeof(continent_items)) / (sizeof(continent_items[0])))
258 static dialogMenuItem continents[NCONTINENTS];
260 #define OCEANP(x) ((x) == 3 || (x) == 5 || (x) == 8 || (x) == 9)
263 continent_country_menu(dialogMenuItem *continent)
265 char title[64], prompt[64];
266 struct continent *contp = continent->data;
267 int isocean = OCEANP(continent - continents);
271 if (strcmp(continent->title, "UTC") == 0)
272 return (set_zone_utc());
274 /* Short cut -- if there's only one country, don't post a menu. */
275 if (contp->nitems == 1)
276 return (contp->menu[0].fire(&contp->menu[0]));
278 /* It's amazing how much good grammar really matters... */
280 snprintf(title, sizeof(title), "Countries in %s",
282 snprintf(prompt, sizeof(prompt), "Select a country or region");
284 snprintf(title, sizeof(title), "Islands and groups in the %s",
286 snprintf(prompt, sizeof(prompt), "Select an island or group");
289 menulen = contp->nitems < 16 ? contp->nitems : 16;
290 rv = xdialog_menu(title, prompt, -1, -1, menulen, contp->nitems,
293 return (DITEM_LEAVE_MENU);
294 return (DITEM_RECREATE);
297 static struct continent *
298 find_continent(const char *name)
302 for (i = 0; i < NCONTINENTS; i++)
303 if (strcmp(name, continent_names[i].name) == 0)
304 return (continent_names[i].continent);
312 char *filename; /* use iff nzones < 0 */
313 struct continent *continent; /* use iff nzones < 0 */
314 TAILQ_HEAD(, zone) zones; /* use iff nzones > 0 */
315 dialogMenuItem *submenu; /* use iff nzones > 0 */
319 TAILQ_ENTRY(zone) link;
322 struct continent *continent;
326 * This is the easiest organization... we use ISO 3166 country codes,
327 * of the two-letter variety, so we just size this array to suit.
328 * Beats worrying about dynamic allocation.
330 #define NCOUNTRIES (26 * 26)
331 static struct country countries[NCOUNTRIES];
333 #define CODE2INT(s) ((s[0] - 'A') * 26 + (s[1] - 'A'))
336 * Read the ISO 3166 country code database in _PATH_ISO3166
337 * (/usr/share/misc/iso3166). On error, exit via err(3).
340 read_iso3166_table(void)
348 fp = fopen(path_iso3166, "r");
350 err(1, "%s", path_iso3166);
353 while ((s = fgetln(fp, &len)) != NULL) {
355 if (s[len - 1] != '\n')
356 errx(1, "%s:%d: invalid format", path_iso3166, lineno);
358 if (s[0] == '#' || strspn(s, " \t") == len - 1)
361 /* Isolate the two-letter code. */
362 t = strsep(&s, "\t");
363 if (t == NULL || strlen(t) != 2)
364 errx(1, "%s:%d: invalid format", path_iso3166, lineno);
365 if (t[0] < 'A' || t[0] > 'Z' || t[1] < 'A' || t[1] > 'Z')
366 errx(1, "%s:%d: invalid code `%s'", path_iso3166,
371 cp = &countries[CODE2INT(t)];
373 errx(1, "%s:%d: country code `%s' multiply defined: %s",
374 path_iso3166, lineno, t, cp->name);
375 cp->name = strdup(name);
376 if (cp->name == NULL)
377 errx(1, "malloc failed");
380 errx(1, "malloc failed");
387 add_zone_to_country(int lineno, const char *tlc, const char *descr,
388 const char *file, struct continent *cont)
393 if (tlc[0] < 'A' || tlc[0] > 'Z' || tlc[1] < 'A' || tlc[1] > 'Z')
394 errx(1, "%s:%d: country code `%s' invalid", path_zonetab,
397 cp = &countries[CODE2INT(tlc)];
399 errx(1, "%s:%d: country code `%s' unknown", path_zonetab,
404 errx(1, "%s:%d: conflicting zone definition",
405 path_zonetab, lineno);
407 zp = malloc(sizeof(*zp));
409 errx(1, "malloc(%zu)", sizeof(*zp));
412 TAILQ_INIT(&cp->zones);
414 zp->descr = strdup(descr);
415 if (zp->descr == NULL)
416 errx(1, "malloc failed");
417 zp->filename = strdup(file);
418 if (zp->filename == NULL)
419 errx(1, "malloc failed");
420 zp->continent = cont;
421 TAILQ_INSERT_TAIL(&cp->zones, zp, link);
425 errx(1, "%s:%d: zone must have description",
426 path_zonetab, lineno);
428 errx(1, "%s:%d: zone multiply defined",
429 path_zonetab, lineno);
431 cp->filename = strdup(file);
432 if (cp->filename == NULL)
433 errx(1, "malloc failed");
434 cp->continent = cont;
439 * This comparison function intentionally sorts all of the null-named
440 * ``countries''---i.e., the codes that don't correspond to a real
441 * country---to the end. Everything else is lexical by country name.
444 compare_countries(const void *xa, const void *xb)
446 const struct country *a = xa, *b = xb;
448 if (a->name == 0 && b->name == 0)
450 if (a->name == 0 && b->name != 0)
455 return (strcmp(a->name, b->name));
459 * This must be done AFTER all zone descriptions are read, since it breaks
466 qsort(countries, NCOUNTRIES, sizeof(countries[0]), compare_countries);
474 struct continent *cont;
476 char *line, *tlc, *file, *descr, *p;
479 fp = fopen(path_zonetab, "r");
481 err(1, "%s", path_zonetab);
484 while ((line = fgetln(fp, &len)) != NULL) {
486 if (line[len - 1] != '\n')
487 errx(1, "%s:%d: invalid format", path_zonetab, lineno);
488 line[len - 1] = '\0';
492 tlc = strsep(&line, "\t");
493 if (strlen(tlc) != 2)
494 errx(1, "%s:%d: invalid country code `%s'",
495 path_zonetab, lineno, tlc);
496 /* coord = */ strsep(&line, "\t"); /* Unused */
497 file = strsep(&line, "\t");
498 /* get continent portion from continent/country */
499 p = strchr(file, '/');
501 errx(1, "%s:%d: invalid zone name `%s'", path_zonetab,
503 contlen = p - file + 1; /* trailing nul */
504 if (contlen > sizeof(contbuf))
505 errx(1, "%s:%d: continent name in zone name `%s' too long",
506 path_zonetab, lineno, file);
507 strlcpy(contbuf, file, contlen);
508 cont = find_continent(contbuf);
510 errx(1, "%s:%d: invalid region `%s'", path_zonetab,
513 descr = (line != NULL && *line != '\0') ? line : NULL;
515 add_zone_to_country(lineno, tlc, descr, file, cont);
524 struct zone *zp, *zp2;
525 struct continent *cont;
530 * First, count up all the countries in each continent/ocean.
531 * Be careful to count those countries which have multiple zones
532 * only once for each. NB: some countries are in multiple
535 for (cp = countries; cp->name; cp++) {
538 if (cp->nzones < 0) {
539 cp->continent->nitems++;
541 TAILQ_FOREACH(zp, &cp->zones, link) {
542 cont = zp->continent;
543 for (zp2 = TAILQ_FIRST(&cp->zones);
544 zp2->continent != cont;
545 zp2 = TAILQ_NEXT(zp2, link))
548 zp->continent->nitems++;
554 * Now allocate memory for the country menus and initialize
555 * continent menus. We set nitems back to zero so that we can
556 * use it for counting again when we actually build the menus.
558 memset(continents, 0, sizeof(continents));
559 for (i = 0; i < NCONTINENTS; i++) {
560 continent_names[i].continent->menu =
561 malloc(sizeof(dialogMenuItem) *
562 continent_names[i].continent->nitems);
563 if (continent_names[i].continent->menu == NULL)
564 errx(1, "malloc for continent menu");
565 continent_names[i].continent->nitems = 0;
566 continents[i].prompt = continent_items[i].prompt;
567 continents[i].title = continent_items[i].title;
568 continents[i].fire = continent_country_menu;
569 continents[i].data = continent_names[i].continent;
573 * Now that memory is allocated, create the menu items for
574 * each continent. For multiple-zone countries, also create
575 * the country's zone submenu.
577 for (cp = countries; cp->name; cp++) {
580 if (cp->nzones < 0) {
581 dmi = &cp->continent->menu[cp->continent->nitems];
582 memset(dmi, 0, sizeof(*dmi));
583 asprintf(&dmi->prompt, "%d", ++cp->continent->nitems);
584 dmi->title = cp->name;
585 dmi->fire = set_zone_whole_country;
588 cp->submenu = malloc(cp->nzones * sizeof(*dmi));
589 if (cp->submenu == 0)
590 errx(1, "malloc for submenu");
592 TAILQ_FOREACH(zp, &cp->zones, link) {
593 cont = zp->continent;
594 dmi = &cp->submenu[cp->nzones];
595 memset(dmi, 0, sizeof(*dmi));
596 asprintf(&dmi->prompt, "%d", ++cp->nzones);
597 dmi->title = zp->descr;
598 dmi->fire = set_zone_multi;
601 for (zp2 = TAILQ_FIRST(&cp->zones);
602 zp2->continent != cont;
603 zp2 = TAILQ_NEXT(zp2, link))
608 dmi = &cont->menu[cont->nitems];
609 memset(dmi, 0, sizeof(*dmi));
610 asprintf(&dmi->prompt, "%d", ++cont->nitems);
611 dmi->title = cp->name;
612 dmi->fire = set_zone_menu;
620 set_zone_menu(dialogMenuItem *dmi)
622 char title[64], prompt[64];
623 struct country *cp = dmi->data;
627 snprintf(title, sizeof(title), "%s Time Zones", cp->name);
628 snprintf(prompt, sizeof(prompt),
629 "Select a zone which observes the same time as your locality.");
630 menulen = cp->nzones < 16 ? cp->nzones : 16;
631 rv = xdialog_menu(title, prompt, -1, -1, menulen, cp->nzones,
634 return (DITEM_RECREATE);
635 return (DITEM_LEAVE_MENU);
641 if (!confirm_zone("UTC"))
642 return (DITEM_FAILURE | DITEM_RECREATE);
644 return (install_zoneinfo("UTC"));
648 confirm_zone(const char *filename)
650 char title[64], prompt[64];
655 setenv("TZ", filename, 1);
659 snprintf(title, sizeof(title), "Confirmation");
660 snprintf(prompt, sizeof(prompt),
661 "Does the abbreviation `%s' look reasonable?", tm->tm_zone);
662 rv = !dialog_yesno(title, prompt, 5, 72);
667 set_zone_multi(dialogMenuItem *dmi)
669 struct zone *zp = dmi->data;
672 if (!confirm_zone(zp->filename))
673 return (DITEM_FAILURE | DITEM_RECREATE);
675 rv = install_zoneinfo(zp->filename);
680 set_zone_whole_country(dialogMenuItem *dmi)
682 struct country *cp = dmi->data;
685 if (!confirm_zone(cp->filename))
686 return (DITEM_FAILURE | DITEM_RECREATE);
688 rv = install_zoneinfo(cp->filename);
695 install_zoneinfo_file(const char *zoneinfo_file)
698 char title[64], prompt[SILLY_BUFFER_SIZE];
701 int fd1, fd2, copymode;
703 if (lstat(path_localtime, &sb) < 0) {
704 /* Nothing there yet... */
706 } else if (S_ISLNK(sb.st_mode))
712 snprintf(title, sizeof(title), "Info");
714 snprintf(prompt, sizeof(prompt),
715 "Copying %s to %s", zoneinfo_file, path_localtime);
717 snprintf(prompt, sizeof(prompt),
718 "Creating symbolic link %s to %s",
719 path_localtime, zoneinfo_file);
722 dialog_msgbox(title, prompt, 8, 72, 1);
725 fprintf(stderr, "%s\n", prompt);
730 fd1 = open(zoneinfo_file, O_RDONLY, 0);
732 snprintf(title, sizeof(title), "Error");
733 snprintf(prompt, sizeof(prompt),
734 "Could not open %s: %s", zoneinfo_file,
738 dialog_msgbox(title, prompt, 8, 72, 1);
741 fprintf(stderr, "%s\n", prompt);
742 return (DITEM_FAILURE | DITEM_RECREATE);
745 if (unlink(path_localtime) < 0 && errno != ENOENT) {
746 snprintf(prompt, sizeof(prompt),
747 "Could not delete %s: %s",
748 path_localtime, strerror(errno));
751 snprintf(title, sizeof(title), "Error");
752 dialog_msgbox(title, prompt, 8, 72, 1);
755 fprintf(stderr, "%s\n", prompt);
756 return (DITEM_FAILURE | DITEM_RECREATE);
759 fd2 = open(path_localtime, O_CREAT | O_EXCL | O_WRONLY,
760 S_IRUSR | S_IRGRP | S_IROTH);
762 snprintf(title, sizeof(title), "Error");
763 snprintf(prompt, sizeof(prompt),
764 "Could not open %s: %s",
765 path_localtime, strerror(errno));
768 dialog_msgbox(title, prompt, 8, 72, 1);
771 fprintf(stderr, "%s\n", prompt);
772 return (DITEM_FAILURE | DITEM_RECREATE);
775 while ((len = read(fd1, buf, sizeof(buf))) > 0)
776 if ((len = write(fd2, buf, len)) < 0)
780 snprintf(title, sizeof(title), "Error");
781 snprintf(prompt, sizeof(prompt),
782 "Error copying %s to %s %s", zoneinfo_file,
783 path_localtime, strerror(errno));
786 dialog_msgbox(title, prompt, 8, 72, 1);
789 fprintf(stderr, "%s\n", prompt);
790 /* Better to leave none than a corrupt one. */
791 unlink(path_localtime);
792 return (DITEM_FAILURE | DITEM_RECREATE);
797 if (access(zoneinfo_file, R_OK) != 0) {
798 snprintf(title, sizeof(title), "Error");
799 snprintf(prompt, sizeof(prompt),
800 "Cannot access %s: %s", zoneinfo_file,
804 dialog_msgbox(title, prompt, 8, 72, 1);
807 fprintf(stderr, "%s\n", prompt);
808 return (DITEM_FAILURE | DITEM_RECREATE);
810 if (unlink(path_localtime) < 0 && errno != ENOENT) {
811 snprintf(prompt, sizeof(prompt),
812 "Could not delete %s: %s",
813 path_localtime, strerror(errno));
816 snprintf(title, sizeof(title), "Error");
817 dialog_msgbox(title, prompt, 8, 72, 1);
820 fprintf(stderr, "%s\n", prompt);
821 return (DITEM_FAILURE | DITEM_RECREATE);
823 if (symlink(zoneinfo_file, path_localtime) < 0) {
824 snprintf(title, sizeof(title), "Error");
825 snprintf(prompt, sizeof(prompt),
826 "Cannot create symbolic link %s to %s: %s",
827 path_localtime, zoneinfo_file,
831 dialog_msgbox(title, prompt, 8, 72, 1);
834 fprintf(stderr, "%s\n", prompt);
835 return (DITEM_FAILURE | DITEM_RECREATE);
840 snprintf(title, sizeof(title), "Done");
842 snprintf(prompt, sizeof(prompt),
843 "Copied timezone file from %s to %s",
844 zoneinfo_file, path_localtime);
846 snprintf(prompt, sizeof(prompt),
847 "Created symbolic link from %s to %s",
848 zoneinfo_file, path_localtime);
851 dialog_msgbox(title, prompt, 8, 72, 1);
854 fprintf(stderr, "%s\n", prompt);
858 return (DITEM_LEAVE_MENU);
862 install_zoneinfo(const char *zoneinfo)
866 char path_zoneinfo_file[MAXPATHLEN + 2];
868 if ((size_t)snprintf(path_zoneinfo_file, sizeof(path_zoneinfo_file),
869 "%s/%s", path_zoneinfo, zoneinfo) >= sizeof(path_zoneinfo_file))
870 errx(1, "%s/%s name too long", path_zoneinfo, zoneinfo);
871 rv = install_zoneinfo_file(path_zoneinfo_file);
873 /* Save knowledge for later */
874 if (reallydoit && (rv & DITEM_FAILURE) == 0) {
875 if ((f = fopen(path_db, "w")) != NULL) {
876 fprintf(f, "%s\n", zoneinfo);
888 fprintf(stderr, "usage: tzsetup [-nrs] [-C chroot_directory]"
889 " [zoneinfo_file | zoneinfo_name]\n");
894 main(int argc, char **argv)
897 char title[64], prompt[128];
902 char vmm_guest[16] = "";
903 size_t len = sizeof(vmm_guest);
908 /* Default skiputc to 1 for VM guests */
909 if (sysctlbyname("kern.vmm_guest", vmm_guest, &len, NULL, 0) == 0 &&
910 strcmp(vmm_guest, "none") != 0)
914 while ((c = getopt(argc, argv, "C:nrs")) != -1) {
938 if (argc - optind > 1)
941 if (chrootenv == NULL) {
942 strcpy(path_zonetab, _PATH_ZONETAB);
943 strcpy(path_iso3166, _PATH_ISO3166);
944 strcpy(path_zoneinfo, _PATH_ZONEINFO);
945 strcpy(path_localtime, _PATH_LOCALTIME);
946 strcpy(path_db, _PATH_DB);
947 strcpy(path_wall_cmos_clock, _PATH_WALL_CMOS_CLOCK);
949 sprintf(path_zonetab, "%s/%s", chrootenv, _PATH_ZONETAB);
950 sprintf(path_iso3166, "%s/%s", chrootenv, _PATH_ISO3166);
951 sprintf(path_zoneinfo, "%s/%s", chrootenv, _PATH_ZONEINFO);
952 sprintf(path_localtime, "%s/%s", chrootenv, _PATH_LOCALTIME);
953 sprintf(path_db, "%s/%s", chrootenv, _PATH_DB);
954 sprintf(path_wall_cmos_clock, "%s/%s", chrootenv,
955 _PATH_WALL_CMOS_CLOCK);
958 /* Override the user-supplied umask. */
959 umask(S_IWGRP | S_IWOTH);
961 if (reinstall == 1) {
963 char zoneinfo[MAXPATHLEN];
965 if ((f = fopen(path_db, "r")) != NULL) {
966 if (fgets(zoneinfo, sizeof(zoneinfo), f) != NULL) {
967 zoneinfo[sizeof(zoneinfo) - 1] = 0;
968 if (strlen(zoneinfo) > 0) {
969 zoneinfo[strlen(zoneinfo) - 1] = 0;
970 rv = install_zoneinfo(zoneinfo);
971 exit(rv & ~DITEM_LEAVE_MENU);
973 errx(1, "Error reading %s.\n", path_db);
977 "Unable to determine earlier installed zoneinfo "
978 "name. Check %s", path_db);
980 errx(1, "Cannot open %s for reading. Does it exist?", path_db);
984 * If the arguments on the command-line do not specify a file,
985 * then interpret it as a zoneinfo name
987 if (optind == argc - 1) {
990 if (stat(argv[optind], &sb) != 0) {
994 rv = install_zoneinfo(argv[optind]);
995 exit(rv & ~DITEM_LEAVE_MENU);
1001 read_iso3166_table();
1006 init_dialog(stdin, stdout);
1008 DIALOG_VARS save_vars;
1011 snprintf(title, sizeof(title),
1012 "Select local or UTC (Greenwich Mean Time) clock");
1013 snprintf(prompt, sizeof(prompt),
1014 "Is this machine's CMOS clock set to UTC? "
1015 "If it is set to local time,\n"
1016 "or you don't know, please choose NO here!");
1017 dlg_save_vars(&save_vars);
1018 yesno = dialog_yesno(title, prompt, 7, 73);
1019 dlg_restore_vars(&save_vars);
1022 unlink(path_wall_cmos_clock);
1025 fd = open(path_wall_cmos_clock,
1026 O_WRONLY | O_CREAT | O_TRUNC,
1027 S_IRUSR | S_IRGRP | S_IROTH);
1031 path_wall_cmos_clock);
1038 if (optind == argc - 1) {
1039 snprintf(title, sizeof(title), "Default timezone provided");
1040 snprintf(prompt, sizeof(prompt),
1041 "\nUse the default `%s' zone?", argv[optind]);
1042 if (!dialog_yesno(title, prompt, 7, 72)) {
1043 rv = install_zoneinfo_file(argv[optind]);
1046 exit(rv & ~DITEM_LEAVE_MENU);
1050 snprintf(title, sizeof(title), "Time Zone Selector");
1051 snprintf(prompt, sizeof(prompt), "Select a region");
1052 xdialog_menu(title, prompt, -1, -1, NCONTINENTS, NCONTINENTS,