1 /* 2 * Copyright 1996 Massachusetts Institute of Technology 3 * 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 14 * warranty. 15 * 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 27 * SUCH DAMAGE. 28 */ 29 30 /* 31 * Second attempt at a `tzmenu' program, using the separate description 32 * files provided in newer tzdata releases. 33 */ 34 35 #include <sys/cdefs.h> 36 __FBSDID("$FreeBSD$"); 37 38 #include <dialog.h> 39 #include <err.h> 40 #include <errno.h> 41 #include <stdio.h> 42 #include <stdlib.h> 43 #include <string.h> 44 #include <time.h> 45 #include <unistd.h> 46 47 #include <sys/fcntl.h> 48 #include <sys/param.h> 49 #include <sys/queue.h> 50 #include <sys/stat.h> 51 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" 58 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]; 62 63 static int reallydoit = 1; 64 static int reinstall = 0; 65 static int usedialog = 1; 66 static char *chrootenv = NULL; 67 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); 76 77 struct continent { 78 dialogMenuItem *menu; 79 int nitems; 80 int ch; 81 int sc; 82 }; 83 84 static struct continent africa, america, antarctica, arctic, asia, atlantic; 85 static struct continent australia, europe, indian, pacific, utc; 86 87 static struct continent_names { 88 const char *name; 89 struct continent *continent; 90 } continent_names[] = { 91 { "Africa", &africa }, 92 { "America", &america }, 93 { "Antarctica", &antarctica }, 94 { "Arctic", &arctic }, 95 { "Asia", &asia }, 96 { "Atlantic", &atlantic }, 97 { "Australia", &australia }, 98 { "Europe", &europe }, 99 { "Indian", &indian }, 100 { "Pacific", &pacific }, 101 { "UTC", &utc } 102 }; 103 104 static struct continent_items { 105 char prompt[2]; 106 char title[30]; 107 } continent_items[] = { 108 { "1", "Africa" }, 109 { "2", "America -- North and South" }, 110 { "3", "Antarctica" }, 111 { "4", "Arctic Ocean" }, 112 { "5", "Asia" }, 113 { "6", "Atlantic Ocean" }, 114 { "7", "Australia" }, 115 { "8", "Europe" }, 116 { "9", "Indian Ocean" }, 117 { "0", "Pacific Ocean" }, 118 { "a", "UTC" } 119 }; 120 121 #define NCONTINENTS \ 122 (int)((sizeof(continent_items)) / (sizeof(continent_items[0]))) 123 static dialogMenuItem continents[NCONTINENTS]; 124 125 #define OCEANP(x) ((x) == 3 || (x) == 5 || (x) == 8 || (x) == 9) 126 127 static int 128 continent_country_menu(dialogMenuItem *continent) 129 { 130 char title[64], prompt[64]; 131 struct continent *contp = continent->data; 132 int isocean = OCEANP(continent - continents); 133 int menulen; 134 int rv; 135 136 if (strcmp(continent->title, "UTC") == 0) 137 return set_zone_utc(); 138 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])); 142 143 /* It's amazing how much good grammar really matters... */ 144 if (!isocean) { 145 snprintf(title, sizeof(title), "Countries in %s", 146 continent->title); 147 snprintf(prompt, sizeof(prompt), "Select a country or region"); 148 } else { 149 snprintf(title, sizeof(title), "Islands and groups in the %s", 150 continent->title); 151 snprintf(prompt, sizeof(prompt), "Select an island or group"); 152 } 153 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); 157 if (rv == 0) 158 return (DITEM_LEAVE_MENU); 159 return (DITEM_RECREATE); 160 } 161 162 static struct continent * 163 find_continent(const char *name) 164 { 165 int i; 166 167 for (i = 0; i < NCONTINENTS; i++) 168 if (strcmp(name, continent_names[i].name) == 0) 169 return (continent_names[i].continent); 170 return (0); 171 } 172 173 struct country { 174 char *name; 175 char *tlc; 176 int nzones; 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 */ 181 }; 182 183 struct zone { 184 TAILQ_ENTRY(zone) link; 185 char *descr; 186 char *filename; 187 struct continent *continent; 188 }; 189 190 /* 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. 194 */ 195 #define NCOUNTRIES (26 * 26) 196 static struct country countries[NCOUNTRIES]; 197 198 #define CODE2INT(s) ((s[0] - 'A') * 26 + (s[1] - 'A')) 199 200 /* 201 * Read the ISO 3166 country code database in _PATH_ISO3166 202 * (/usr/share/misc/iso3166). On error, exit via err(3). 203 */ 204 static void 205 read_iso3166_table(void) 206 { 207 FILE *fp; 208 struct country *cp; 209 size_t len; 210 char *s, *t, *name; 211 int lineno; 212 213 fp = fopen(path_iso3166, "r"); 214 if (!fp) 215 err(1, "%s", path_iso3166); 216 lineno = 0; 217 218 while ((s = fgetln(fp, &len)) != 0) { 219 lineno++; 220 if (s[len - 1] != '\n') 221 errx(1, "%s:%d: invalid format", path_iso3166, lineno); 222 s[len - 1] = '\0'; 223 if (s[0] == '#' || strspn(s, " \t") == len - 1) 224 continue; 225 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, 232 lineno, t); 233 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); 241 242 name = s; 243 244 cp = &countries[CODE2INT(t)]; 245 if (cp->name) 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"); 251 cp->tlc = strdup(t); 252 if (cp->tlc == NULL) 253 errx(1, "malloc failed"); 254 } 255 256 fclose(fp); 257 } 258 259 static void 260 add_zone_to_country(int lineno, const char *tlc, const char *descr, 261 const char *file, struct continent *cont) 262 { 263 struct zone *zp; 264 struct country *cp; 265 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, 268 lineno, tlc); 269 270 cp = &countries[CODE2INT(tlc)]; 271 if (cp->name == 0) 272 errx(1, "%s:%d: country code `%s' unknown", path_zonetab, 273 lineno, tlc); 274 275 if (descr) { 276 if (cp->nzones < 0) 277 errx(1, "%s:%d: conflicting zone definition", 278 path_zonetab, lineno); 279 280 zp = malloc(sizeof(*zp)); 281 if (zp == 0) 282 errx(1, "malloc(%zu)", sizeof(*zp)); 283 284 if (cp->nzones == 0) 285 TAILQ_INIT(&cp->zones); 286 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); 295 cp->nzones++; 296 } else { 297 if (cp->nzones > 0) 298 errx(1, "%s:%d: zone must have description", 299 path_zonetab, lineno); 300 if (cp->nzones < 0) 301 errx(1, "%s:%d: zone multiply defined", 302 path_zonetab, lineno); 303 cp->nzones = -1; 304 cp->filename = strdup(file); 305 if (cp->filename == NULL) 306 errx(1, "malloc failed"); 307 cp->continent = cont; 308 } 309 } 310 311 /* 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. 315 */ 316 static int 317 compare_countries(const void *xa, const void *xb) 318 { 319 const struct country *a = xa, *b = xb; 320 321 if (a->name == 0 && b->name == 0) 322 return (0); 323 if (a->name == 0 && b->name != 0) 324 return (1); 325 if (b->name == 0) 326 return (-1); 327 328 return (strcmp(a->name, b->name)); 329 } 330 331 /* 332 * This must be done AFTER all zone descriptions are read, since it breaks 333 * CODE2INT(). 334 */ 335 static void 336 sort_countries(void) 337 { 338 339 qsort(countries, NCOUNTRIES, sizeof(countries[0]), compare_countries); 340 } 341 342 static void 343 read_zones(void) 344 { 345 char contbuf[16]; 346 FILE *fp; 347 struct continent *cont; 348 size_t len; 349 char *line, *tlc, *coord, *file, *descr, *p; 350 int lineno; 351 352 fp = fopen(path_zonetab, "r"); 353 if (!fp) 354 err(1, "%s", path_zonetab); 355 lineno = 0; 356 357 while ((line = fgetln(fp, &len)) != 0) { 358 lineno++; 359 if (line[len - 1] != '\n') 360 errx(1, "%s:%d: invalid format", path_zonetab, lineno); 361 line[len - 1] = '\0'; 362 if (line[0] == '#') 363 continue; 364 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, '/'); 372 if (p == 0) 373 errx(1, "%s:%d: invalid zone name `%s'", path_zonetab, 374 lineno, file); 375 contbuf[0] = '\0'; 376 strncat(contbuf, file, p - file); 377 cont = find_continent(contbuf); 378 if (!cont) 379 errx(1, "%s:%d: invalid region `%s'", path_zonetab, 380 lineno, contbuf); 381 382 descr = (line != NULL && *line != '\0') ? line : NULL; 383 384 add_zone_to_country(lineno, tlc, descr, file, cont); 385 } 386 fclose(fp); 387 } 388 389 static void 390 make_menus(void) 391 { 392 struct country *cp; 393 struct zone *zp, *zp2; 394 struct continent *cont; 395 dialogMenuItem *dmi; 396 int i; 397 398 /* 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 402 * continents/oceans. 403 */ 404 for (cp = countries; cp->name; cp++) { 405 if (cp->nzones == 0) 406 continue; 407 if (cp->nzones < 0) { 408 cp->continent->nitems++; 409 } else { 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)) 415 ; 416 if (zp2 == zp) 417 zp->continent->nitems++; 418 } 419 } 420 } 421 422 /* 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. 426 */ 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; 439 } 440 441 /* 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. 445 */ 446 for (cp = countries; cp->name; cp++) { 447 if (cp->nzones == 0) 448 continue; 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; 454 dmi->checked = 0; 455 dmi->fire = set_zone_whole_country; 456 dmi->selected = 0; 457 dmi->data = cp; 458 } else { 459 cp->submenu = malloc(cp->nzones * sizeof(*dmi)); 460 if (cp->submenu == 0) 461 errx(1, "malloc for submenu"); 462 cp->nzones = 0; 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; 469 dmi->checked = 0; 470 dmi->fire = set_zone_multi; 471 dmi->selected = 0; 472 dmi->data = zp; 473 474 for (zp2 = TAILQ_FIRST(&cp->zones); 475 zp2->continent != cont; 476 zp2 = TAILQ_NEXT(zp2, link)) 477 ; 478 if (zp2 != zp) 479 continue; 480 481 dmi = &cont->menu[cont->nitems]; 482 memset(dmi, 0, sizeof(*dmi)); 483 asprintf(&dmi->prompt, "%d", ++cont->nitems); 484 dmi->title = cp->name; 485 dmi->checked = 0; 486 dmi->fire = set_zone_menu; 487 dmi->selected = 0; 488 dmi->data = cp; 489 } 490 } 491 } 492 } 493 494 static int 495 set_zone_menu(dialogMenuItem *dmi) 496 { 497 char title[64], prompt[64]; 498 struct country *cp = dmi->data; 499 int menulen; 500 int rv; 501 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); 508 if (rv != 0) 509 return (DITEM_RECREATE); 510 return (DITEM_LEAVE_MENU); 511 } 512 513 int 514 set_zone_utc(void) 515 { 516 if (!confirm_zone(NULL)) 517 return (DITEM_FAILURE | DITEM_RECREATE); 518 519 return (install_zoneinfo_file(NULL)); 520 } 521 522 static int 523 install_zoneinfo_file(const char *zoneinfo_file) 524 { 525 char buf[1024]; 526 char title[64], prompt[64]; 527 struct stat sb; 528 ssize_t len; 529 int fd1, fd2, copymode; 530 531 if (lstat(path_localtime, &sb) < 0) { 532 /* Nothing there yet... */ 533 copymode = 1; 534 } else if (S_ISLNK(sb.st_mode)) 535 copymode = 0; 536 else 537 copymode = 1; 538 539 #ifdef VERBOSE 540 if (copymode) 541 snprintf(prompt, sizeof(prompt), 542 "Copying %s to %s", zoneinfo_file, path_localtime); 543 else 544 snprintf(prompt, sizeof(prompt), 545 "Creating symbolic link %s to %s", 546 path_localtime, 547 zoneinfo_file == NULL ? "(UTC)" : zoneinfo_file); 548 if (usedialog) 549 dialog_notify(prompt); 550 else 551 fprintf(stderr, "%s\n", prompt); 552 #endif 553 554 if (reallydoit) { 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, 560 strerror(errno)); 561 if (usedialog) 562 dialog_mesgbox(title, prompt, 8, 72); 563 else 564 fprintf(stderr, "%s\n", prompt); 565 566 return (DITEM_FAILURE | DITEM_RECREATE); 567 } 568 return (DITEM_LEAVE_MENU); 569 } 570 571 if (copymode) { 572 fd1 = open(zoneinfo_file, O_RDONLY, 0); 573 if (fd1 < 0) { 574 snprintf(title, sizeof(title), "Error"); 575 snprintf(prompt, sizeof(prompt), 576 "Could not open %s: %s", zoneinfo_file, 577 strerror(errno)); 578 if (usedialog) 579 dialog_mesgbox(title, prompt, 8, 72); 580 else 581 fprintf(stderr, "%s\n", prompt); 582 return (DITEM_FAILURE | DITEM_RECREATE); 583 } 584 585 unlink(path_localtime); 586 fd2 = open(path_localtime, O_CREAT | O_EXCL | O_WRONLY, 587 S_IRUSR | S_IRGRP | S_IROTH); 588 if (fd2 < 0) { 589 snprintf(title, sizeof(title), "Error"); 590 snprintf(prompt, sizeof(prompt), 591 "Could not open %s: %s", 592 path_localtime, strerror(errno)); 593 if (usedialog) 594 dialog_mesgbox(title, prompt, 8, 72); 595 else 596 fprintf(stderr, "%s\n", prompt); 597 return (DITEM_FAILURE | DITEM_RECREATE); 598 } 599 600 while ((len = read(fd1, buf, sizeof(buf))) > 0) 601 if ((len = write(fd2, buf, len)) < 0) 602 break; 603 604 if (len == -1) { 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)); 609 if (usedialog) 610 dialog_mesgbox(title, prompt, 8, 72); 611 else 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); 616 } 617 close(fd1); 618 close(fd2); 619 } else { 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, 624 strerror(errno)); 625 if (usedialog) 626 dialog_mesgbox(title, prompt, 8, 72); 627 else 628 fprintf(stderr, "%s\n", prompt); 629 return (DITEM_FAILURE | DITEM_RECREATE); 630 } 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, 637 strerror(errno)); 638 if (usedialog) 639 dialog_mesgbox(title, prompt, 8, 72); 640 else 641 fprintf(stderr, "%s\n", prompt); 642 return (DITEM_FAILURE | DITEM_RECREATE); 643 } 644 } 645 } 646 647 #ifdef VERBOSE 648 snprintf(title, sizeof(title), "Done"); 649 if (copymode) 650 snprintf(prompt, sizeof(prompt), 651 "Copied timezone file from %s to %s", zoneinfo_file, 652 path_localtime); 653 else 654 snprintf(prompt, sizeof(prompt), 655 "Created symbolic link from %s to %s", zoneinfo_file, 656 path_localtime); 657 if (usedialog) 658 dialog_mesgbox(title, prompt, 8, 72); 659 else 660 fprintf(stderr, "%s\n", prompt); 661 #endif 662 663 return (DITEM_LEAVE_MENU); 664 } 665 666 static int 667 install_zoneinfo(const char *zoneinfo) 668 { 669 int rv; 670 FILE *f; 671 char path_zoneinfo_file[MAXPATHLEN]; 672 673 sprintf(path_zoneinfo_file, "%s/%s", path_zoneinfo, zoneinfo); 674 rv = install_zoneinfo_file(path_zoneinfo_file); 675 676 /* Save knowledge for later */ 677 if ((f = fopen(path_db, "w")) != NULL) { 678 fprintf(f, "%s\n", zoneinfo); 679 fclose(f); 680 } 681 682 return (rv); 683 } 684 685 static int 686 confirm_zone(const char *filename) 687 { 688 char title[64], prompt[64]; 689 time_t t = time(0); 690 struct tm *tm; 691 int rv; 692 693 setenv("TZ", filename == NULL ? "" : filename, 1); 694 tzset(); 695 tm = localtime(&t); 696 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); 701 return (rv); 702 } 703 704 static int 705 set_zone_multi(dialogMenuItem *dmi) 706 { 707 struct zone *zp = dmi->data; 708 int rv; 709 710 if (!confirm_zone(zp->filename)) 711 return (DITEM_FAILURE | DITEM_RECREATE); 712 713 rv = install_zoneinfo(zp->filename); 714 return (rv); 715 } 716 717 static int 718 set_zone_whole_country(dialogMenuItem *dmi) 719 { 720 struct country *cp = dmi->data; 721 int rv; 722 723 if (!confirm_zone(cp->filename)) 724 return (DITEM_FAILURE | DITEM_RECREATE); 725 726 rv = install_zoneinfo(cp->filename); 727 return (rv); 728 } 729 730 static void 731 usage(void) 732 { 733 734 fprintf(stderr, "usage: tzsetup [-nrs] [-C chroot_directory]" 735 " [zoneinfo_file | zoneinfo_name]\n"); 736 exit(1); 737 } 738 739 #if defined(__sparc64__) 740 #define DIALOG_UTC dialog_yesno 741 #else 742 #define DIALOG_UTC dialog_noyes 743 #endif 744 745 int 746 main(int argc, char **argv) 747 { 748 char title[64], prompt[128]; 749 int c, fd, rv, skiputc; 750 751 skiputc = 0; 752 while ((c = getopt(argc, argv, "C:nrs")) != -1) { 753 switch(c) { 754 case 'C': 755 chrootenv = optarg; 756 break; 757 case 'n': 758 reallydoit = 0; 759 break; 760 case 'r': 761 reinstall = 1; 762 usedialog = 0; 763 break; 764 case 's': 765 skiputc = 1; 766 break; 767 default: 768 usage(); 769 } 770 } 771 772 if (argc - optind > 1) 773 usage(); 774 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); 782 } else { 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); 790 } 791 792 793 /* Override the user-supplied umask. */ 794 (void)umask(S_IWGRP | S_IWOTH); 795 796 read_iso3166_table(); 797 read_zones(); 798 sort_countries(); 799 make_menus(); 800 801 if (reinstall == 1) { 802 FILE *f; 803 char zonefile[MAXPATHLEN]; 804 char path_db[MAXPATHLEN]; 805 806 zonefile[0] = '\0'; 807 path_db[0] = '\0'; 808 if (chrootenv != NULL) { 809 sprintf(zonefile, "%s/", chrootenv); 810 sprintf(path_db, "%s/", chrootenv); 811 } 812 strcat(zonefile, _PATH_ZONEINFO); 813 strcat(zonefile, "/"); 814 strcat(path_db, _PATH_DB); 815 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); 823 } 824 errx(1, "Error reading %s.\n", path_db); 825 } 826 fclose(f); 827 errx(1, 828 "Unable to determine earlier installed zoneinfo " 829 "file. Check %s", path_db); 830 } 831 errx(1, "Cannot open %s for reading. Does it exist?", path_db); 832 } 833 834 /* 835 * If the arguments on the command-line do not specify a file, 836 * then interpret it as a zoneinfo name 837 */ 838 if (optind == argc - 1) { 839 struct stat sb; 840 841 if (stat(argv[optind], &sb) != 0) { 842 usedialog = 0; 843 rv = install_zoneinfo(argv[optind]); 844 exit(rv & ~DITEM_LEAVE_MENU); 845 } 846 /* FALLTHROUGH */ 847 } 848 849 init_dialog(); 850 if (skiputc == 0) { 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)) { 858 if (reallydoit) 859 unlink(path_wall_cmos_clock); 860 } else { 861 if (reallydoit) { 862 fd = open(path_wall_cmos_clock, 863 O_WRONLY | O_CREAT | O_TRUNC, 864 S_IRUSR | S_IRGRP | S_IROTH); 865 if (fd < 0) { 866 end_dialog(); 867 err(1, "create %s", 868 path_wall_cmos_clock); 869 } 870 close(fd); 871 } 872 } 873 dialog_clear_norefresh(); 874 } 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]); 881 dialog_clear(); 882 end_dialog(); 883 exit(rv & ~DITEM_LEAVE_MENU); 884 } 885 dialog_clear_norefresh(); 886 } 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); 891 892 dialog_clear(); 893 end_dialog(); 894 return (0); 895 } 896