tzsetup: Sync with FreeBSD.
authorPeter Avalos <pavalos@dragonflybsd.org>
Thu, 14 Feb 2013 18:23:09 +0000 (10:23 -0800)
committerPeter Avalos <pavalos@dragonflybsd.org>
Thu, 14 Feb 2013 19:33:56 +0000 (11:33 -0800)
Primarily this is an update for the new libdialog, but while I'm here:

- Add a menu entry for UTC in the main menu.
- Add support for chrooted installs.
- Add examples to the man-page.
- Instead of having to know which timezone was picked last time, you
now can run "tzsetup -r" which will reinstall the last choice. This
data is recorded in /var/db/zoneinfo.
- Add a new options (-s) that, when specified, skips the question about
adjusting the clock to UTC. That avoids to write on /etc/wall_cmos_clock
which is useful in some cases (example: host user in a jail).
- Remove paths.h and embed it in the source.
- Fix style(9) issues.

Obtained-from:   FreeBSD

usr.sbin/tzsetup/Makefile
usr.sbin/tzsetup/paths.h [deleted file]
usr.sbin/tzsetup/tzsetup.8
usr.sbin/tzsetup/tzsetup.c

index 5eb2846..b81315c 100644 (file)
@@ -2,9 +2,9 @@
 
 PROG=  tzsetup
 MAN=   tzsetup.8
-CFLAGS+=       -I${.CURDIR}
-LDADD+=        -ldialog -lncurses
-DPADD+=        ${LIBDIALOG} ${LIBNCURSES}
-WARNS?=        2
+WARNS?=        3
+
+DPADD= ${LIBDIALOG} ${LIBM} ${LIBNCURSESW}
+LDADD= -ldialog -lm -lncursesw
 
 .include <bsd.prog.mk>
diff --git a/usr.sbin/tzsetup/paths.h b/usr.sbin/tzsetup/paths.h
deleted file mode 100644 (file)
index 4754e9a..0000000
+++ /dev/null
@@ -1,5 +0,0 @@
-#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_WALL_CMOS_CLOCK   "/etc/wall_cmos_clock"
index 5c17c9d..9c8e03a 100644 (file)
@@ -22,9 +22,9 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.\" $FreeBSD: src/usr.sbin/tzsetup/tzsetup.8,v 1.11.2.4 2003/03/11 22:31:34 trhodes Exp $
-.\" $DragonFly: src/usr.sbin/tzsetup/tzsetup.8,v 1.4 2006/02/17 19:40:29 swildner Exp $
-.Dd January 24, 1996
+.\" $FreeBSD: src/usr.sbin/tzsetup/tzsetup.8,v 1.26 2012/11/17 01:54:59 svnexp Exp $
+.\"
+.Dd February 14, 2013
 .Dt TZSETUP 8
 .Os
 .Sh NAME
 .Nd set local timezone
 .Sh SYNOPSIS
 .Nm
-.Op Fl n
-.Op Ar default
+.Op Fl nrs
+.Op Fl C Ar chroot_directory
+.Op Ar zoneinfo_file | zoneinfo_name
 .Sh DESCRIPTION
 The
 .Nm
 utility reads a database of timezone information and presents a menu
 allowing the user to select a specific zone without knowing the details
-of the database layout.  The selected zone is installed as the system
+of the database layout.
+The selected zone is installed as the system
 default zone.
 The
 .Nm
@@ -47,16 +49,29 @@ utility also determines whether any adjustment is necessary for systems where
 the hardware clock does not keep
 .Tn UTC .
 .Pp
-The following option is available:
+The following options are available:
 .Bl -tag -offset indent -width Fl
+.It Fl C Ar chroot_directory
+Open all files and directories relative to
+.Ar chroot_directory .
 .It Fl n
 Do not create or copy files.
+.It Fl r
+Reinstall the zoneinfo file installed last time.
+The name is obtained from
+.Pa /var/db/zoneinfo .
+.It Fl s
+Skip the initial question about adjusting the clock if not set to
+.Tn UTC .
 .El
 .Pp
-It is possible to short-circuit the menu system by specifying a
-.Ar default
-on the command line; this is intended mainly for pre-configured
-installation scripts.
+It is possible to short-circuit the menu system by specifying the
+location of a
+.Ar zoneinfo_file
+or the name of the
+.Ar zoneinfo_name
+on the command line; this is intended mainly for pre-configured installation
+scripts or people who know which zoneinfo they want to install.
 .Sh TIMEZONE DATABASE
 The contents of the timezone database are indexed by
 .Pa /usr/share/zoneinfo/zone.tab .
