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 continent_country_menu(dialogMenuItem *); 70 static int set_zone_multi(dialogMenuItem *); 71 static int set_zone_whole_country(dialogMenuItem *); 72 static int set_zone_menu(dialogMenuItem *); 73 74 struct continent { 75 dialogMenuItem *menu; 76 int nitems; 77 int ch; 78 int sc; 79 }; 80 81 static struct continent africa, america, antarctica, arctic, asia, atlantic; 82 static struct continent australia, europe, indian, pacific; 83 84 static struct continent_names { 85 const char *name; 86 struct continent *continent; 87 } continent_names[] = { 88 { "Africa", &africa }, 89 { "America", &america }, 90 { "Antarctica", &antarctica }, 91 { "Arctic", &arctic }, 92 { "Asia", &asia }, 93 { "Atlantic", &atlantic }, 94 { "Australia", &australia }, 95 { "Europe", &europe }, 96 { "Indian", &indian }, 97 { "Pacific", &pacific } 98 }; 99 100 static struct continent_items { 101 char prompt[2]; 102 char title[30]; 103 } continent_items[] = { 104 { "1", "Africa" }, 105 { "2", "America -- North and South" }, 106 { "3", "Antarctica" }, 107 { "4", "Arctic Ocean" }, 108 { "5", "Asia" }, 109 { "6", "Atlantic Ocean" }, 110 { "7", "Australia" }, 111 { "8", "Europe" }, 112 { "9", "Indian Ocean" }, 113 { "0", "Pacific Ocean" } 114 }; 115 116 #define NCONTINENTS \ 117 (int)((sizeof(continent_items)) / (sizeof(continent_items[0]))) 118 static dialogMenuItem continents[NCONTINENTS]; 119 120 #define OCEANP(x) ((x) == 3 || (x) == 5 || (x) == 8 || (x) == 9) 121 122 static int 123 continent_country_menu(dialogMenuItem *continent) 124 { 125 char title[64], prompt[64]; 126 struct continent *contp = continent->data; 127 int isocean = OCEANP(continent - continents); 128 int menulen; 129 int rv; 130 131 /* Short cut -- if there's only one country, don't post a menu. */ 132 if (contp->nitems == 1) 133 return (contp->menu[0].fire(&contp->menu[0])); 134 135 /* It's amazing how much good grammar really matters... */ 136 if (!isocean) { 137 snprintf(title, sizeof(title), "Countries in %s", 138 continent->title); 139 snprintf(prompt, sizeof(prompt), "Select a country or region"); 140 } else { 141 snprintf(title, sizeof(title), "Islands and groups in the %s", 142 continent->title); 143 snprintf(prompt, sizeof(prompt), "Select an island or group"); 144 } 145 146 menulen = contp->nitems < 16 ? contp->nitems : 16; 147 rv = dialog_menu(title, prompt, -1, -1, menulen, -contp->nitems, 148 contp->menu, 0, &contp->ch, &contp->sc); 149 if (rv == 0) 150 return (DITEM_LEAVE_MENU); 151 return (DITEM_RECREATE); 152 } 153 154 static struct continent * 155 find_continent(const char *name) 156 { 157 int i; 158 159 for (i = 0; i < NCONTINENTS; i++) 160 if (strcmp(name, continent_names[i].name) == 0) 161 return (continent_names[i].continent); 162 return (0); 163 } 164 165 struct country { 166 char *name; 167 char *tlc; 168 int nzones; 169 char *filename; /* use iff nzones < 0 */ 170 struct continent *continent; /* use iff nzones < 0 */ 171 TAILQ_HEAD(, zone) zones; /* use iff nzones > 0 */ 172 dialogMenuItem *submenu; /* use iff nzones > 0 */ 173 }; 174 175 struct zone { 176 TAILQ_ENTRY(zone) link; 177 char *descr; 178 char *filename; 179 struct continent *continent; 180 }; 181 182 /* 183 * This is the easiest organization... we use ISO 3166 country codes, 184 * of the two-letter variety, so we just size this array to suit. 185 * Beats worrying about dynamic allocation. 186 */ 187 #define NCOUNTRIES (26 * 26) 188 static struct country countries[NCOUNTRIES]; 189 190 #define CODE2INT(s) ((s[0] - 'A') * 26 + (s[1] - 'A')) 191 192 /* 193 * Read the ISO 3166 country code database in _PATH_ISO3166 194 * (/usr/share/misc/iso3166). On error, exit via err(3). 195 */ 196 static void 197 read_iso3166_table(void) 198 { 199 FILE *fp; 200 struct country *cp; 201 size_t len; 202 char *s, *t, *name; 203 int lineno; 204 205 fp = fopen(path_iso3166, "r"); 206 if (!fp) 207 err(1, path_iso3166); 208 lineno = 0; 209 210 while ((s = fgetln(fp, &len)) != 0) { 211 lineno++; 212 if (s[len - 1] != '\n') 213 errx(1, "%s:%d: invalid format", path_iso3166, lineno); 214 s[len - 1] = '\0'; 215 if (s[0] == '#' || strspn(s, " \t") == len - 1) 216 continue; 217 218 /* Isolate the two-letter code. */ 219 t = strsep(&s, "\t"); 220 if (t == 0 || strlen(t) != 2) 221 errx(1, "%s:%d: invalid format", path_iso3166, lineno); 222 if (t[0] < 'A' || t[0] > 'Z' || t[1] < 'A' || t[1] > 'Z') 223 errx(1, "%s:%d: invalid code `%s'", path_iso3166, 224 lineno, t); 225 226 /* Now skip past the three-letter and numeric codes. */ 227 name = strsep(&s, "\t"); /* 3-let */ 228 if (name == 0 || strlen(name) != 3) 229 errx(1, "%s:%d: invalid format", path_iso3166, lineno); 230 name = strsep(&s, "\t"); /* numeric */ 231 if (name == 0 || strlen(name) != 3) 232 errx(1, "%s:%d: invalid format", path_iso3166, lineno); 233 234 name = s; 235 236 cp = &countries[CODE2INT(t)]; 237 if (cp->name) 238 errx(1, "%s:%d: country code `%s' multiply defined: %s", 239 path_iso3166, lineno, t, cp->name); 240 cp->name = strdup(name); 241 if (cp->name == NULL) 242 errx(1, "malloc failed"); 243 cp->tlc = strdup(t); 244 if (cp->tlc == NULL) 245 errx(1, "malloc failed"); 246 } 247 248 fclose(fp); 249 } 250 251 static void 252 add_zone_to_country(int lineno, const char *tlc, const char *descr, 253 const char *file, struct continent *cont) 254 { 255 struct zone *zp; 256 struct country *cp; 257 258 if (tlc[0] < 'A' || tlc[0] > 'Z' || tlc[1] < 'A' || tlc[1] > 'Z') 259 errx(1, "%s:%d: country code `%s' invalid", path_zonetab, 260 lineno, tlc); 261 262 cp = &countries[CODE2INT(tlc)]; 263 if (cp->name == 0) 264 errx(1, "%s:%d: country code `%s' unknown", path_zonetab, 265 lineno, tlc); 266 267 if (descr) { 268 if (cp->nzones < 0) 269 errx(1, "%s:%d: conflicting zone definition", 270 path_zonetab, lineno); 271 272 zp = malloc(sizeof(*zp)); 273 if (zp == 0) 274 errx(1, "malloc(%zu)", sizeof(*zp)); 275 276 if (cp->nzones == 0) 277 TAILQ_INIT(&cp->zones); 278 279 zp->descr = strdup(descr); 280 if (zp->descr == NULL) 281 errx(1, "malloc failed"); 282 zp->filename = strdup(file); 283 if (zp->filename == NULL) 284 errx(1, "malloc failed"); 285 zp->continent = cont; 286 TAILQ_INSERT_TAIL(&cp->zones, zp, link); 287 cp->nzones++; 288 } else { 289 if (cp->nzones > 0) 290 errx(1, "%s:%d: zone must have description", 291 path_zonetab, lineno); 292 if (cp->nzones < 0) 293 errx(1, "%s:%d: zone multiply defined", 294 path_zonetab, lineno); 295 cp->nzones = -1; 296 cp->filename = strdup(file); 297 if (cp->filename == NULL) 298 errx(1, "malloc failed"); 299 cp->continent = cont; 300 } 301 } 302 303 /* 304 * This comparison function intentionally sorts all of the null-named 305 * ``countries''---i.e., the codes that don't correspond to a real 306 * country---to the end. Everything else is lexical by country name. 307 */ 308 static int 309 compare_countries(const void *xa, const void *xb) 310 { 311 const struct country *a = xa, *b = xb; 312 313 if (a->name == 0 && b->name == 0) 314 return (0); 315 if (a->name == 0 && b->name != 0) 316 return (1); 317 if (b->name == 0) 318 return (-1); 319 320 return (strcmp(a->name, b->name)); 321 } 322 323 /* 324 * This must be done AFTER all zone descriptions are read, since it breaks 325 * CODE2INT(). 326 */ 327 static void 328 sort_countries(void) 329 { 330 331 qsort(countries, NCOUNTRIES, sizeof(countries[0]), compare_countries); 332 } 333 334 static void 335 read_zones(void) 336 { 337 char contbuf[16]; 338 FILE *fp; 339 struct continent *cont; 340 size_t len; 341 char *line, *tlc, *coord, *file, *descr, *p; 342 int lineno; 343 344 fp = fopen(path_zonetab, "r"); 345 if (!fp) 346 err(1, path_zonetab); 347 lineno = 0; 348 349 while ((line = fgetln(fp, &len)) != 0) { 350 lineno++; 351 if (line[len - 1] != '\n') 352 errx(1, "%s:%d: invalid format", path_zonetab, lineno); 353 line[len - 1] = '\0'; 354 if (line[0] == '#') 355 continue; 356 357 tlc = strsep(&line, "\t"); 358 if (strlen(tlc) != 2) 359 errx(1, "%s:%d: invalid country code `%s'", 360 path_zonetab, lineno, tlc); 361 coord = strsep(&line, "\t"); 362 file = strsep(&line, "\t"); 363 p = strchr(file, '/'); 364 if (p == 0) 365 errx(1, "%s:%d: invalid zone name `%s'", path_zonetab, 366 lineno, file); 367 contbuf[0] = '\0'; 368 strncat(contbuf, file, p - file); 369 cont = find_continent(contbuf); 370 if (!cont) 371 errx(1, "%s:%d: invalid region `%s'", path_zonetab, 372 lineno, contbuf); 373 374 descr = (line != NULL && *line != '\0') ? line : NULL; 375 376 add_zone_to_country(lineno, tlc, descr, file, cont); 377 } 378 fclose(fp); 379 } 380 381 static void 382 make_menus(void) 383 { 384 struct country *cp; 385 struct zone *zp, *zp2; 386 struct continent *cont; 387 dialogMenuItem *dmi; 388 int i; 389 390 /* 391 * First, count up all the countries in each continent/ocean. 392 * Be careful to count those countries which have multiple zones 393 * only once for each. NB: some countries are in multiple 394 * continents/oceans. 395 */ 396 for (cp = countries; cp->name; cp++) { 397 if (cp->nzones == 0) 398 continue; 399 if (cp->nzones < 0) { 400 cp->continent->nitems++; 401 } else { 402 TAILQ_FOREACH(zp, &cp->zones, link) { 403 cont = zp->continent; 404 for (zp2 = TAILQ_FIRST(&cp->zones); 405 zp2->continent != cont; 406 zp2 = TAILQ_NEXT(zp2, link)) 407 ; 408 if (zp2 == zp) 409 zp->continent->nitems++; 410 } 411 } 412 } 413 414 /* 415 * Now allocate memory for the country menus and initialize 416 * continent menus. We set nitems back to zero so that we can 417 * use it for counting again when we actually build the menus. 418 */ 419 memset(continents, 0, sizeof(continents)); 420 for (i = 0; i < NCONTINENTS; i++) { 421 continent_names[i].continent->menu = 422 malloc(sizeof(dialogMenuItem) * 423 continent_names[i].continent->nitems); 424 if (continent_names[i].continent->menu == 0) 425 errx(1, "malloc for continent menu"); 426 continent_names[i].continent->nitems = 0; 427 continents[i].prompt = continent_items[i].prompt; 428 continents[i].title = continent_items[i].title; 429 continents[i].fire = continent_country_menu; 430 continents[i].data = continent_names[i].continent; 431 } 432 433 /* 434 * Now that memory is allocated, create the menu items for 435 * each continent. For multiple-zone countries, also create 436 * the country's zone submenu. 437 */ 438 for (cp = countries; cp->name; cp++) { 439 if (cp->nzones == 0) 440 continue; 441 if (cp->nzones < 0) { 442 dmi = &cp->continent->menu[cp->continent->nitems]; 443 memset(dmi, 0, sizeof(*dmi)); 444 asprintf(&dmi->prompt, "%d", ++cp->continent->nitems); 445 dmi->title = cp->name; 446 dmi->checked = 0; 447 dmi->fire = set_zone_whole_country; 448 dmi->selected = 0; 449 dmi->data = cp; 450 } else { 451 cp->submenu = malloc(cp->nzones * sizeof(*dmi)); 452 if (cp->submenu == 0) 453 errx(1, "malloc for submenu"); 454 cp->nzones = 0; 455 TAILQ_FOREACH(zp, &cp->zones, link) { 456 cont = zp->continent; 457 dmi = &cp->submenu[cp->nzones]; 458 memset(dmi, 0, sizeof(*dmi)); 459 asprintf(&dmi->prompt, "%d", ++cp->nzones); 460 dmi->title = zp->descr; 461 dmi->checked = 0; 462 dmi->fire = set_zone_multi; 463 dmi->selected = 0; 464 dmi->data = zp; 465 466 for (zp2 = TAILQ_FIRST(&cp->zones); 467 zp2->continent != cont; 468 zp2 = TAILQ_NEXT(zp2, link)) 469 ; 470 if (zp2 != zp) 471 continue; 472 473 dmi = &cont->menu[cont->nitems]; 474 memset(dmi, 0, sizeof(*dmi)); 475 asprintf(&dmi->prompt, "%d", ++cont->nitems); 476 dmi->title = cp->name; 477 dmi->checked = 0; 478 dmi->fire = set_zone_menu; 479 dmi->selected = 0; 480 dmi->data = cp; 481 } 482 } 483 } 484 } 485 486 static int 487 set_zone_menu(dialogMenuItem *dmi) 488 { 489 char title[64], prompt[64]; 490 struct country *cp = dmi->data; 491 int menulen; 492 int rv; 493 494 snprintf(title, sizeof(title), "%s Time Zones", cp->name); 495 snprintf(prompt, sizeof(prompt), 496 "Select a zone which observes the same time as your locality."); 497 menulen = cp->nzones < 16 ? cp->nzones : 16; 498 rv = dialog_menu(title, prompt, -1, -1, menulen, -cp->nzones, 499 cp->submenu, 0, 0, 0); 500 if (rv != 0) 501 return (DITEM_RECREATE); 502 return (DITEM_LEAVE_MENU); 503 } 504 505 static int 506 install_zoneinfo_file(const char *zoneinfo_file) 507 { 508 char buf[1024]; 509 char title[64], prompt[64]; 510 struct stat sb; 511 ssize_t len; 512 int fd1, fd2, copymode; 513 514 if (lstat(path_localtime, &sb) < 0) { 515 /* Nothing there yet... */ 516 copymode = 1; 517 } else if (S_ISLNK(sb.st_mode)) 518 copymode = 0; 519 else 520 copymode = 1; 521 522 #ifdef VERBOSE 523 if (copymode) 524 snprintf(prompt, sizeof(prompt), 525 "Copying %s to %s", zoneinfo_file, path_localtime); 526 else 527 snprintf(prompt, sizeof(prompt), 528 "Creating symbolic link %s to %s", 529 path_localtime, zoneinfo_file); 530 if (usedialog) 531 dialog_notify(prompt); 532 else 533 fprintf(stderr, "%s\n", prompt); 534 #endif 535 536 if (reallydoit) { 537 if (copymode) { 538 fd1 = open(zoneinfo_file, O_RDONLY, 0); 539 if (fd1 < 0) { 540 snprintf(title, sizeof(title), "Error"); 541 snprintf(prompt, sizeof(prompt), 542 "Could not open %s: %s", zoneinfo_file, 543 strerror(errno)); 544 if (usedialog) 545 dialog_mesgbox(title, prompt, 8, 72); 546 else 547 fprintf(stderr, "%s\n", prompt); 548 return (DITEM_FAILURE | DITEM_RECREATE); 549 } 550 551 unlink(path_localtime); 552 fd2 = open(path_localtime, O_CREAT | O_EXCL | O_WRONLY, 553 S_IRUSR | S_IRGRP | S_IROTH); 554 if (fd2 < 0) { 555 snprintf(title, sizeof(title), "Error"); 556 snprintf(prompt, sizeof(prompt), 557 "Could not open %s: %s", 558 path_localtime, strerror(errno)); 559 if (usedialog) 560 dialog_mesgbox(title, prompt, 8, 72); 561 else 562 fprintf(stderr, "%s\n", prompt); 563 return (DITEM_FAILURE | DITEM_RECREATE); 564 } 565 566 while ((len = read(fd1, buf, sizeof(buf))) > 0) 567 len = write(fd2, buf, len); 568 569 if (len == -1) { 570 snprintf(title, sizeof(title), "Error"); 571 snprintf(prompt, sizeof(prompt), 572 "Error copying %s to %s %s", zoneinfo_file, 573 path_localtime, strerror(errno)); 574 if (usedialog) 575 dialog_mesgbox(title, prompt, 8, 72); 576 else 577 fprintf(stderr, "%s\n", prompt); 578 /* Better to leave none than a corrupt one. */ 579 unlink(path_localtime); 580 return (DITEM_FAILURE | DITEM_RECREATE); 581 } 582 close(fd1); 583 close(fd2); 584 } else { 585 if (access(zoneinfo_file, R_OK) != 0) { 586 snprintf(title, sizeof(title), "Error"); 587 snprintf(prompt, sizeof(prompt), 588 "Cannot access %s: %s", zoneinfo_file, 589 strerror(errno)); 590 if (usedialog) 591 dialog_mesgbox(title, prompt, 8, 72); 592 else 593 fprintf(stderr, "%s\n", prompt); 594 return (DITEM_FAILURE | DITEM_RECREATE); 595 } 596 unlink(path_localtime); 597 if (symlink(zoneinfo_file, path_localtime) < 0) { 598 snprintf(title, sizeof(title), "Error"); 599 snprintf(prompt, sizeof(prompt), 600 "Cannot create symbolic link %s to %s: %s", 601 path_localtime, zoneinfo_file, 602 strerror(errno)); 603 if (usedialog) 604 dialog_mesgbox(title, prompt, 8, 72); 605 else 606 fprintf(stderr, "%s\n", prompt); 607 return (DITEM_FAILURE | DITEM_RECREATE); 608 } 609 } 610 } 611 612 #ifdef VERBOSE 613 snprintf(title, sizeof(title), "Done"); 614 if (copymode) 615 snprintf(prompt, sizeof(prompt), 616 "Copied timezone file from %s to %s", zoneinfo_file, 617 path_localtime); 618 else 619 snprintf(prompt, sizeof(prompt), 620 "Created symbolic link from %s to %s", zoneinfo_file, 621 path_localtime); 622 if (usedialog) 623 dialog_mesgbox(title, prompt, 8, 72); 624 else 625 fprintf(stderr, "%s\n", prompt); 626 #endif 627 628 return (DITEM_LEAVE_MENU); 629 } 630 631 static int 632 install_zoneinfo(const char *zoneinfo) 633 { 634 int rv; 635 FILE *f; 636 char path_zoneinfo_file[MAXPATHLEN]; 637 638 sprintf(path_zoneinfo_file, "%s/%s", path_zoneinfo, zoneinfo); 639 rv = install_zoneinfo_file(path_zoneinfo_file); 640 641 /* Save knowledge for later */ 642 if ((f = fopen(path_db, "w")) != NULL) { 643 fprintf(f, "%s\n", zoneinfo); 644 fclose(f); 645 } 646 647 return (rv); 648 } 649 650 static int 651 confirm_zone(const char *filename) 652 { 653 char title[64], prompt[64]; 654 time_t t = time(0); 655 struct tm *tm; 656 int rv; 657 658 setenv("TZ", filename, 1); 659 tzset(); 660 tm = localtime(&t); 661 662 snprintf(title, sizeof(title), "Confirmation"); 663 snprintf(prompt, sizeof(prompt), 664 "Does the abbreviation `%s' look reasonable?", tm->tm_zone); 665 rv = !dialog_yesno(title, prompt, 5, 72); 666 return (rv); 667 } 668 669 static int 670 set_zone_multi(dialogMenuItem *dmi) 671 { 672 struct zone *zp = dmi->data; 673 int rv; 674 675 if (!confirm_zone(zp->filename)) 676 return (DITEM_FAILURE | DITEM_RECREATE); 677 678 rv = install_zoneinfo(zp->filename); 679 return (rv); 680 } 681 682 static int 683 set_zone_whole_country(dialogMenuItem *dmi) 684 { 685 struct country *cp = dmi->data; 686 int rv; 687 688 if (!confirm_zone(cp->filename)) 689 return (DITEM_FAILURE | DITEM_RECREATE); 690 691 rv = install_zoneinfo(cp->filename); 692 return (rv); 693 } 694 695 static void 696 usage(void) 697 { 698 699 fprintf(stderr, "usage: tzsetup [-nrs] [zoneinfo file]\n"); 700 exit(1); 701 } 702 703 #if defined(__sparc64__) 704 #define DIALOG_UTC dialog_yesno 705 #else 706 #define DIALOG_UTC dialog_noyes 707 #endif 708 709 int 710 main(int argc, char **argv) 711 { 712 char title[64], prompt[128]; 713 int c, fd, rv, skiputc; 714 715 skiputc = 0; 716 while ((c = getopt(argc, argv, "C:nrs")) != -1) { 717 switch(c) { 718 case 'C': 719 chrootenv = optarg; 720 break; 721 case 'n': 722 reallydoit = 0; 723 break; 724 case 'r': 725 reinstall = 1; 726 usedialog = 0; 727 break; 728 case 's': 729 skiputc = 1; 730 break; 731 default: 732 usage(); 733 } 734 } 735 736 if (argc - optind > 1) 737 usage(); 738 739 if (chrootenv == NULL) { 740 strcpy(path_zonetab, _PATH_ZONETAB); 741 strcpy(path_iso3166, _PATH_ISO3166); 742 strcpy(path_zoneinfo, _PATH_ZONEINFO); 743 strcpy(path_localtime, _PATH_LOCALTIME); 744 strcpy(path_db, _PATH_DB); 745 strcpy(path_wall_cmos_clock, _PATH_WALL_CMOS_CLOCK); 746 } else { 747 sprintf(path_zonetab, "%s/%s", chrootenv, _PATH_ZONETAB); 748 sprintf(path_iso3166, "%s/%s", chrootenv, _PATH_ISO3166); 749 sprintf(path_zoneinfo, "%s/%s", chrootenv, _PATH_ZONEINFO); 750 sprintf(path_localtime, "%s/%s", chrootenv, _PATH_LOCALTIME); 751 sprintf(path_db, "%s/%s", chrootenv, _PATH_DB); 752 sprintf(path_wall_cmos_clock, "%s/%s", chrootenv, 753 _PATH_WALL_CMOS_CLOCK); 754 } 755 756 757 /* Override the user-supplied umask. */ 758 (void)umask(S_IWGRP | S_IWOTH); 759 760 read_iso3166_table(); 761 read_zones(); 762 sort_countries(); 763 make_menus(); 764 765 if (reinstall == 1) { 766 FILE *f; 767 char zonefile[MAXPATHLEN]; 768 char path_db[MAXPATHLEN]; 769 770 zonefile[0] = '\0'; 771 path_db[0] = '\0'; 772 if (chrootenv != NULL) { 773 sprintf(zonefile, "%s/", chrootenv); 774 sprintf(path_db, "%s/", chrootenv); 775 } 776 strcat(zonefile, _PATH_ZONEINFO); 777 strcat(zonefile, "/"); 778 strcat(path_db, _PATH_DB); 779 780 if ((f = fopen(path_db, "r")) != NULL) { 781 if (fgets(zonefile, sizeof(zonefile), f) != NULL) { 782 zonefile[sizeof(zonefile) - 1] = 0; 783 if (strlen(zonefile) > 0) { 784 zonefile[strlen(zonefile) - 1] = 0; 785 rv = install_zoneinfo(zonefile); 786 exit(rv & ~DITEM_LEAVE_MENU); 787 } 788 errx(1, "Error reading %s.\n", path_db); 789 } 790 fclose(f); 791 errx(1, 792 "Unable to determine earlier installed zoneinfo " 793 "file. Check %s", path_db); 794 } 795 errx(1, "Cannot open %s for reading. Does it exist?", path_db); 796 } 797 798 /* 799 * If the arguments on the command-line do not specify a file, 800 * then interpret it as a zoneinfo name 801 */ 802 if (optind == argc - 1) { 803 struct stat sb; 804 805 if (stat(argv[optind], &sb) != 0) { 806 usedialog = 0; 807 rv = install_zoneinfo(argv[optind]); 808 exit(rv & ~DITEM_LEAVE_MENU); 809 } 810 /* FALLTHROUGH */ 811 } 812 813 init_dialog(); 814 if (skiputc == 0) { 815 snprintf(title, sizeof(title), 816 "Select local or UTC (Greenwich Mean Time) clock"); 817 snprintf(prompt, sizeof(prompt), 818 "Is this machine's CMOS clock set to UTC? " 819 "If it is set to local time,\n" 820 "or you don't know, please choose NO here!"); 821 if (!DIALOG_UTC(title, prompt, 7, 72)) { 822 if (reallydoit) 823 unlink(_PATH_WALL_CMOS_CLOCK); 824 } else { 825 if (reallydoit) { 826 fd = open(_PATH_WALL_CMOS_CLOCK, 827 O_WRONLY | O_CREAT | O_TRUNC, 828 S_IRUSR | S_IRGRP | S_IROTH); 829 if (fd < 0) { 830 end_dialog(); 831 err(1, "create %s", 832 _PATH_WALL_CMOS_CLOCK); 833 } 834 close(fd); 835 } 836 } 837 dialog_clear_norefresh(); 838 } 839 if (optind == argc - 1) { 840 snprintf(title, sizeof(title), "Default timezone provided"); 841 snprintf(prompt, sizeof(prompt), 842 "\nUse the default `%s' zone?", argv[optind]); 843 if (!dialog_yesno(title, prompt, 7, 72)) { 844 rv = install_zoneinfo_file(argv[optind]); 845 dialog_clear(); 846 end_dialog(); 847 exit(rv & ~DITEM_LEAVE_MENU); 848 } 849 dialog_clear_norefresh(); 850 } 851 snprintf(title, sizeof(title), "Time Zone Selector"); 852 snprintf(prompt, sizeof(prompt), "Select a region"); 853 dialog_menu(title, prompt, -1, -1, NCONTINENTS, -NCONTINENTS, 854 continents, 0, NULL, NULL); 855 856 dialog_clear(); 857 end_dialog(); 858 return (0); 859 } 860