Index: head/usr.sbin/bsdconfig/timezone/timezone =================================================================== --- head/usr.sbin/bsdconfig/timezone/timezone (revision 274393) +++ head/usr.sbin/bsdconfig/timezone/timezone (revision 274394) @@ -1,454 +1,457 @@ #!/bin/sh #- # Copyright (c) 2011-2013 Devin Teske # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF # SUCH DAMAGE. # # $FreeBSD$ # ############################################################ INCLUDES BSDCFG_SHARE="/usr/share/bsdconfig" . $BSDCFG_SHARE/common.subr || exit 1 f_dprintf "%s: loading includes..." "$0" f_include $BSDCFG_SHARE/dialog.subr f_include $BSDCFG_SHARE/mustberoot.subr f_include $BSDCFG_SHARE/strings.subr f_include $BSDCFG_SHARE/timezone/continents.subr f_include $BSDCFG_SHARE/timezone/countries.subr f_include $BSDCFG_SHARE/timezone/iso3166.subr f_include $BSDCFG_SHARE/timezone/menus.subr f_include $BSDCFG_SHARE/timezone/zones.subr BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="090.timezone" f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" ipgm && pgm="${ipgm:-$pgm}" ############################################################ CONFIGURATION # # Standard pathnames # _PATH_DB="/var/db/zoneinfo" _PATH_WALL_CMOS_CLOCK="/etc/wall_cmos_clock" ############################################################ GLOBALS # # Options # REALLYDOIT=1 REINSTALL= USEDIALOG=1 -SKIPUTC= +SKIPUTC= # See MAIN VERBOSE= TZ_OR_FAIL= CHROOTENV= # # Dummy vars (populated dynamically) # COUNTRIES= # list of 2-character country codes created by f_read_iso3166_table ############################################################ FUNCTIONS # dialog_menu_main # # Display the dialog(1)-based application main menu. # dialog_menu_main() { local title="$DIALOG_TITLE" local btitle="$DIALOG_BACKTITLE" local prompt="$msg_select_region" local defaultitem= # Calculated below local hline= local height width rows eval f_dialog_menu_size height width rows \ \"\$title\" \ \"\$btitle\" \ \"\$prompt\" \ \"\$hline\" \ $continent_menu_list # Obtain default-item from previously stored selection f_dialog_default_fetch defaultitem local menu_choice menu_choice=$( eval $DIALOG \ --title \"\$title\" \ --backtitle \"\$btitle\" \ --hline \"\$hline\" \ --ok-label \"\$msg_ok\" \ --cancel-label \"\$msg_cancel\" \ --default-item \"\$defaultitem\" \ --menu \"\$prompt\" \ $height $width $rows \ $continent_menu_list \ 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD ) local retval=$? f_dialog_data_sanitize menu_choice f_dialog_menutag_store "$menu_choice" f_dialog_default_store "$menu_choice" return $retval } ############################################################ MAIN + +# Skip initial question regarding UTC v. Wall-Clock time if run in VM +[ "$( sysctl -n kern.vm_guest 2> /dev/null )" = "none" ] || SKIPUTC=1 # Incorporate rc-file if it exists [ -f "$HOME/.bsdconfigrc" ] && f_include "$HOME/.bsdconfigrc" # # Process command-line arguments # while getopts C:ehnrsv$GETOPTS_STDARGS flag; do case "$flag" in C) CHROOTENV="$OPTARG" ;; e) TZ_OR_FAIL=1 ;; n) REALLYDOIT= ;; r) REINSTALL=1 USEDIALOG= ;; s) SKIPUTC=1 ;; v) VERBOSE=1 ;; h|\?) f_usage $BSDCFG_LIBE/$APP_DIR/USAGE "PROGRAM_NAME" "$pgm" ;; esac done shift $(( $OPTIND - 1 )) # # Initialize # f_dialog_title "$msg_time_zone" f_dialog_backtitle "${ipgm:+bsdconfig }$pgm" f_mustberoot_init # # Process `-C chroot_directory' command-line argument # if [ "$CHROOTENV" ]; then _PATH_ZONETAB="$CHROOTENV$_PATH_ZONETAB" _PATH_ISO3166="$CHROOTENV$_PATH_ISO3166" _PATH_ZONEINFO="$CHROOTENV$_PATH_ZONEINFO" _PATH_LOCALTIME="$CHROOTENV$_PATH_LOCALTIME" _PATH_DB="$CHROOTENV$_PATH_DB" _PATH_WALL_CMOS_CLOCK="$CHROOTENV$_PATH_WALL_CMOS_CLOCK" fi # # Process `-r' command-line option # if [ "$REINSTALL" ]; then [ -f "$_PATH_DB" -a -r "$_PATH_DB" ] || f_die 1 "$msg_cannot_open_for_reading" "$_PATH_DB" f_eval_catch -dk zoneinfo "$0" cat 'cat "%s"' "$_PATH_DB" || f_die 1 "$msg_error_reading" "$_PATH_DB" [ "$zoneinfo" ] || f_die 1 "$msg_unable_to_determine_name_from_db" "$_PATH_DB" f_install_zoneinfo "$zoneinfo" exit $? fi # # If the arguments on the command-line do not specify a file, # then interpret it as a zoneinfo name # if [ $# -ge 1 ]; then zoneinfo="$1" if [ ! -f "$zoneinfo" ]; then USEDIALOG= f_install_zoneinfo "$zoneinfo" exit $? fi # FALLTHROUGH fi # # Process the UTC option # if [ "$_PATH_WALL_CMOS_CLOCK" -a ! "$SKIPUTC" ]; then f_dialog_title "$msg_select_local_or_utc" title="$DIALOG_TITLE" btitle="$DIALOG_BACKTITLE" f_dialog_title_restore msg="$msg_is_machine_clock_utc" if [ "$USE_XDIALOG" ]; then defaultno="default-no" height=10 width=77 else defaultno="defaultno" height=7 width=73 fi if [ "$USE_XDIALOG" ]; then $DIALOG \ --title "$title" \ --backtitle "$btitle" \ --$defaultno \ --ok-label "$msg_yes" \ --cancel-label "$msg_no" \ --yesno "$msg" $height $width result=$? else $DIALOG \ --title "$title" \ --backtitle "$btitle" \ --$defaultno \ --yes-label "$msg_yes" \ --no-label "$msg_no" \ --yesno "$msg" $height $width result=$? fi if [ $result -eq $DIALOG_OK ]; then # User chose YES [ "$REALLYDOIT" ] && f_quietly rm -f "$_PATH_WALL_CMOS_CLOCK" else # User chose NO, pressed ESC (or Ctrl-C), or closed box [ "$REALLYDOIT" ] && ( umask 222 && :> "$_PATH_WALL_CMOS_CLOCK" ) fi fi # # Process optional default zone argument # if [ $# -ge 1 ]; then default="$1" f_dialog_title "$msg_default_zone_provided" f_sprintf msg "\n$msg_use_default_zone" "$default" hline= f_dialog_yesno "$msg" "$hline" result=$? f_dialog_title_restore if [ $result -eq $DIALOG_OK ]; then # User chose YES f_install_zoneinfo_file "$default" result=$? [ ! "$USE_XDIALOG" ] && f_dialog_clear exit $result fi [ ! "$USE_XDIALOG" ] && f_dialog_clear fi # # Override the user-supplied umask # umask 022 # # Read databases and perform initialization # f_read_iso3166_table # creates $COUNTRIES and $country_*_name f_read_zones # creates $country_*_{descr,cont,filename} f_sort_countries # sorts the countries listed for each continent f_make_menus # creates $continent_menu_list and $continent_*_menu_list # # Launch application main menu # defaultctry="" defaultzone="" NEED_CONTINENT=1 NEED_COUNTRY=1 while :; do if [ "$NEED_CONTINENT" ]; then dialog_menu_main # prompt the user to select a continent/ocean retval=$? f_dialog_menutag_fetch mtag if [ $retval -ne $DIALOG_OK ]; then [ "$TZ_OR_FAIL" ] && f_die exit $SUCCESS fi NEED_CONTINENT= continent=$( eval f_dialog_menutag2item \"\$mtag\" \ $continent_menu_list ) cont=$( f_find_continent "$continent" ) cont_title=$( f_continent $cont title ) nitems=$( f_continent $cont nitems ) isocean=$( f_OCEANP $cont ) fi if [ "$NEED_COUNTRY" ]; then if [ "$cont_title" = "$continent_utc_title" ]; then if f_set_zone_utc; then break else NEED_CONTINENT=1 continue fi fi # # Short cut -- if there's only one country, don't post a menu. # if [ $nitems -eq 1 ]; then tag=1 else # # It's amazing how much good grammar really matters... # if [ ! "$isocean" ]; then f_sprintf title "$msg_country_title" \ "$cont_title" f_dialog_title "$title" title="$DIALOG_TITLE" btitle="$DIALOG_BACKTITLE" f_dialog_title_restore prompt="$msg_select_country" else f_sprintf title "$msg_island_and_group_title" \ "$cont_title" f_dialog_title "$title" title="$DIALOG_TITLE" btitle="$DIALOG_BACKTITLE" f_dialog_title_restore prompt="$msg_select_island_or_group" fi # # Calculate size of menu # menu_list=$( f_continent $cont menu_list ) eval f_dialog_menu_size height width rows \ \"\$title\" \ \"\$btitle\" \ \"\$prompt\" \ \"\" \ $menu_list # # Launch the country selection menu # tag=$( eval $DIALOG \ --title \"\$title\" \ --backtitle \"\$btitle\" \ --ok-label \"\$msg_ok\" \ --cancel-label \"\$msg_cancel\" \ --default-item \"\$defaultctry\" \ --menu \"\$prompt\" \ $height $width $rows \ $menu_list \ 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD ) retval=$? f_dialog_data_sanitize tag defaultctry="$tag" if [ $retval -ne $DIALOG_OK ]; then NEED_CONTINENT=1 continue # back to main menu fi fi # Get the country code from the user's selection tlc=$( f_continent $cont tlc_$tag ) NEED_COUNTRY= fi # # If the selection has only one zone (nzones == -1), # just set it. # nzones=$( f_country $tlc nzones ) if [ $nzones -lt 0 ]; then real_cont=$( f_country $tlc cont ) real_continent=$( f_continent $real_cont name ) name=$( f_country $tlc name ) filename=$( f_country $tlc filename ) if ! f_confirm_zone "$real_continent/$filename"; then [ $nitems -eq 1 ] && NEED_CONTINENT=1 NEED_COUNTRY=1 continue fi else f_sprintf title "$msg_country_time_zones" \ "$( f_country $tlc name )" f_dialog_title "$title" title="$DIALOG_TITLE" btitle="$DIALOG_BACKTITLE" f_dialog_title_restore prompt="$msg_select_zone" menu_list=$( f_country $tlc menu_list ) eval f_dialog_menu_size height width rows \ \"\$title\" \"\$btitle\" \"\$prompt\" \"\" $menu_list # # Launch the zone selection menu # NOTE: This is as deep as we go # n=$( eval $DIALOG \ --title \"\$title\" \ --backtitle \"\$btitle\" \ --ok-label \"\$msg_ok\" \ --cancel-label \"\$msg_cancel\" \ --default-item \"\$defaultzone\" \ --menu \"\$prompt\" \ $height $width $rows \ $menu_list \ 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD ) retval=$? f_dialog_data_sanitize n defaultzone="$n" if [ $retval -ne $DIALOG_OK ]; then [ $nitems -eq 1 ] && NEED_CONTINENT=1 NEED_COUNTRY=1 continue fi real_cont=$( f_country $tlc cont_$n ) real_continent=$( f_continent $real_cont name ) name=$( f_country $tlc name ) filename=$( f_country $tlc filename_$n ) f_confirm_zone "$real_continent/$filename" || continue fi [ $retval -eq $DIALOG_OK ] || continue # back to main menu if ! f_install_zoneinfo "$real_continent/$filename"; then [ $nzones -lt 0 ] && NEED_COUNTRY=1 else break fi done ################################################################################ # END ################################################################################ Index: head/usr.sbin/tzsetup/tzsetup.c =================================================================== --- head/usr.sbin/tzsetup/tzsetup.c (revision 274393) +++ head/usr.sbin/tzsetup/tzsetup.c (revision 274394) @@ -1,1056 +1,1065 @@ /* * Copyright 1996 Massachusetts Institute of Technology * * Permission to use, copy, modify, and distribute this software and * its documentation for any purpose and without fee is hereby * granted, provided that both the above copyright notice and this * permission notice appear in all copies, that both the above * copyright notice and this permission notice appear in all * supporting documentation, and that the name of M.I.T. not be used * in advertising or publicity pertaining to distribution of the * software without specific, written prior permission. M.I.T. makes * no representations about the suitability of this software for any * purpose. It is provided "as is" without express or implied * warranty. * * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ /* * Second attempt at a `tzmenu' program, using the separate description * files provided in newer tzdata releases. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include +#include #include #define _PATH_ZONETAB "/usr/share/zoneinfo/zone.tab" #define _PATH_ISO3166 "/usr/share/misc/iso3166" #define _PATH_ZONEINFO "/usr/share/zoneinfo" #define _PATH_LOCALTIME "/etc/localtime" #define _PATH_DB "/var/db/zoneinfo" #define _PATH_WALL_CMOS_CLOCK "/etc/wall_cmos_clock" #ifdef PATH_MAX #define SILLY_BUFFER_SIZE 2*PATH_MAX #else #warning "Somebody needs to fix this to dynamically size this buffer." #define SILLY_BUFFER_SIZE 2048 #endif /* special return codes for `fire' actions */ #define DITEM_FAILURE 1 /* flags - returned in upper 16 bits of return status */ #define DITEM_LEAVE_MENU (1 << 16) #define DITEM_RECREATE (1 << 18) /* for use in describing more exotic behaviors */ typedef struct dialogMenuItem { char *prompt; char *title; int (*fire)(struct dialogMenuItem *self); void *data; } dialogMenuItem; static int xdialog_count_rows(const char *p) { int rows = 0; while ((p = strchr(p, '\n')) != NULL) { p++; if (*p == '\0') break; rows++; } return rows ? rows : 1; } static int xdialog_count_columns(const char *p) { int len; int max_len = 0; const char *q; for (; (q = strchr(p, '\n')) != NULL; p = q + 1) { len = q - p; max_len = MAX(max_len, len); } len = strlen(p); max_len = MAX(max_len, len); return max_len; } static int xdialog_menu(const char *title, const char *cprompt, int height, int width, int menu_height, int item_no, dialogMenuItem *ditems) { int i, result, choice = 0; DIALOG_LISTITEM *listitems; DIALOG_VARS save_vars; dlg_save_vars(&save_vars); /* initialize list items */ listitems = dlg_calloc(DIALOG_LISTITEM, item_no + 1); assert_ptr(listitems, "xdialog_menu"); for (i = 0; i < item_no; i++) { listitems[i].name = ditems[i].prompt; listitems[i].text = ditems[i].title; } /* calculate height */ if (height < 0) height = xdialog_count_rows(cprompt) + menu_height + 4 + 2; if (height > LINES) height = LINES; /* calculate width */ if (width < 0) { int tag_x = 0; for (i = 0; i < item_no; i++) { int j, l; l = strlen(listitems[i].name); for (j = 0; j < item_no; j++) { int k = strlen(listitems[j].text); tag_x = MAX(tag_x, l + k + 2); } } width = MAX(xdialog_count_columns(cprompt), title != NULL ? xdialog_count_columns(title) : 0); width = MAX(width, tag_x + 4) + 4; } width = MAX(width, 24); if (width > COLS) width = COLS; again: dialog_vars.default_item = listitems[choice].name; result = dlg_menu(title, cprompt, height, width, menu_height, item_no, listitems, &choice, NULL); switch (result) { case DLG_EXIT_ESC: result = -1; break; case DLG_EXIT_OK: if (ditems[choice].fire != NULL) { int status; status = ditems[choice].fire(ditems + choice); if (status & DITEM_RECREATE) { dlg_clear(); goto again; } } result = 0; break; case DLG_EXIT_CANCEL: default: result = 1; break; } free(listitems); dlg_restore_vars(&save_vars); return result; } static char path_zonetab[MAXPATHLEN], path_iso3166[MAXPATHLEN], path_zoneinfo[MAXPATHLEN], path_localtime[MAXPATHLEN], path_db[MAXPATHLEN], path_wall_cmos_clock[MAXPATHLEN]; static int reallydoit = 1; static int reinstall = 0; static int usedialog = 1; static char *chrootenv = NULL; static void usage(void); static int confirm_zone(const char *filename); static int continent_country_menu(dialogMenuItem *); static int install_zoneinfo_file(const char *zoneinfo_file); static int set_zone_multi(dialogMenuItem *); static int set_zone_whole_country(dialogMenuItem *); static int set_zone_menu(dialogMenuItem *); static int set_zone_utc(void); struct continent { dialogMenuItem *menu; int nitems; }; static struct continent africa, america, antarctica, arctic, asia, atlantic; static struct continent australia, europe, indian, pacific, utc; static struct continent_names { const char *name; struct continent *continent; } continent_names[] = { { "Africa", &africa }, { "America", &america }, { "Antarctica", &antarctica }, { "Arctic", &arctic }, { "Asia", &asia }, { "Atlantic", &atlantic }, { "Australia", &australia }, { "Europe", &europe }, { "Indian", &indian }, { "Pacific", &pacific }, { "UTC", &utc } }; static struct continent_items { char prompt[2]; char title[30]; } continent_items[] = { { "1", "Africa" }, { "2", "America -- North and South" }, { "3", "Antarctica" }, { "4", "Arctic Ocean" }, { "5", "Asia" }, { "6", "Atlantic Ocean" }, { "7", "Australia" }, { "8", "Europe" }, { "9", "Indian Ocean" }, { "0", "Pacific Ocean" }, { "a", "UTC" } }; #define NCONTINENTS \ (int)((sizeof(continent_items)) / (sizeof(continent_items[0]))) static dialogMenuItem continents[NCONTINENTS]; #define OCEANP(x) ((x) == 3 || (x) == 5 || (x) == 8 || (x) == 9) static int continent_country_menu(dialogMenuItem *continent) { char title[64], prompt[64]; struct continent *contp = continent->data; int isocean = OCEANP(continent - continents); int menulen; int rv; if (strcmp(continent->title, "UTC") == 0) return set_zone_utc(); /* Short cut -- if there's only one country, don't post a menu. */ if (contp->nitems == 1) return (contp->menu[0].fire(&contp->menu[0])); /* It's amazing how much good grammar really matters... */ if (!isocean) { snprintf(title, sizeof(title), "Countries in %s", continent->title); snprintf(prompt, sizeof(prompt), "Select a country or region"); } else { snprintf(title, sizeof(title), "Islands and groups in the %s", continent->title); snprintf(prompt, sizeof(prompt), "Select an island or group"); } menulen = contp->nitems < 16 ? contp->nitems : 16; rv = xdialog_menu(title, prompt, -1, -1, menulen, contp->nitems, contp->menu); if (rv == 0) return (DITEM_LEAVE_MENU); return (DITEM_RECREATE); } static struct continent * find_continent(const char *name) { int i; for (i = 0; i < NCONTINENTS; i++) if (strcmp(name, continent_names[i].name) == 0) return (continent_names[i].continent); return (0); } struct country { char *name; char *tlc; int nzones; char *filename; /* use iff nzones < 0 */ struct continent *continent; /* use iff nzones < 0 */ TAILQ_HEAD(, zone) zones; /* use iff nzones > 0 */ dialogMenuItem *submenu; /* use iff nzones > 0 */ }; struct zone { TAILQ_ENTRY(zone) link; char *descr; char *filename; struct continent *continent; }; /* * This is the easiest organization... we use ISO 3166 country codes, * of the two-letter variety, so we just size this array to suit. * Beats worrying about dynamic allocation. */ #define NCOUNTRIES (26 * 26) static struct country countries[NCOUNTRIES]; #define CODE2INT(s) ((s[0] - 'A') * 26 + (s[1] - 'A')) /* * Read the ISO 3166 country code database in _PATH_ISO3166 * (/usr/share/misc/iso3166). On error, exit via err(3). */ static void read_iso3166_table(void) { FILE *fp; struct country *cp; size_t len; char *s, *t, *name; int lineno; fp = fopen(path_iso3166, "r"); if (!fp) err(1, "%s", path_iso3166); lineno = 0; while ((s = fgetln(fp, &len)) != 0) { lineno++; if (s[len - 1] != '\n') errx(1, "%s:%d: invalid format", path_iso3166, lineno); s[len - 1] = '\0'; if (s[0] == '#' || strspn(s, " \t") == len - 1) continue; /* Isolate the two-letter code. */ t = strsep(&s, "\t"); if (t == 0 || strlen(t) != 2) errx(1, "%s:%d: invalid format", path_iso3166, lineno); if (t[0] < 'A' || t[0] > 'Z' || t[1] < 'A' || t[1] > 'Z') errx(1, "%s:%d: invalid code `%s'", path_iso3166, lineno, t); /* Now skip past the three-letter and numeric codes. */ name = strsep(&s, "\t"); /* 3-let */ if (name == 0 || strlen(name) != 3) errx(1, "%s:%d: invalid format", path_iso3166, lineno); name = strsep(&s, "\t"); /* numeric */ if (name == 0 || strlen(name) != 3) errx(1, "%s:%d: invalid format", path_iso3166, lineno); name = s; cp = &countries[CODE2INT(t)]; if (cp->name) errx(1, "%s:%d: country code `%s' multiply defined: %s", path_iso3166, lineno, t, cp->name); cp->name = strdup(name); if (cp->name == NULL) errx(1, "malloc failed"); cp->tlc = strdup(t); if (cp->tlc == NULL) errx(1, "malloc failed"); } fclose(fp); } static void add_zone_to_country(int lineno, const char *tlc, const char *descr, const char *file, struct continent *cont) { struct zone *zp; struct country *cp; if (tlc[0] < 'A' || tlc[0] > 'Z' || tlc[1] < 'A' || tlc[1] > 'Z') errx(1, "%s:%d: country code `%s' invalid", path_zonetab, lineno, tlc); cp = &countries[CODE2INT(tlc)]; if (cp->name == 0) errx(1, "%s:%d: country code `%s' unknown", path_zonetab, lineno, tlc); if (descr) { if (cp->nzones < 0) errx(1, "%s:%d: conflicting zone definition", path_zonetab, lineno); zp = malloc(sizeof(*zp)); if (zp == 0) errx(1, "malloc(%zu)", sizeof(*zp)); if (cp->nzones == 0) TAILQ_INIT(&cp->zones); zp->descr = strdup(descr); if (zp->descr == NULL) errx(1, "malloc failed"); zp->filename = strdup(file); if (zp->filename == NULL) errx(1, "malloc failed"); zp->continent = cont; TAILQ_INSERT_TAIL(&cp->zones, zp, link); cp->nzones++; } else { if (cp->nzones > 0) errx(1, "%s:%d: zone must have description", path_zonetab, lineno); if (cp->nzones < 0) errx(1, "%s:%d: zone multiply defined", path_zonetab, lineno); cp->nzones = -1; cp->filename = strdup(file); if (cp->filename == NULL) errx(1, "malloc failed"); cp->continent = cont; } } /* * This comparison function intentionally sorts all of the null-named * ``countries''---i.e., the codes that don't correspond to a real * country---to the end. Everything else is lexical by country name. */ static int compare_countries(const void *xa, const void *xb) { const struct country *a = xa, *b = xb; if (a->name == 0 && b->name == 0) return (0); if (a->name == 0 && b->name != 0) return (1); if (b->name == 0) return (-1); return (strcmp(a->name, b->name)); } /* * This must be done AFTER all zone descriptions are read, since it breaks * CODE2INT(). */ static void sort_countries(void) { qsort(countries, NCOUNTRIES, sizeof(countries[0]), compare_countries); } static void read_zones(void) { char contbuf[16]; FILE *fp; struct continent *cont; size_t len; char *line, *tlc, *coord, *file, *descr, *p; int lineno; fp = fopen(path_zonetab, "r"); if (!fp) err(1, "%s", path_zonetab); lineno = 0; while ((line = fgetln(fp, &len)) != 0) { lineno++; if (line[len - 1] != '\n') errx(1, "%s:%d: invalid format", path_zonetab, lineno); line[len - 1] = '\0'; if (line[0] == '#') continue; tlc = strsep(&line, "\t"); if (strlen(tlc) != 2) errx(1, "%s:%d: invalid country code `%s'", path_zonetab, lineno, tlc); coord = strsep(&line, "\t"); /* Unused */ file = strsep(&line, "\t"); p = strchr(file, '/'); if (p == 0) errx(1, "%s:%d: invalid zone name `%s'", path_zonetab, lineno, file); contbuf[0] = '\0'; strncat(contbuf, file, p - file); cont = find_continent(contbuf); if (!cont) errx(1, "%s:%d: invalid region `%s'", path_zonetab, lineno, contbuf); descr = (line != NULL && *line != '\0') ? line : NULL; add_zone_to_country(lineno, tlc, descr, file, cont); } fclose(fp); } static void make_menus(void) { struct country *cp; struct zone *zp, *zp2; struct continent *cont; dialogMenuItem *dmi; int i; /* * First, count up all the countries in each continent/ocean. * Be careful to count those countries which have multiple zones * only once for each. NB: some countries are in multiple * continents/oceans. */ for (cp = countries; cp->name; cp++) { if (cp->nzones == 0) continue; if (cp->nzones < 0) { cp->continent->nitems++; } else { TAILQ_FOREACH(zp, &cp->zones, link) { cont = zp->continent; for (zp2 = TAILQ_FIRST(&cp->zones); zp2->continent != cont; zp2 = TAILQ_NEXT(zp2, link)) ; if (zp2 == zp) zp->continent->nitems++; } } } /* * Now allocate memory for the country menus and initialize * continent menus. We set nitems back to zero so that we can * use it for counting again when we actually build the menus. */ memset(continents, 0, sizeof(continents)); for (i = 0; i < NCONTINENTS; i++) { continent_names[i].continent->menu = malloc(sizeof(dialogMenuItem) * continent_names[i].continent->nitems); if (continent_names[i].continent->menu == 0) errx(1, "malloc for continent menu"); continent_names[i].continent->nitems = 0; continents[i].prompt = continent_items[i].prompt; continents[i].title = continent_items[i].title; continents[i].fire = continent_country_menu; continents[i].data = continent_names[i].continent; } /* * Now that memory is allocated, create the menu items for * each continent. For multiple-zone countries, also create * the country's zone submenu. */ for (cp = countries; cp->name; cp++) { if (cp->nzones == 0) continue; if (cp->nzones < 0) { dmi = &cp->continent->menu[cp->continent->nitems]; memset(dmi, 0, sizeof(*dmi)); asprintf(&dmi->prompt, "%d", ++cp->continent->nitems); dmi->title = cp->name; dmi->fire = set_zone_whole_country; dmi->data = cp; } else { cp->submenu = malloc(cp->nzones * sizeof(*dmi)); if (cp->submenu == 0) errx(1, "malloc for submenu"); cp->nzones = 0; TAILQ_FOREACH(zp, &cp->zones, link) { cont = zp->continent; dmi = &cp->submenu[cp->nzones]; memset(dmi, 0, sizeof(*dmi)); asprintf(&dmi->prompt, "%d", ++cp->nzones); dmi->title = zp->descr; dmi->fire = set_zone_multi; dmi->data = zp; for (zp2 = TAILQ_FIRST(&cp->zones); zp2->continent != cont; zp2 = TAILQ_NEXT(zp2, link)) ; if (zp2 != zp) continue; dmi = &cont->menu[cont->nitems]; memset(dmi, 0, sizeof(*dmi)); asprintf(&dmi->prompt, "%d", ++cont->nitems); dmi->title = cp->name; dmi->fire = set_zone_menu; dmi->data = cp; } } } } static int set_zone_menu(dialogMenuItem *dmi) { char title[64], prompt[64]; struct country *cp = dmi->data; int menulen; int rv; snprintf(title, sizeof(title), "%s Time Zones", cp->name); snprintf(prompt, sizeof(prompt), "Select a zone which observes the same time as your locality."); menulen = cp->nzones < 16 ? cp->nzones : 16; rv = xdialog_menu(title, prompt, -1, -1, menulen, cp->nzones, cp->submenu); if (rv != 0) return (DITEM_RECREATE); return (DITEM_LEAVE_MENU); } int set_zone_utc(void) { if (!confirm_zone(NULL)) return (DITEM_FAILURE | DITEM_RECREATE); return (install_zoneinfo_file(NULL)); } static int install_zoneinfo_file(const char *zoneinfo_file) { char buf[1024]; char title[64], prompt[SILLY_BUFFER_SIZE]; struct stat sb; ssize_t len; int fd1, fd2, copymode; if (lstat(path_localtime, &sb) < 0) { /* Nothing there yet... */ copymode = 1; } else if (S_ISLNK(sb.st_mode)) copymode = 0; else copymode = 1; #ifdef VERBOSE snprintf(title, sizeof(title), "Info"); if (zoneinfo_file == NULL) snprintf(prompt, sizeof(prompt), "Removing %s", path_localtime); else if (copymode) snprintf(prompt, sizeof(prompt), "Copying %s to %s", zoneinfo_file, path_localtime); else snprintf(prompt, sizeof(prompt), "Creating symbolic link %s to %s", path_localtime, zoneinfo_file); if (usedialog) dialog_msgbox(title, prompt, 8, 72, 1); else fprintf(stderr, "%s\n", prompt); #endif if (reallydoit) { if (zoneinfo_file == NULL) { if (unlink(path_localtime) < 0 && errno != ENOENT) { snprintf(title, sizeof(title), "Error"); snprintf(prompt, sizeof(prompt), "Could not delete %s: %s", path_localtime, strerror(errno)); if (usedialog) dialog_msgbox(title, prompt, 8, 72, 1); else fprintf(stderr, "%s\n", prompt); return (DITEM_FAILURE | DITEM_RECREATE); } if (unlink(path_db) < 0 && errno != ENOENT) { snprintf(title, sizeof(title), "Error"); snprintf(prompt, sizeof(prompt), "Could not delete %s: %s", path_db, strerror(errno)); if (usedialog) dialog_msgbox(title, prompt, 8, 72, 1); else fprintf(stderr, "%s\n", prompt); return (DITEM_FAILURE | DITEM_RECREATE); } #ifdef VERBOSE snprintf(title, sizeof(title), "Done"); snprintf(prompt, sizeof(prompt), "Removed %s", path_localtime); if (usedialog) dialog_msgbox(title, prompt, 8, 72, 1); else fprintf(stderr, "%s\n", prompt); #endif return (DITEM_LEAVE_MENU); } if (copymode) { fd1 = open(zoneinfo_file, O_RDONLY, 0); if (fd1 < 0) { snprintf(title, sizeof(title), "Error"); snprintf(prompt, sizeof(prompt), "Could not open %s: %s", zoneinfo_file, strerror(errno)); if (usedialog) dialog_msgbox(title, prompt, 8, 72, 1); else fprintf(stderr, "%s\n", prompt); return (DITEM_FAILURE | DITEM_RECREATE); } if (unlink(path_localtime) < 0 && errno != ENOENT) { snprintf(prompt, sizeof(prompt), "Could not unlink %s: %s", path_localtime, strerror(errno)); if (usedialog) { snprintf(title, sizeof(title), "Error"); dialog_msgbox(title, prompt, 8, 72, 1); } else fprintf(stderr, "%s\n", prompt); return (DITEM_FAILURE | DITEM_RECREATE); } fd2 = open(path_localtime, O_CREAT | O_EXCL | O_WRONLY, S_IRUSR | S_IRGRP | S_IROTH); if (fd2 < 0) { snprintf(title, sizeof(title), "Error"); snprintf(prompt, sizeof(prompt), "Could not open %s: %s", path_localtime, strerror(errno)); if (usedialog) dialog_msgbox(title, prompt, 8, 72, 1); else fprintf(stderr, "%s\n", prompt); return (DITEM_FAILURE | DITEM_RECREATE); } while ((len = read(fd1, buf, sizeof(buf))) > 0) if ((len = write(fd2, buf, len)) < 0) break; if (len == -1) { snprintf(title, sizeof(title), "Error"); snprintf(prompt, sizeof(prompt), "Error copying %s to %s %s", zoneinfo_file, path_localtime, strerror(errno)); if (usedialog) dialog_msgbox(title, prompt, 8, 72, 1); else fprintf(stderr, "%s\n", prompt); /* Better to leave none than a corrupt one. */ unlink(path_localtime); return (DITEM_FAILURE | DITEM_RECREATE); } close(fd1); close(fd2); } else { if (access(zoneinfo_file, R_OK) != 0) { snprintf(title, sizeof(title), "Error"); snprintf(prompt, sizeof(prompt), "Cannot access %s: %s", zoneinfo_file, strerror(errno)); if (usedialog) dialog_msgbox(title, prompt, 8, 72, 1); else fprintf(stderr, "%s\n", prompt); return (DITEM_FAILURE | DITEM_RECREATE); } if (unlink(path_localtime) < 0 && errno != ENOENT) { snprintf(prompt, sizeof(prompt), "Could not unlink %s: %s", path_localtime, strerror(errno)); if (usedialog) { snprintf(title, sizeof(title), "Error"); dialog_msgbox(title, prompt, 8, 72, 1); } else fprintf(stderr, "%s\n", prompt); return (DITEM_FAILURE | DITEM_RECREATE); } if (symlink(zoneinfo_file, path_localtime) < 0) { snprintf(title, sizeof(title), "Error"); snprintf(prompt, sizeof(prompt), "Cannot create symbolic link %s to %s: %s", path_localtime, zoneinfo_file, strerror(errno)); if (usedialog) dialog_msgbox(title, prompt, 8, 72, 1); else fprintf(stderr, "%s\n", prompt); return (DITEM_FAILURE | DITEM_RECREATE); } } #ifdef VERBOSE snprintf(title, sizeof(title), "Done"); if (copymode) snprintf(prompt, sizeof(prompt), "Copied timezone file from %s to %s", zoneinfo_file, path_localtime); else snprintf(prompt, sizeof(prompt), "Created symbolic link from %s to %s", zoneinfo_file, path_localtime); if (usedialog) dialog_msgbox(title, prompt, 8, 72, 1); else fprintf(stderr, "%s\n", prompt); #endif } /* reallydoit */ return (DITEM_LEAVE_MENU); } static int install_zoneinfo(const char *zoneinfo) { int rv; FILE *f; char path_zoneinfo_file[MAXPATHLEN]; sprintf(path_zoneinfo_file, "%s/%s", path_zoneinfo, zoneinfo); rv = install_zoneinfo_file(path_zoneinfo_file); /* Save knowledge for later */ if (reallydoit && (rv & DITEM_FAILURE) == 0) { if ((f = fopen(path_db, "w")) != NULL) { fprintf(f, "%s\n", zoneinfo); fclose(f); } } return (rv); } static int confirm_zone(const char *filename) { char title[64], prompt[64]; time_t t = time(0); struct tm *tm; int rv; setenv("TZ", filename == NULL ? "" : filename, 1); tzset(); tm = localtime(&t); snprintf(title, sizeof(title), "Confirmation"); snprintf(prompt, sizeof(prompt), "Does the abbreviation `%s' look reasonable?", tm->tm_zone); rv = !dialog_yesno(title, prompt, 5, 72); return (rv); } static int set_zone_multi(dialogMenuItem *dmi) { struct zone *zp = dmi->data; int rv; if (!confirm_zone(zp->filename)) return (DITEM_FAILURE | DITEM_RECREATE); rv = install_zoneinfo(zp->filename); return (rv); } static int set_zone_whole_country(dialogMenuItem *dmi) { struct country *cp = dmi->data; int rv; if (!confirm_zone(cp->filename)) return (DITEM_FAILURE | DITEM_RECREATE); rv = install_zoneinfo(cp->filename); return (rv); } static void usage(void) { fprintf(stderr, "usage: tzsetup [-nrs] [-C chroot_directory]" " [zoneinfo_file | zoneinfo_name]\n"); exit(1); } int main(int argc, char **argv) { char title[64], prompt[128]; int c, fd, rv, skiputc; + char vm_guest[16] = ""; + size_t len = sizeof(vm_guest); skiputc = 0; + + /* Default skiputc to 1 for VM guests */ + if (sysctlbyname("kern.vm_guest", vm_guest, &len, NULL, 0) == 0 && + strcmp(vm_guest, "none") != 0) + skiputc = 1; + while ((c = getopt(argc, argv, "C:nrs")) != -1) { switch(c) { case 'C': chrootenv = optarg; break; case 'n': reallydoit = 0; break; case 'r': reinstall = 1; usedialog = 0; break; case 's': skiputc = 1; break; default: usage(); } } if (argc - optind > 1) usage(); if (chrootenv == NULL) { strcpy(path_zonetab, _PATH_ZONETAB); strcpy(path_iso3166, _PATH_ISO3166); strcpy(path_zoneinfo, _PATH_ZONEINFO); strcpy(path_localtime, _PATH_LOCALTIME); strcpy(path_db, _PATH_DB); strcpy(path_wall_cmos_clock, _PATH_WALL_CMOS_CLOCK); } else { sprintf(path_zonetab, "%s/%s", chrootenv, _PATH_ZONETAB); sprintf(path_iso3166, "%s/%s", chrootenv, _PATH_ISO3166); sprintf(path_zoneinfo, "%s/%s", chrootenv, _PATH_ZONEINFO); sprintf(path_localtime, "%s/%s", chrootenv, _PATH_LOCALTIME); sprintf(path_db, "%s/%s", chrootenv, _PATH_DB); sprintf(path_wall_cmos_clock, "%s/%s", chrootenv, _PATH_WALL_CMOS_CLOCK); } /* Override the user-supplied umask. */ (void)umask(S_IWGRP | S_IWOTH); if (reinstall == 1) { FILE *f; char zoneinfo[MAXPATHLEN]; if ((f = fopen(path_db, "r")) != NULL) { if (fgets(zoneinfo, sizeof(zoneinfo), f) != NULL) { zoneinfo[sizeof(zoneinfo) - 1] = 0; if (strlen(zoneinfo) > 0) { zoneinfo[strlen(zoneinfo) - 1] = 0; rv = install_zoneinfo(zoneinfo); exit(rv & ~DITEM_LEAVE_MENU); } errx(1, "Error reading %s.\n", path_db); } fclose(f); errx(1, "Unable to determine earlier installed zoneinfo " "name. Check %s", path_db); } errx(1, "Cannot open %s for reading. Does it exist?", path_db); } /* * If the arguments on the command-line do not specify a file, * then interpret it as a zoneinfo name */ if (optind == argc - 1) { struct stat sb; if (stat(argv[optind], &sb) != 0) { usedialog = 0; rv = install_zoneinfo(argv[optind]); exit(rv & ~DITEM_LEAVE_MENU); } /* FALLTHROUGH */ } read_iso3166_table(); read_zones(); sort_countries(); make_menus(); init_dialog(stdin, stdout); if (skiputc == 0) { DIALOG_VARS save_vars; int yesno; snprintf(title, sizeof(title), "Select local or UTC (Greenwich Mean Time) clock"); snprintf(prompt, sizeof(prompt), "Is this machine's CMOS clock set to UTC? " "If it is set to local time,\n" "or you don't know, please choose NO here!"); dlg_save_vars(&save_vars); #if !defined(__sparc64__) dialog_vars.defaultno = TRUE; #endif yesno = dialog_yesno(title, prompt, 7, 73); dlg_restore_vars(&save_vars); if (!yesno) { if (reallydoit) unlink(path_wall_cmos_clock); } else { if (reallydoit) { fd = open(path_wall_cmos_clock, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IRGRP | S_IROTH); if (fd < 0) { end_dialog(); err(1, "create %s", path_wall_cmos_clock); } close(fd); } } dlg_clear(); } if (optind == argc - 1) { snprintf(title, sizeof(title), "Default timezone provided"); snprintf(prompt, sizeof(prompt), "\nUse the default `%s' zone?", argv[optind]); if (!dialog_yesno(title, prompt, 7, 72)) { rv = install_zoneinfo_file(argv[optind]); dlg_clear(); end_dialog(); exit(rv & ~DITEM_LEAVE_MENU); } dlg_clear(); } snprintf(title, sizeof(title), "Time Zone Selector"); snprintf(prompt, sizeof(prompt), "Select a region"); xdialog_menu(title, prompt, -1, -1, NCONTINENTS, NCONTINENTS, continents); dlg_clear(); end_dialog(); return (0); }