@@ -79,7 +94,8 @@ beginning of the
 epoch (January 1, 1970,
 .Tn GMT ) .
 .It
-Each zone is named for the most populous city therein.  (Where possible,
+Each zone is named for the most populous city therein.
+(Where possible,
 the database includes pre-1970 history for its city.)
 .El
 The source code to the database
@@ -87,12 +103,12 @@ The source code to the database
 contains many additional comments and documentation references for the
 historically minded.
 .Sh FILES
-.Bl -tag -width /usr/share/zoneinfo/zone.tab -compact
+.Bl -tag -width ".Pa /usr/share/zoneinfo/zone.tab" -compact
 .It Pa /etc/localtime
 current time zone file
 .It Pa /etc/wall_cmos_clock
 see
-.Xr adjkerntz 8 .
+.Xr adjkerntz 8
 .It Pa /usr/share/misc/iso3166
 mapping of
 .Tn ISO
@@ -101,7 +117,27 @@ mapping of
 directory for zoneinfo files
 .It Pa /usr/share/zoneinfo/zone.tab
 mapping of timezone file to country and location
+.It Pa /var/db/zoneinfo
+saved name of the timezone file installed last
 .El
+.Sh EXAMPLES
+Normal usage, to select the right zoneinfo file via the dialog-based
+user interface:
+.Dl tzsetup
+.Pp
+Install the file
+.Pa /usr/share/zoneinfo/Australia/Sydney :
+.Dl "tzsetup /usr/share/zoneinfo/Australia/Sydney"
+.Pp
+Install the zoneinfo file for Australia/Sydney, assumed to be located
+in
+.Pa /usr/share/zoneinfo :
+.Dl "tzsetup Australia/Sydney"
+.Pp
+After a reinstall of the zoneinfo files, you can reinstall the
+latest installed zoneinfo file (as specified in
+.Pa /var/db/zoneinfo ) :
+.Dl "tzsetup -r"
 .Sh SEE ALSO
 .Xr date 1 ,
 .Xr adjtime 2 ,
index bc2630a..ccdb9f8 100644 (file)
@@ -12,7 +12,7 @@
  * 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
@@ -26,7 +26,7 @@
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * $FreeBSD: src/usr.sbin/tzsetup/tzsetup.c,v 1.16.2.2 2002/03/06 06:17:41 obrien Exp $
+ * $FreeBSD: src/usr.sbin/tzsetup/tzsetup.c,v 1.48 2012/11/17 01:54:59 svnexp Exp $
  */
 
 /*
  * files provided in newer tzdata releases.
  */
 
-#include <sys/types.h>
-#include <dialog.h>
 #include <err.h>
 #include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <time.h>
 #include <unistd.h>
 
 #include <sys/fcntl.h>
+#include <sys/param.h>
 #include <sys/queue.h>
 #include <sys/stat.h>
 
-#include "paths.h"
+#include <dialog.h>
 
-static int reallydoit = 1;
+#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;
+       }
 
-static int continent_country_menu(dialogMenuItem *);
-static int set_zone_multi(dialogMenuItem *);
-static int set_zone_whole_country(dialogMenuItem *);
-static int set_zone_menu(dialogMenuItem *);
+       /* 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;
-       int ch;
-       int sc;
+       int             nitems;
 };
 
-static struct continent africa, america, antarctica, arctic, asia, atlantic;
-static struct continent australia, europe, indian, pacific;
+static struct continent        africa, america, antarctica, arctic, asia, atlantic;
+static struct continent        australia, europe, indian, pacific, utc;
 
 static struct continent_names {
-       char *name;
+       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 }
+       { "Africa",     &africa },
+       { "America",    &america },
+       { "Antarctica", &antarctica },
+       { "Arctic",     &arctic },
+       { "Asia",       &asia },
+       { "Atlantic",   &atlantic },
+       { "Australia",  &australia },
+       { "Europe",     &europe },
+       { "Indian",     &indian },
+       { "Pacific",    &pacific },
+       { "UTC",        &utc }
 };
 
-static dialogMenuItem continents[] = {
-       { "1", "Africa", 0, continent_country_menu, 0, &africa },
-       { "2", "America -- North and South", 0, continent_country_menu, 0, 
-                 &america },
-       { "3", "Antarctica", 0, continent_country_menu, 0, &antarctica },
-       { "4", "Arctic Ocean", 0, continent_country_menu, 0, &arctic },
-       { "5", "Asia", 0, continent_country_menu, 0, &asia },
-       { "6", "Atlantic Ocean", 0, continent_country_menu, 0, &atlantic },
-       { "7", "Australia", 0, continent_country_menu, 0, &australia },
-       { "8", "Europe", 0, continent_country_menu, 0, &europe },
-       { "9", "Indian Ocean", 0, continent_country_menu, 0, &indian },
-       { "0", "Pacific Ocean", 0, continent_country_menu, 0, &pacific }
+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 continents)/(sizeof continents[0]))
-#define OCEANP(x) ((x) == 3 || (x) == 5 || (x) == 8 || (x) == 9)
+
+#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)
 {
-       int rv;
+       char            title[64], prompt[64];
        struct continent *contp = continent->data;
-       char title[256];
-       int isocean = OCEANP(continent - continents);
-       int menulen;
+       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);
-       else 
-               snprintf(title, sizeof title, "Islands and groups in the %s",
-                        continent->title);
+       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 = dialog_menu(title, (isocean ? "Select an island or group"
-                                 : "Select a country"), -1, -1, menulen,
-                        -contp->nitems, contp->menu, 0, &contp->ch,
-                        &contp->sc);
+       rv = xdialog_menu(title, prompt, -1, -1, menulen, contp->nitems,
+           contp->menu);
        if (rv == 0)
-               return DITEM_LEAVE_MENU;
-       return DITEM_RECREATE;
+               return (DITEM_LEAVE_MENU);
+       return (DITEM_RECREATE);
 }
 
 static struct continent *
 find_continent(const char *name)
 {
-       int i;
+       int             i;
 
-       for (i = 0; i < NCONTINENTS; i++) {
+       for (i = 0; i < NCONTINENTS; i++)
                if (strcmp(name, continent_names[i].name) == 0)
-                       return continent_names[i].continent;
-       }
-       return 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 */
+       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;
+       char            *descr;
+       char            *filename;
        struct continent *continent;
 };
 
@@ -158,9 +319,10 @@ struct zone {
  * of the two-letter variety, so we just size this array to suit.
  * Beats worrying about dynamic allocation.
  */
-#define NCOUNTRIES     (26*26)
+#define        NCOUNTRIES      (26 * 26)
 static struct country countries[NCOUNTRIES];
-#define CODE2INT(s) ((s[0] - 'A') * 26 + (s[1] - 'A'))
+
+#define        CODE2INT(s)     ((s[0] - 'A') * 26 + (s[1] - 'A'))
 
 /*
  * Read the ISO 3166 country code database in _PATH_ISO3166
@@ -169,21 +331,21 @@ static struct country countries[NCOUNTRIES];
 static void
 read_iso3166_table(void)
 {
-       FILE *fp;
-       char *s, *t, *name;
-       size_t len;
-       int lineno;
-       struct country *cp;
+       FILE            *fp;
+       struct country  *cp;
+       size_t          len;
+       char            *s, *t, *name;
+       int             lineno;
 
-       fp = fopen(_PATH_ISO3166, "r");
+       fp = fopen(path_iso3166, "r");
        if (!fp)
-               err(1, _PATH_ISO3166);
+               err(1, "%s", path_iso3166);
        lineno = 0;
 
        while ((s = fgetln(fp, &len)) != NULL) {
                lineno++;
                if (s[len - 1] != '\n')
-                       errx(1, _PATH_ISO3166 ":%d: invalid format", lineno);
+                       errx(1, "%s:%d: invalid format", path_iso3166, lineno);
                s[len - 1] = '\0';
                if (s[0] == '#' || strspn(s, " \t") == len - 1)
                        continue;
@@ -191,18 +353,17 @@ read_iso3166_table(void)
                /* Isolate the two-letter code. */
                t = strsep(&s, "\t");
                if (t == NULL || strlen(t) != 2)
-                       errx(1, _PATH_ISO3166 ":%d: invalid format", lineno);
+                       errx(1, "%s:%d: invalid format", path_iso3166, lineno);
                if (t[0] < 'A' || t[0] > 'Z' || t[1] < 'A' || t[1] > 'Z')
-                       errx(1, _PATH_ISO3166 ":%d: invalid code `%s'",
-                            lineno, t);
+                       errx(1, "%s:%d: invalid code `%s'", path_iso3166,
+                           lineno, t);
 
                name = s;
 
                cp = &countries[CODE2INT(t)];
                if (cp->name)
-                       errx(1, _PATH_ISO3166 
-                            ":%d: country code `%s' multiply defined: %s",
-                            lineno, t, 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");
@@ -216,29 +377,29 @@ read_iso3166_table(void)
 
 static void
 add_zone_to_country(int lineno, const char *tlc, const char *descr,
-                   const char *file, struct continent *cont)
+    const char *file, struct continent *cont)
 {
-       struct zone *zp;
-       struct country *cp;
+       struct zone     *zp;
+       struct country  *cp;
 
        if (tlc[0] < 'A' || tlc[0] > 'Z' || tlc[1] < 'A' || tlc[1] > 'Z')
-               errx(1, _PATH_ZONETAB ":%d: country code `%s' invalid",
-                    lineno, tlc);
-       
+               errx(1, "%s:%d: country code `%s' invalid", path_zonetab,
+                   lineno, tlc);
+
        cp = &countries[CODE2INT(tlc)];
        if (cp->name == 0)
-               errx(1, _PATH_ZONETAB ":%d: country code `%s' unknown",
-                    lineno, tlc);
+               errx(1, "%s:%d: country code `%s' unknown", path_zonetab,
+                   lineno, tlc);
 
        if (descr) {
                if (cp->nzones < 0)
-                       errx(1, _PATH_ZONETAB 
-                            ":%d: conflicting zone definition", lineno);
+                       errx(1, "%s:%d: conflicting zone definition",
+                           path_zonetab, lineno);
 
-               zp = malloc(sizeof *zp);
+               zp = malloc(sizeof(*zp));
                if (zp == NULL)
-                       errx(1, "malloc(%lu)", (unsigned long)sizeof *zp);
-               
+                       errx(1, "malloc(%zu)", sizeof(*zp));
+
                if (cp->nzones == 0)
                        TAILQ_INIT(&cp->zones);
 
@@ -253,11 +414,11 @@ add_zone_to_country(int lineno, const char *tlc, const char *descr,
                cp->nzones++;
        } else {
                if (cp->nzones > 0)
-                       errx(1, _PATH_ZONETAB 
-                            ":%d: zone must have description", lineno);
+                       errx(1, "%s:%d: zone must have description",
+                           path_zonetab, lineno);
                if (cp->nzones < 0)
-                       errx(1, _PATH_ZONETAB
-                            ":%d: zone multiply defined", lineno);
+                       errx(1, "%s:%d: zone multiply defined",
+                           path_zonetab, lineno);
                cp->nzones = -1;
                cp->filename = strdup(file);
                if (cp->filename == NULL)
@@ -277,13 +438,13 @@ compare_countries(const void *xa, const void *xb)
        const struct country *a = xa, *b = xb;
 
        if (a->name == 0 && b->name == 0)
-               return 0;
+               return (0);
        if (a->name == 0 && b->name != 0)
-               return 1;
+               return (1);
        if (b->name == 0)
-               return -1;
+               return (-1);
 
-       return strcmp(a->name, b->name);
+       return (strcmp(a->name, b->name));
 }
 
 /*
@@ -293,51 +454,51 @@ compare_countries(const void *xa, const void *xb)
 static void
 sort_countries(void)
 {
-       qsort(countries, NCOUNTRIES, sizeof countries[0], compare_countries);
+
+       qsort(countries, NCOUNTRIES, sizeof(countries[0]), compare_countries);
 }
 
 static void
 read_zones(void)
 {
-       FILE *fp;
-       char *line;
-       size_t len;
-       int lineno;
-       char *tlc, *coord, *file, *descr, *p;
-       char contbuf[16];
+       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");
+       fp = fopen(path_zonetab, "r");
        if (!fp)
-               err(1, _PATH_ZONETAB);
+               err(1, "%s", path_zonetab);
        lineno = 0;
 
        while ((line = fgetln(fp, &len)) != NULL) {
                lineno++;
                if (line[len - 1] != '\n')
-                       errx(1, _PATH_ZONETAB ":%d: invalid format", lineno);
+                       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, _PATH_ZONETAB ":%d: invalid country code `%s'",
-                            lineno, tlc);
-               coord = strsep(&line, "\t");
+                       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 == NULL)
-                       errx(1, _PATH_ZONETAB ":%d: invalid zone name `%s'",
-                            lineno, file);
+                       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, _PATH_ZONETAB ":%d: invalid region `%s'",
-                            lineno, contbuf);
+                       errx(1, "%s:%d: invalid region `%s'", path_zonetab,
+                           lineno, contbuf);
 
-               descr = (line && *line) ? line : 0;
+               descr = (line != NULL && *line != '\0') ? line : NULL;
 
                add_zone_to_country(lineno, tlc, descr, file, cont);
        }
@@ -347,11 +508,11 @@ read_zones(void)
 static void
 make_menus(void)
 {
-       struct country *cp;
-       struct zone *zp, *zp2;
+       struct country  *cp;
+       struct zone     *zp, *zp2;
        struct continent *cont;
-       dialogMenuItem *dmi;
-       int i;
+       dialogMenuItem  *dmi;
+       int             i;
 
        /*
         * First, count up all the countries in each continent/ocean.
@@ -365,12 +526,11 @@ make_menus(void)
                if (cp->nzones < 0) {
                        cp->continent->nitems++;
                } else {
-                       for (zp = cp->zones.tqh_first; zp; 
-                            zp = zp->link.tqe_next) {
+                       TAILQ_FOREACH(zp, &cp->zones, link) {
                                cont = zp->continent;
-                               for (zp2 = cp->zones.tqh_first;
-                                    zp2->continent != cont;
-                                    zp2 = zp2->link.tqe_next)
+                               for (zp2 = TAILQ_FIRST(&cp->zones);
+                                   zp2->continent != cont;
+                                   zp2 = TAILQ_NEXT(zp2, link))
                                        ;
                                if (zp2 == zp)
                                        zp->continent->nitems++;
@@ -379,17 +539,22 @@ make_menus(void)
        }
 
        /*
-        * Now allocate memory for the country menus.  We set
-        * nitems back to zero so that we can use it for counting
-        * again when we actually build the menus.
+        * 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);
+                   malloc(sizeof(dialogMenuItem) *
+                   continent_names[i].continent->nitems);
                if (continent_names[i].continent->menu == NULL)
                        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;
        }
 
        /*
@@ -402,46 +567,37 @@ make_menus(void)
                        continue;
                if (cp->nzones < 0) {
                        dmi = &cp->continent->menu[cp->continent->nitems];
-                       memset(dmi, 0, sizeof *dmi);
-                       asprintf(&dmi->prompt, "%d", 
-                                ++cp->continent->nitems);
+                       memset(dmi, 0, sizeof(*dmi));
+                       asprintf(&dmi->prompt, "%d", ++cp->continent->nitems);
                        dmi->title = cp->name;
-                       dmi->checked = 0;
                        dmi->fire = set_zone_whole_country;
-                       dmi->selected = 0;
                        dmi->data = cp;
                } else {
-                       cp->submenu = malloc(cp->nzones * sizeof *dmi);
+                       cp->submenu = malloc(cp->nzones * sizeof(*dmi));
                        if (cp->submenu == 0)
                                errx(1, "malloc for submenu");
                        cp->nzones = 0;
-                       for (zp = cp->zones.tqh_first; zp; 
-                            zp = zp->link.tqe_next) {
+                       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);
+                               memset(dmi, 0, sizeof(*dmi));
+                               asprintf(&dmi->prompt, "%d", ++cp->nzones);
                                dmi->title = zp->descr;
-                               dmi->checked = 0;
                                dmi->fire = set_zone_multi;
-                               dmi->selected = 0;
                                dmi->data = zp;
 
-                               for (zp2 = cp->zones.tqh_first;
-                                    zp2->continent != cont;
-                                    zp2 = zp2->link.tqe_next)
+                               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);
+                               memset(dmi, 0, sizeof(*dmi));
                                asprintf(&dmi->prompt, "%d", ++cont->nitems);
                                dmi->title = cp->name;
-                               dmi->checked = 0;
                                dmi->fire = set_zone_menu;
-                               dmi->selected = 0;
                                dmi->data = cp;
                        }
                }
@@ -451,196 +607,312 @@ make_menus(void)
 static int
 set_zone_menu(dialogMenuItem *dmi)
 {
-       int rv;
-       char buf[256];
-       struct country *cp = dmi->data;
-       int menulen;
-
-       snprintf(buf, sizeof buf, "%s Time Zones", cp->name);
+       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 = dialog_menu(buf, "Select a zone which observes the same time as "
-                        "your locality.", -1, -1, menulen, -cp->nzones,
-                        cp->submenu, 0, 0, 0);
+       rv = xdialog_menu(title, prompt, -1, -1, menulen, cp->nzones,
+           cp->submenu);
        if (rv != 0)
-               return DITEM_RECREATE;
-       return DITEM_LEAVE_MENU;
+               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_zone_file(const char *filename)
+install_zoneinfo_file(const char *zoneinfo_file)
 {
-       struct stat sb;
-       int fd1, fd2;
-       int copymode;
-       char *msg;
-       ssize_t len;
-       char buf[1024];
-
-       if (lstat(_PATH_LOCALTIME, &sb) < 0)
+       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))
+       } else if (S_ISLNK(sb.st_mode))
                copymode = 0;
        else
                copymode = 1;
 
 #ifdef VERBOSE
-       if (copymode)
-               asprintf(&msg, "Copying %s to " _PATH_LOCALTIME, filename);
+       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
-               asprintf(&msg, "Creating symbolic link " _PATH_LOCALTIME
-                        " to %s", filename);
-
-       dialog_notify(msg);
-       free(msg);
+               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(prompt, sizeof(prompt),
+                           "Removed %s", path_localtime);
+#endif
+                       return (DITEM_LEAVE_MENU);
+               }
+
                if (copymode) {
-                       fd1 = open(filename, O_RDONLY, 0);
+                       fd1 = open(zoneinfo_file, O_RDONLY, 0);
                        if (fd1 < 0) {
-                               asprintf(&msg, "Could not open %s: %s",
-                                        filename, strerror(errno));
-                               dialog_mesgbox("Error", msg, 8, 72);
-                               free(msg);
-                               return DITEM_FAILURE | DITEM_RECREATE;
+                               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);
                        }
 
-                       unlink(_PATH_LOCALTIME);
-                       fd2 = open(_PATH_LOCALTIME, 
-                                  O_CREAT|O_EXCL|O_WRONLY,
-                                  S_IRUSR|S_IRGRP|S_IROTH);
+                       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) {
-                               asprintf(&msg, "Could not open "
-                                        _PATH_LOCALTIME ": %s", 
-                                        strerror(errno));
-                               dialog_mesgbox("Error", msg, 8, 72);
-                               free(msg);
-                               return DITEM_FAILURE | DITEM_RECREATE;
+                               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)
-                               len = write(fd2, buf, len);
+                       while ((len = read(fd1, buf, sizeof(buf))) > 0)
+                               if ((len = write(fd2, buf, len)) < 0)
+                                       break;
 
                        if (len == -1) {
-                               asprintf(&msg, "Error copying %s to "
-                                        _PATH_LOCALTIME ": %s",
-                                        filename, strerror(errno));
-                               dialog_mesgbox("Error", msg, 8, 72);
-                               free(msg);
+                               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;
+                               unlink(path_localtime);
+                               return (DITEM_FAILURE | DITEM_RECREATE);
                        }
                        close(fd1);
                        close(fd2);
                } else {
-                       if (access(filename, R_OK) != 0) {
-                               asprintf(&msg, "Cannot access %s: %s",
-                                        filename, strerror(errno));
-                               dialog_mesgbox("Error", msg, 8, 72);
-                               free(msg);
-                               return DITEM_FAILURE | DITEM_RECREATE;
+                       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);
                        }
-                       unlink(_PATH_LOCALTIME);
-                       if (symlink(filename, _PATH_LOCALTIME) < 0) {
-                               asprintf(&msg, "Cannot create symbolic link "
-                                        _PATH_LOCALTIME " to %s: %s",
-                                        filename, strerror(errno));
-                               dialog_mesgbox("Error", msg, 8, 72);
-                               free(msg);
-                               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
-       if (copymode)
-               asprintf(&msg, "Copied timezone file from %s to " 
-                        _PATH_LOCALTIME, filename);
-       else
-               asprintf(&msg, "Created symbolic link from " _PATH_LOCALTIME
-                        " to %s", filename);
-
-       dialog_mesgbox("Done", msg, 8, 72);
-       free(msg);
+               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
-       return DITEM_LEAVE_MENU;
+       } /* 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 *msg;
-       struct tm *tm;
-       time_t t = time(0);
-       int rv;
-       
-       if (setenv("TZ", filename, 1) == -1)
-               err(1, "setenv: cannot set TZ=%s", 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);
 
-       asprintf(&msg, "Does the abbreviation `%s' look reasonable?",
-                tm->tm_zone);
-       rv = !dialog_yesno("Confirmation", msg, 4, 72);
-       free(msg);
-       return rv;
+       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)
 {
-       char *fn;
-       struct zone *zp = dmi->data;
-       int rv;
+       struct zone     *zp = dmi->data;
+       int             rv;
 
        if (!confirm_zone(zp->filename))
-               return DITEM_FAILURE | DITEM_RECREATE;
+               return (DITEM_FAILURE | DITEM_RECREATE);
 
-       asprintf(&fn, "%s/%s", _PATH_ZONEINFO, zp->filename);
-       rv = install_zone_file(fn);
-       free(fn);
-       return rv;
+       rv = install_zoneinfo(zp->filename);
+       return (rv);
 }
 
 static int
 set_zone_whole_country(dialogMenuItem *dmi)
 {
-       char *fn;
-       struct country *cp = dmi->data;
-       int rv;
+       struct country  *cp = dmi->data;
+       int             rv;
 
        if (!confirm_zone(cp->filename))
-               return DITEM_FAILURE | DITEM_RECREATE;
+               return (DITEM_FAILURE | DITEM_RECREATE);
 
-       asprintf(&fn, "%s/%s", _PATH_ZONEINFO, cp->filename);
-       rv = install_zone_file(fn);
-       free(fn);
-       return rv;
+       rv = install_zoneinfo(cp->filename);
+       return (rv);
 }
 
 static void
 usage(void)
 {
-       fprintf(stderr, "usage: tzsetup [-n]\n");
+
+       fprintf(stderr, "usage: tzsetup [-nrs] [-C chroot_directory]"
+           " [zoneinfo_file | zoneinfo_name]\n");
        exit(1);
 }
 
 int
 main(int argc, char **argv)
 {
-       int c, fd;
-       int (*dialog_utc)(unsigned char *, unsigned char *, int, int);
-
-       dialog_utc = dialog_noyes;
+       char            title[64], prompt[128];
+       int             c, fd, rv, skiputc;
 
-       while ((c = getopt(argc, argv, "n")) != -1) {
+       skiputc = 0;
+       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();
                }
@@ -649,48 +921,119 @@ main(int argc, char **argv)
        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. */
-       umask(S_IWGRP|S_IWOTH);
+       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();
-       if (!dialog_utc("Select local or UTC (Greenwich Mean Time) clock",
-                       "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!", 7, 72)) {
-               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)
-                               err(1, "create %s", _PATH_WALL_CMOS_CLOCK);
-                       close(fd);
+       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);
+               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();
        }
-       dialog_clear_norefresh();
        if (optind == argc - 1) {
-               char *msg;
-               asprintf(&msg, "\nUse the default `%s' zone?", argv[optind]);
-               if (!dialog_yesno("Default timezone provided", msg, 7, 72)) {
-                       install_zone_file(argv[optind]);
-                       dialog_clear();
+               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();
-                       return 0;
+                       exit(rv & ~DITEM_LEAVE_MENU);
                }
-               free(msg);
-               dialog_clear_norefresh();
+               dlg_clear();
        }
-       dialog_menu("Time Zone Selector", "Select a region", -1, -1, 
-                   NCONTINENTS, -NCONTINENTS, continents, 0, NULL, NULL);
+       snprintf(title, sizeof(title), "Time Zone Selector");
+       snprintf(prompt, sizeof(prompt), "Select a region");
+       xdialog_menu(title, prompt, -1, -1, NCONTINENTS, NCONTINENTS,
+           continents);
 
-       dialog_clear();
+       dlg_clear();
        end_dialog();
-       return 0;
+       return (0);
 }
-