119872Swollman/*
219872Swollman * Copyright 1996 Massachusetts Institute of Technology
319872Swollman *
419872Swollman * Permission to use, copy, modify, and distribute this software and
519872Swollman * its documentation for any purpose and without fee is hereby
619872Swollman * granted, provided that both the above copyright notice and this
719872Swollman * permission notice appear in all copies, that both the above
819872Swollman * copyright notice and this permission notice appear in all
919872Swollman * supporting documentation, and that the name of M.I.T. not be used
1019872Swollman * in advertising or publicity pertaining to distribution of the
1119872Swollman * software without specific, written prior permission.  M.I.T. makes
1219872Swollman * no representations about the suitability of this software for any
1319872Swollman * purpose.  It is provided "as is" without express or implied
1419872Swollman * warranty.
15179530Sjkim *
1619872Swollman * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
1719872Swollman * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
1819872Swollman * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
1919872Swollman * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
2019872Swollman * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2119872Swollman * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2219872Swollman * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
2319872Swollman * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
2419872Swollman * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2519872Swollman * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
2619872Swollman * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2719872Swollman * SUCH DAMAGE.
2819872Swollman */
2919872Swollman
3019872Swollman/*
3119872Swollman * Second attempt at a `tzmenu' program, using the separate description
3219872Swollman * files provided in newer tzdata releases.
3319872Swollman */
3419872Swollman
35179530Sjkim#include <sys/cdefs.h>
36179530Sjkim__FBSDID("$FreeBSD: releng/11.0/usr.sbin/tzsetup/tzsetup.c 301131 2016-06-01 15:39:11Z smh $");
3730763Scharnier
3819872Swollman#include <err.h>
3919872Swollman#include <errno.h>
4019872Swollman#include <stdio.h>
4119872Swollman#include <stdlib.h>
4219872Swollman#include <string.h>
4366907Swollman#include <time.h>
4419872Swollman#include <unistd.h>
4519872Swollman
4619872Swollman#include <sys/fcntl.h>
47198267Sedwin#include <sys/param.h>
4819872Swollman#include <sys/queue.h>
4919872Swollman#include <sys/stat.h>
50274394Sdteske#include <sys/sysctl.h>
5119872Swollman
52227934Sfjoe#include <dialog.h>
53227934Sfjoe
54179530Sjkim#define	_PATH_ZONETAB		"/usr/share/zoneinfo/zone.tab"
55179530Sjkim#define	_PATH_ISO3166		"/usr/share/misc/iso3166"
56179530Sjkim#define	_PATH_ZONEINFO		"/usr/share/zoneinfo"
57179530Sjkim#define	_PATH_LOCALTIME		"/etc/localtime"
58198267Sedwin#define	_PATH_DB		"/var/db/zoneinfo"
59179530Sjkim#define	_PATH_WALL_CMOS_CLOCK	"/etc/wall_cmos_clock"
6019872Swollman
61230005Swollman#ifdef PATH_MAX
62230005Swollman#define	SILLY_BUFFER_SIZE	2*PATH_MAX
63230005Swollman#else
64230005Swollman#warning "Somebody needs to fix this to dynamically size this buffer."
65230005Swollman#define	SILLY_BUFFER_SIZE	2048
66230005Swollman#endif
67230005Swollman
68227934Sfjoe/* special return codes for `fire' actions */
69227934Sfjoe#define DITEM_FAILURE           1
70227934Sfjoe
71227934Sfjoe/* flags - returned in upper 16 bits of return status */
72227934Sfjoe#define DITEM_LEAVE_MENU        (1 << 16)
73227934Sfjoe#define DITEM_RECREATE          (1 << 18)
74227934Sfjoe
75227934Sfjoe/* for use in describing more exotic behaviors */
76227934Sfjoetypedef struct dialogMenuItem {
77227934Sfjoe	char *prompt;
78227934Sfjoe	char *title;
79227934Sfjoe	int (*fire)(struct dialogMenuItem *self);
80227934Sfjoe	void *data;
81227934Sfjoe} dialogMenuItem;
82227934Sfjoe
83227934Sfjoestatic int
84228176Sfjoexdialog_count_rows(const char *p)
85228176Sfjoe{
86228176Sfjoe	int rows = 0;
87228176Sfjoe
88228176Sfjoe	while ((p = strchr(p, '\n')) != NULL) {
89228176Sfjoe		p++;
90228176Sfjoe		if (*p == '\0')
91228176Sfjoe			break;
92228176Sfjoe		rows++;
93228176Sfjoe	}
94228176Sfjoe
95228176Sfjoe	return rows ? rows : 1;
96228176Sfjoe}
97228176Sfjoe
98228176Sfjoestatic int
99228176Sfjoexdialog_count_columns(const char *p)
100228176Sfjoe{
101228176Sfjoe	int len;
102228176Sfjoe	int max_len = 0;
103228176Sfjoe	const char *q;
104228176Sfjoe
105228176Sfjoe	for (; (q = strchr(p, '\n')) != NULL; p = q + 1) {
106228176Sfjoe		len = q - p;
107228176Sfjoe		max_len = MAX(max_len, len);
108228176Sfjoe	}
109228176Sfjoe
110228176Sfjoe	len = strlen(p);
111228176Sfjoe	max_len = MAX(max_len, len);
112228176Sfjoe	return max_len;
113228176Sfjoe}
114228176Sfjoe
115228176Sfjoestatic int
116227934Sfjoexdialog_menu(const char *title, const char *cprompt, int height, int width,
117227934Sfjoe	     int menu_height, int item_no, dialogMenuItem *ditems)
118227934Sfjoe{
119227947Sfjoe	int i, result, choice = 0;
120227934Sfjoe	DIALOG_LISTITEM *listitems;
121227934Sfjoe	DIALOG_VARS save_vars;
122227934Sfjoe
123227934Sfjoe	dlg_save_vars(&save_vars);
124227934Sfjoe
125227934Sfjoe	/* initialize list items */
126227947Sfjoe	listitems = dlg_calloc(DIALOG_LISTITEM, item_no + 1);
127227934Sfjoe	assert_ptr(listitems, "xdialog_menu");
128227934Sfjoe	for (i = 0; i < item_no; i++) {
129227934Sfjoe		listitems[i].name = ditems[i].prompt;
130227934Sfjoe		listitems[i].text = ditems[i].title;
131227934Sfjoe	}
132227934Sfjoe
133228176Sfjoe	/* calculate height */
134228176Sfjoe	if (height < 0)
135228176Sfjoe		height = xdialog_count_rows(cprompt) + menu_height + 4 + 2;
136228176Sfjoe	if (height > LINES)
137228176Sfjoe		height = LINES;
138228176Sfjoe
139227934Sfjoe	/* calculate width */
140227934Sfjoe	if (width < 0) {
141227934Sfjoe		int tag_x = 0;
142227934Sfjoe
143227934Sfjoe		for (i = 0; i < item_no; i++) {
144227934Sfjoe			int j, l;
145227934Sfjoe
146227934Sfjoe			l = strlen(listitems[i].name);
147227934Sfjoe			for (j = 0; j < item_no; j++) {
148227934Sfjoe				int k = strlen(listitems[j].text);
149227934Sfjoe				tag_x = MAX(tag_x, l + k + 2);
150227934Sfjoe			}
151227934Sfjoe		}
152228176Sfjoe		width = MAX(xdialog_count_columns(cprompt), title != NULL ? xdialog_count_columns(title) : 0);
153227934Sfjoe		width = MAX(width, tag_x + 4) + 4;
154227934Sfjoe	}
155227934Sfjoe	width = MAX(width, 24);
156227934Sfjoe	if (width > COLS)
157227934Sfjoe		width = COLS;
158227934Sfjoe
159227934Sfjoeagain:
160227947Sfjoe	dialog_vars.default_item = listitems[choice].name;
161227934Sfjoe	result = dlg_menu(title, cprompt, height, width,
162227934Sfjoe	    menu_height, item_no, listitems, &choice, NULL);
163227934Sfjoe	switch (result) {
164227934Sfjoe	case DLG_EXIT_ESC:
165227934Sfjoe		result = -1;
166227934Sfjoe		break;
167227934Sfjoe	case DLG_EXIT_OK:
168227934Sfjoe		if (ditems[choice].fire != NULL) {
169227934Sfjoe			int status;
170227934Sfjoe
171227934Sfjoe			status = ditems[choice].fire(ditems + choice);
172227934Sfjoe			if (status & DITEM_RECREATE) {
173227934Sfjoe				dlg_clear();
174227934Sfjoe				goto again;
175227934Sfjoe			}
176227934Sfjoe		}
177227934Sfjoe		result = 0;
178227934Sfjoe		break;
179227934Sfjoe	case DLG_EXIT_CANCEL:
180227934Sfjoe	default:
181227934Sfjoe		result = 1;
182227934Sfjoe		break;
183227934Sfjoe	}
184227934Sfjoe
185227934Sfjoe	free(listitems);
186227934Sfjoe	dlg_restore_vars(&save_vars);
187227934Sfjoe	return result;
188227934Sfjoe}
189227934Sfjoe
190198350Sedwinstatic char	path_zonetab[MAXPATHLEN], path_iso3166[MAXPATHLEN],
191227934Sfjoe		path_zoneinfo[MAXPATHLEN], path_localtime[MAXPATHLEN],
192198350Sedwin		path_db[MAXPATHLEN], path_wall_cmos_clock[MAXPATHLEN];
193198350Sedwin
19419872Swollmanstatic int reallydoit = 1;
195198267Sedwinstatic int reinstall = 0;
196198350Sedwinstatic int usedialog = 1;
197198350Sedwinstatic char *chrootenv = NULL;
19819872Swollman
199179530Sjkimstatic void	usage(void);
200220172Sedwinstatic int	confirm_zone(const char *filename);
201179530Sjkimstatic int	continent_country_menu(dialogMenuItem *);
202301131Ssmhstatic int	install_zoneinfo(const char *zoneinfo);
203220172Sedwinstatic int	install_zoneinfo_file(const char *zoneinfo_file);
204179530Sjkimstatic int	set_zone_multi(dialogMenuItem *);
205179530Sjkimstatic int	set_zone_whole_country(dialogMenuItem *);
206179530Sjkimstatic int	set_zone_menu(dialogMenuItem *);
207220172Sedwinstatic int	set_zone_utc(void);
20819872Swollman
20919872Swollmanstruct continent {
21019872Swollman	dialogMenuItem *menu;
211179530Sjkim	int		nitems;
21219872Swollman};
21319872Swollman
214179530Sjkimstatic struct continent	africa, america, antarctica, arctic, asia, atlantic;
215220172Sedwinstatic struct continent	australia, europe, indian, pacific, utc;
21619872Swollman
21719872Swollmanstatic struct continent_names {
218179530Sjkim	const char	*name;
21919872Swollman	struct continent *continent;
22019872Swollman} continent_names[] = {
221179530Sjkim	{ "Africa",	&africa },
222179530Sjkim	{ "America",	&america },
223179530Sjkim	{ "Antarctica",	&antarctica },
224179530Sjkim	{ "Arctic",	&arctic },
225179530Sjkim	{ "Asia",	&asia },
226179530Sjkim	{ "Atlantic",	&atlantic },
227179530Sjkim	{ "Australia",	&australia },
228179530Sjkim	{ "Europe",	&europe },
229179530Sjkim	{ "Indian",	&indian },
230220172Sedwin	{ "Pacific",	&pacific },
231227934Sfjoe	{ "UTC",	&utc }
23219872Swollman};
23319872Swollman
234179530Sjkimstatic struct continent_items {
235179530Sjkim	char		prompt[2];
236179530Sjkim	char		title[30];
237179530Sjkim} continent_items[] = {
238179530Sjkim	{ "1",	"Africa" },
239179530Sjkim	{ "2",	"America -- North and South" },
240179530Sjkim	{ "3",	"Antarctica" },
241179530Sjkim	{ "4",	"Arctic Ocean" },
242179530Sjkim	{ "5",	"Asia" },
243179530Sjkim	{ "6",	"Atlantic Ocean" },
244179530Sjkim	{ "7",	"Australia" },
245179530Sjkim	{ "8",	"Europe" },
246179530Sjkim	{ "9",	"Indian Ocean" },
247220172Sedwin	{ "0",	"Pacific Ocean" },
248220172Sedwin	{ "a",	"UTC" }
24919872Swollman};
25019872Swollman
251179530Sjkim#define	NCONTINENTS	\
252179530Sjkim    (int)((sizeof(continent_items)) / (sizeof(continent_items[0])))
253179530Sjkimstatic dialogMenuItem continents[NCONTINENTS];
254179530Sjkim
255179530Sjkim#define	OCEANP(x)	((x) == 3 || (x) == 5 || (x) == 8 || (x) == 9)
256179530Sjkim
25719872Swollmanstatic int
25819872Swollmancontinent_country_menu(dialogMenuItem *continent)
25919872Swollman{
260179530Sjkim	char		title[64], prompt[64];
26119872Swollman	struct continent *contp = continent->data;
262179530Sjkim	int		isocean = OCEANP(continent - continents);
263179530Sjkim	int		menulen;
264179530Sjkim	int		rv;
26519872Swollman
266220172Sedwin	if (strcmp(continent->title, "UTC") == 0)
267227934Sfjoe	        return set_zone_utc();
268220172Sedwin
26919872Swollman	/* Short cut -- if there's only one country, don't post a menu. */
270179497Sjkim	if (contp->nitems == 1)
271179497Sjkim		return (contp->menu[0].fire(&contp->menu[0]));
27219872Swollman
27319872Swollman	/* It's amazing how much good grammar really matters... */
274179530Sjkim	if (!isocean) {
275179530Sjkim		snprintf(title, sizeof(title), "Countries in %s",
276179530Sjkim		    continent->title);
277179530Sjkim		snprintf(prompt, sizeof(prompt), "Select a country or region");
278179530Sjkim	} else {
279179530Sjkim		snprintf(title, sizeof(title), "Islands and groups in the %s",
280179530Sjkim		    continent->title);
281179530Sjkim		snprintf(prompt, sizeof(prompt), "Select an island or group");
282179530Sjkim	}
28319872Swollman
28419872Swollman	menulen = contp->nitems < 16 ? contp->nitems : 16;
285227934Sfjoe	rv = xdialog_menu(title, prompt, -1, -1, menulen, contp->nitems,
286227934Sfjoe	    contp->menu);
28719872Swollman	if (rv == 0)
288179530Sjkim		return (DITEM_LEAVE_MENU);
289179530Sjkim	return (DITEM_RECREATE);
29019872Swollman}
29119872Swollman
29219872Swollmanstatic struct continent *
29319872Swollmanfind_continent(const char *name)
29419872Swollman{
295179530Sjkim	int		i;
29619872Swollman
297179530Sjkim	for (i = 0; i < NCONTINENTS; i++)
29819872Swollman		if (strcmp(name, continent_names[i].name) == 0)
299179530Sjkim			return (continent_names[i].continent);
300179530Sjkim	return (0);
30119872Swollman}
30219872Swollman
30319872Swollmanstruct country {
304179530Sjkim	char		*name;
305179530Sjkim	char		*tlc;
306179530Sjkim	int		nzones;
307179530Sjkim	char		*filename;	/* use iff nzones < 0 */
308179530Sjkim	struct continent *continent;	/* use iff nzones < 0 */
309179530Sjkim	TAILQ_HEAD(, zone) zones;	/* use iff nzones > 0 */
310179530Sjkim	dialogMenuItem	*submenu;	/* use iff nzones > 0 */
31119872Swollman};
31219872Swollman
31319872Swollmanstruct zone {
31460938Sjake	TAILQ_ENTRY(zone) link;
315179530Sjkim	char		*descr;
316179530Sjkim	char		*filename;
31719872Swollman	struct continent *continent;
31819872Swollman};
31919872Swollman
32019872Swollman/*
32119872Swollman * This is the easiest organization... we use ISO 3166 country codes,
32219872Swollman * of the two-letter variety, so we just size this array to suit.
32319872Swollman * Beats worrying about dynamic allocation.
32419872Swollman */
325179530Sjkim#define	NCOUNTRIES	(26 * 26)
32622181Sjhaystatic struct country countries[NCOUNTRIES];
32719872Swollman
328179530Sjkim#define	CODE2INT(s)	((s[0] - 'A') * 26 + (s[1] - 'A'))
329179530Sjkim
33019872Swollman/*
33119872Swollman * Read the ISO 3166 country code database in _PATH_ISO3166
33219872Swollman * (/usr/share/misc/iso3166).  On error, exit via err(3).
33319872Swollman */
33419872Swollmanstatic void
33519872Swollmanread_iso3166_table(void)
33619872Swollman{
337179530Sjkim	FILE		*fp;
338179530Sjkim	struct country	*cp;
339179530Sjkim	size_t		len;
340179530Sjkim	char		*s, *t, *name;
341179530Sjkim	int		lineno;
34219872Swollman
343198350Sedwin	fp = fopen(path_iso3166, "r");
34419872Swollman	if (!fp)
345209190Semaste		err(1, "%s", path_iso3166);
34619872Swollman	lineno = 0;
34719872Swollman
348298033Saraujo	while ((s = fgetln(fp, &len)) != NULL) {
34919872Swollman		lineno++;
35019872Swollman		if (s[len - 1] != '\n')
351198350Sedwin			errx(1, "%s:%d: invalid format", path_iso3166, lineno);
35219872Swollman		s[len - 1] = '\0';
35330999Sjoerg		if (s[0] == '#' || strspn(s, " \t") == len - 1)
35419872Swollman			continue;
35519872Swollman
35619872Swollman		/* Isolate the two-letter code. */
35719872Swollman		t = strsep(&s, "\t");
358298033Saraujo		if (t == NULL || strlen(t) != 2)
359198350Sedwin			errx(1, "%s:%d: invalid format", path_iso3166, lineno);
36019872Swollman		if (t[0] < 'A' || t[0] > 'Z' || t[1] < 'A' || t[1] > 'Z')
361198350Sedwin			errx(1, "%s:%d: invalid code `%s'", path_iso3166,
362179530Sjkim			    lineno, t);
36319872Swollman
36419872Swollman		/* Now skip past the three-letter and numeric codes. */
365179530Sjkim		name = strsep(&s, "\t");	/* 3-let */
366298033Saraujo		if (name == NULL || strlen(name) != 3)
367198350Sedwin			errx(1, "%s:%d: invalid format", path_iso3166, lineno);
368179530Sjkim		name = strsep(&s, "\t");	/* numeric */
369298033Saraujo		if (name == NULL || strlen(name) != 3)
370198350Sedwin			errx(1, "%s:%d: invalid format", path_iso3166, lineno);
37119872Swollman
37219872Swollman		name = s;
37319872Swollman
37419872Swollman		cp = &countries[CODE2INT(t)];
37519872Swollman		if (cp->name)
376198350Sedwin			errx(1, "%s:%d: country code `%s' multiply defined: %s",
377198350Sedwin			    path_iso3166, lineno, t, cp->name);
37819872Swollman		cp->name = strdup(name);
37956487Scharnier		if (cp->name == NULL)
38056487Scharnier			errx(1, "malloc failed");
38119872Swollman		cp->tlc = strdup(t);
38256487Scharnier		if (cp->tlc == NULL)
38356487Scharnier			errx(1, "malloc failed");
38419872Swollman	}
38519872Swollman
38619872Swollman	fclose(fp);
38719872Swollman}
38819872Swollman
38919872Swollmanstatic void
39019872Swollmanadd_zone_to_country(int lineno, const char *tlc, const char *descr,
391179530Sjkim    const char *file, struct continent *cont)
39219872Swollman{
393179530Sjkim	struct zone	*zp;
394179530Sjkim	struct country	*cp;
39519872Swollman
39619872Swollman	if (tlc[0] < 'A' || tlc[0] > 'Z' || tlc[1] < 'A' || tlc[1] > 'Z')
397198350Sedwin		errx(1, "%s:%d: country code `%s' invalid", path_zonetab,
398179530Sjkim		    lineno, tlc);
399227934Sfjoe
40019872Swollman	cp = &countries[CODE2INT(tlc)];
40119872Swollman	if (cp->name == 0)
402198350Sedwin		errx(1, "%s:%d: country code `%s' unknown", path_zonetab,
403179530Sjkim		    lineno, tlc);
40419872Swollman
40519872Swollman	if (descr) {
40619872Swollman		if (cp->nzones < 0)
407198350Sedwin			errx(1, "%s:%d: conflicting zone definition",
408198350Sedwin			    path_zonetab, lineno);
40919872Swollman
410179530Sjkim		zp = malloc(sizeof(*zp));
411298033Saraujo		if (zp == NULL)
412179530Sjkim			errx(1, "malloc(%zu)", sizeof(*zp));
413227934Sfjoe
41419872Swollman		if (cp->nzones == 0)
41519872Swollman			TAILQ_INIT(&cp->zones);
41619872Swollman
41719872Swollman		zp->descr = strdup(descr);
41856487Scharnier		if (zp->descr == NULL)
41956487Scharnier			errx(1, "malloc failed");
42019872Swollman		zp->filename = strdup(file);
42156487Scharnier		if (zp->filename == NULL)
42256487Scharnier			errx(1, "malloc failed");
42319872Swollman		zp->continent = cont;
42419872Swollman		TAILQ_INSERT_TAIL(&cp->zones, zp, link);
42519872Swollman		cp->nzones++;
42619872Swollman	} else {
42719872Swollman		if (cp->nzones > 0)
428198350Sedwin			errx(1, "%s:%d: zone must have description",
429198350Sedwin			    path_zonetab, lineno);
43019872Swollman		if (cp->nzones < 0)
431198350Sedwin			errx(1, "%s:%d: zone multiply defined",
432198350Sedwin			    path_zonetab, lineno);
43319872Swollman		cp->nzones = -1;
43419872Swollman		cp->filename = strdup(file);
43556487Scharnier		if (cp->filename == NULL)
43656487Scharnier			errx(1, "malloc failed");
43719872Swollman		cp->continent = cont;
43819872Swollman	}
43919872Swollman}
44019872Swollman
44119872Swollman/*
44219872Swollman * This comparison function intentionally sorts all of the null-named
44319872Swollman * ``countries''---i.e., the codes that don't correspond to a real
44419872Swollman * country---to the end.  Everything else is lexical by country name.
44519872Swollman */
44619872Swollmanstatic int
44719872Swollmancompare_countries(const void *xa, const void *xb)
44819872Swollman{
44919872Swollman	const struct country *a = xa, *b = xb;
45019872Swollman
45119872Swollman	if (a->name == 0 && b->name == 0)
452179530Sjkim		return (0);
45319872Swollman	if (a->name == 0 && b->name != 0)
454179530Sjkim		return (1);
45519872Swollman	if (b->name == 0)
456179530Sjkim		return (-1);
45719872Swollman
458179530Sjkim	return (strcmp(a->name, b->name));
45919872Swollman}
46019872Swollman
46119872Swollman/*
46219872Swollman * This must be done AFTER all zone descriptions are read, since it breaks
46319872Swollman * CODE2INT().
46419872Swollman */
46519872Swollmanstatic void
46619872Swollmansort_countries(void)
46719872Swollman{
468179530Sjkim
469179530Sjkim	qsort(countries, NCOUNTRIES, sizeof(countries[0]), compare_countries);
47019872Swollman}
47119872Swollman
47219872Swollmanstatic void
47319872Swollmanread_zones(void)
47419872Swollman{
475179530Sjkim	char		contbuf[16];
476179530Sjkim	FILE		*fp;
47719872Swollman	struct continent *cont;
478179530Sjkim	size_t		len;
479281733Seadler	char		*line, *tlc, *file, *descr, *p;
480179530Sjkim	int		lineno;
48119872Swollman
482198350Sedwin	fp = fopen(path_zonetab, "r");
48319872Swollman	if (!fp)
484209190Semaste		err(1, "%s", path_zonetab);
48519872Swollman	lineno = 0;
48619872Swollman
487298033Saraujo	while ((line = fgetln(fp, &len)) != NULL) {
48819872Swollman		lineno++;
48919872Swollman		if (line[len - 1] != '\n')
490198350Sedwin			errx(1, "%s:%d: invalid format", path_zonetab, lineno);
49119872Swollman		line[len - 1] = '\0';
49219872Swollman		if (line[0] == '#')
49319872Swollman			continue;
49419872Swollman
49519872Swollman		tlc = strsep(&line, "\t");
49619872Swollman		if (strlen(tlc) != 2)
497198350Sedwin			errx(1, "%s:%d: invalid country code `%s'",
498198350Sedwin			    path_zonetab, lineno, tlc);
499281733Seadler		/* coord = */ strsep(&line, "\t");	 /* Unused */
50019872Swollman		file = strsep(&line, "\t");
50119872Swollman		p = strchr(file, '/');
502298033Saraujo		if (p == NULL)
503198350Sedwin			errx(1, "%s:%d: invalid zone name `%s'", path_zonetab,
504179530Sjkim			    lineno, file);
50519872Swollman		contbuf[0] = '\0';
50619872Swollman		strncat(contbuf, file, p - file);
50719872Swollman		cont = find_continent(contbuf);
50819872Swollman		if (!cont)
509198350Sedwin			errx(1, "%s:%d: invalid region `%s'", path_zonetab,
510179530Sjkim			    lineno, contbuf);
51119872Swollman
512179530Sjkim		descr = (line != NULL && *line != '\0') ? line : NULL;
51319872Swollman
51419872Swollman		add_zone_to_country(lineno, tlc, descr, file, cont);
51519872Swollman	}
51619872Swollman	fclose(fp);
51719872Swollman}
51819872Swollman
51919872Swollmanstatic void
52019872Swollmanmake_menus(void)
52119872Swollman{
522179530Sjkim	struct country	*cp;
523179530Sjkim	struct zone	*zp, *zp2;
52419872Swollman	struct continent *cont;
525179530Sjkim	dialogMenuItem	*dmi;
526179530Sjkim	int		i;
52719872Swollman
52819872Swollman	/*
52919872Swollman	 * First, count up all the countries in each continent/ocean.
53019872Swollman	 * Be careful to count those countries which have multiple zones
53119872Swollman	 * only once for each.  NB: some countries are in multiple
53219872Swollman	 * continents/oceans.
53319872Swollman	 */
53419872Swollman	for (cp = countries; cp->name; cp++) {
53519872Swollman		if (cp->nzones == 0)
53619872Swollman			continue;
53719872Swollman		if (cp->nzones < 0) {
53819872Swollman			cp->continent->nitems++;
53919872Swollman		} else {
54070486Sben			TAILQ_FOREACH(zp, &cp->zones, link) {
54119872Swollman				cont = zp->continent;
54270486Sben				for (zp2 = TAILQ_FIRST(&cp->zones);
543179530Sjkim				    zp2->continent != cont;
544179530Sjkim				    zp2 = TAILQ_NEXT(zp2, link))
54519872Swollman					;
54619872Swollman				if (zp2 == zp)
54719872Swollman					zp->continent->nitems++;
54819872Swollman			}
54919872Swollman		}
55019872Swollman	}
55119872Swollman
55219872Swollman	/*
553179530Sjkim	 * Now allocate memory for the country menus and initialize
554179530Sjkim	 * continent menus.  We set nitems back to zero so that we can
555179530Sjkim	 * use it for counting again when we actually build the menus.
55619872Swollman	 */
557179530Sjkim	memset(continents, 0, sizeof(continents));
55819872Swollman	for (i = 0; i < NCONTINENTS; i++) {
55919872Swollman		continent_names[i].continent->menu =
560179530Sjkim		    malloc(sizeof(dialogMenuItem) *
561179530Sjkim		    continent_names[i].continent->nitems);
562298033Saraujo		if (continent_names[i].continent->menu == NULL)
56356487Scharnier			errx(1, "malloc for continent menu");
56419872Swollman		continent_names[i].continent->nitems = 0;
565179530Sjkim		continents[i].prompt = continent_items[i].prompt;
566179530Sjkim		continents[i].title = continent_items[i].title;
567179530Sjkim		continents[i].fire = continent_country_menu;
568179530Sjkim		continents[i].data = continent_names[i].continent;
56919872Swollman	}
57019872Swollman
57119872Swollman	/*
57219872Swollman	 * Now that memory is allocated, create the menu items for
57319872Swollman	 * each continent.  For multiple-zone countries, also create
57419872Swollman	 * the country's zone submenu.
57519872Swollman	 */
57619872Swollman	for (cp = countries; cp->name; cp++) {
57719872Swollman		if (cp->nzones == 0)
57819872Swollman			continue;
57919872Swollman		if (cp->nzones < 0) {
58019872Swollman			dmi = &cp->continent->menu[cp->continent->nitems];
581179530Sjkim			memset(dmi, 0, sizeof(*dmi));
582179530Sjkim			asprintf(&dmi->prompt, "%d", ++cp->continent->nitems);
58319872Swollman			dmi->title = cp->name;
58419872Swollman			dmi->fire = set_zone_whole_country;
58519872Swollman			dmi->data = cp;
58619872Swollman		} else {
587179530Sjkim			cp->submenu = malloc(cp->nzones * sizeof(*dmi));
58819872Swollman			if (cp->submenu == 0)
58956487Scharnier				errx(1, "malloc for submenu");
59019872Swollman			cp->nzones = 0;
59170486Sben			TAILQ_FOREACH(zp, &cp->zones, link) {
59219872Swollman				cont = zp->continent;
59319872Swollman				dmi = &cp->submenu[cp->nzones];
594179530Sjkim				memset(dmi, 0, sizeof(*dmi));
595179530Sjkim				asprintf(&dmi->prompt, "%d", ++cp->nzones);
59619872Swollman				dmi->title = zp->descr;
59719872Swollman				dmi->fire = set_zone_multi;
59819872Swollman				dmi->data = zp;
59919872Swollman
60070486Sben				for (zp2 = TAILQ_FIRST(&cp->zones);
601179530Sjkim				    zp2->continent != cont;
602179530Sjkim				    zp2 = TAILQ_NEXT(zp2, link))
60319872Swollman					;
60419872Swollman				if (zp2 != zp)
60519872Swollman					continue;
60619872Swollman
60719872Swollman				dmi = &cont->menu[cont->nitems];
608179530Sjkim				memset(dmi, 0, sizeof(*dmi));
60919872Swollman				asprintf(&dmi->prompt, "%d", ++cont->nitems);
61019872Swollman				dmi->title = cp->name;
61119872Swollman				dmi->fire = set_zone_menu;
61219872Swollman				dmi->data = cp;
61319872Swollman			}
61419872Swollman		}
61519872Swollman	}
61619872Swollman}
61719872Swollman
61819872Swollmanstatic int
61919872Swollmanset_zone_menu(dialogMenuItem *dmi)
62019872Swollman{
621179530Sjkim	char		title[64], prompt[64];
622179530Sjkim	struct country	*cp = dmi->data;
623179530Sjkim	int		menulen;
624179530Sjkim	int		rv;
62519872Swollman
626179530Sjkim	snprintf(title, sizeof(title), "%s Time Zones", cp->name);
627179530Sjkim	snprintf(prompt, sizeof(prompt),
628179530Sjkim	    "Select a zone which observes the same time as your locality.");
62919872Swollman	menulen = cp->nzones < 16 ? cp->nzones : 16;
630227934Sfjoe	rv = xdialog_menu(title, prompt, -1, -1, menulen, cp->nzones,
631227934Sfjoe	    cp->submenu);
63219872Swollman	if (rv != 0)
633179530Sjkim		return (DITEM_RECREATE);
634179530Sjkim	return (DITEM_LEAVE_MENU);
63519872Swollman}
63619872Swollman
637301131Ssmhstatic int
638220172Sedwinset_zone_utc(void)
639220172Sedwin{
640220172Sedwin	if (!confirm_zone(NULL))
641220172Sedwin		return (DITEM_FAILURE | DITEM_RECREATE);
642227934Sfjoe
643301131Ssmh	return (install_zoneinfo("UTC"));
644220172Sedwin}
645220172Sedwin
64619872Swollmanstatic int
647198350Sedwininstall_zoneinfo_file(const char *zoneinfo_file)
64819872Swollman{
649179530Sjkim	char		buf[1024];
650230005Swollman	char		title[64], prompt[SILLY_BUFFER_SIZE];
651179530Sjkim	struct stat	sb;
652179530Sjkim	ssize_t		len;
653179530Sjkim	int		fd1, fd2, copymode;
65419872Swollman
655198350Sedwin	if (lstat(path_localtime, &sb) < 0) {
65619872Swollman		/* Nothing there yet... */
65719872Swollman		copymode = 1;
658179530Sjkim	} else if (S_ISLNK(sb.st_mode))
65919872Swollman		copymode = 0;
66019872Swollman	else
66119872Swollman		copymode = 1;
66219872Swollman
66321915Sjkh#ifdef VERBOSE
664230299Semaste	snprintf(title, sizeof(title), "Info");
665230299Semaste	if (zoneinfo_file == NULL)
666179530Sjkim		snprintf(prompt, sizeof(prompt),
667230299Semaste		    "Removing %s", path_localtime);
668230299Semaste	else if (copymode)
669230299Semaste		snprintf(prompt, sizeof(prompt),
670198350Sedwin		    "Copying %s to %s", zoneinfo_file, path_localtime);
67119872Swollman	else
672179530Sjkim		snprintf(prompt, sizeof(prompt),
673198350Sedwin		    "Creating symbolic link %s to %s",
674230299Semaste		    path_localtime, zoneinfo_file);
675198267Sedwin	if (usedialog)
676230299Semaste		dialog_msgbox(title, prompt, 8, 72, 1);
677198267Sedwin	else
678198267Sedwin		fprintf(stderr, "%s\n", prompt);
67921915Sjkh#endif
68019872Swollman
68119872Swollman	if (reallydoit) {
682220172Sedwin		if (zoneinfo_file == NULL) {
683220172Sedwin			if (unlink(path_localtime) < 0 && errno != ENOENT) {
684220172Sedwin				snprintf(title, sizeof(title), "Error");
685220172Sedwin				snprintf(prompt, sizeof(prompt),
686220172Sedwin				     "Could not delete %s: %s", path_localtime,
687220172Sedwin				     strerror(errno));
688220172Sedwin				if (usedialog)
689227934Sfjoe					dialog_msgbox(title, prompt, 8, 72, 1);
690220172Sedwin				else
691220172Sedwin					fprintf(stderr, "%s\n", prompt);
692220172Sedwin
693220172Sedwin				return (DITEM_FAILURE | DITEM_RECREATE);
694220172Sedwin			}
695227011Sdougb			if (unlink(path_db) < 0 && errno != ENOENT) {
696227011Sdougb				snprintf(title, sizeof(title), "Error");
697227011Sdougb				snprintf(prompt, sizeof(prompt),
698227011Sdougb				     "Could not delete %s: %s", path_db,
699227011Sdougb				     strerror(errno));
700227011Sdougb				if (usedialog)
701227934Sfjoe					dialog_msgbox(title, prompt, 8, 72, 1);
702227011Sdougb				else
703227011Sdougb					fprintf(stderr, "%s\n", prompt);
704227011Sdougb
705227011Sdougb				return (DITEM_FAILURE | DITEM_RECREATE);
706227011Sdougb			}
707230299Semaste#ifdef VERBOSE
708247780Sdteske			snprintf(title, sizeof(title), "Done");
709230299Semaste			snprintf(prompt, sizeof(prompt),
710230299Semaste			    "Removed %s", path_localtime);
711247780Sdteske			if (usedialog)
712247780Sdteske				dialog_msgbox(title, prompt, 8, 72, 1);
713247780Sdteske			else
714247780Sdteske				fprintf(stderr, "%s\n", prompt);
715230299Semaste#endif
716220172Sedwin			return (DITEM_LEAVE_MENU);
717220172Sedwin		}
718227934Sfjoe
71919872Swollman		if (copymode) {
720198350Sedwin			fd1 = open(zoneinfo_file, O_RDONLY, 0);
72119872Swollman			if (fd1 < 0) {
722179530Sjkim				snprintf(title, sizeof(title), "Error");
723179530Sjkim				snprintf(prompt, sizeof(prompt),
724198350Sedwin				    "Could not open %s: %s", zoneinfo_file,
725179530Sjkim				    strerror(errno));
726198267Sedwin				if (usedialog)
727227934Sfjoe					dialog_msgbox(title, prompt, 8, 72, 1);
728198267Sedwin				else
729198267Sedwin					fprintf(stderr, "%s\n", prompt);
730179530Sjkim				return (DITEM_FAILURE | DITEM_RECREATE);
73119872Swollman			}
73219872Swollman
733231181Swollman			if (unlink(path_localtime) < 0 && errno != ENOENT) {
734230005Swollman				snprintf(prompt, sizeof(prompt),
735230005Swollman				    "Could not unlink %s: %s",
736230005Swollman				    path_localtime, strerror(errno));
737230005Swollman				if (usedialog) {
738230005Swollman					snprintf(title, sizeof(title), "Error");
739230005Swollman					dialog_msgbox(title, prompt, 8, 72, 1);
740230005Swollman				} else
741230005Swollman					fprintf(stderr, "%s\n", prompt);
742230005Swollman				return (DITEM_FAILURE | DITEM_RECREATE);
743230005Swollman			}
744230005Swollman
745198350Sedwin			fd2 = open(path_localtime, O_CREAT | O_EXCL | O_WRONLY,
746179530Sjkim			    S_IRUSR | S_IRGRP | S_IROTH);
74719872Swollman			if (fd2 < 0) {
748179530Sjkim				snprintf(title, sizeof(title), "Error");
749179530Sjkim				snprintf(prompt, sizeof(prompt),
750198350Sedwin				    "Could not open %s: %s",
751198350Sedwin				    path_localtime, strerror(errno));
752198267Sedwin				if (usedialog)
753227934Sfjoe					dialog_msgbox(title, prompt, 8, 72, 1);
754198267Sedwin				else
755198267Sedwin					fprintf(stderr, "%s\n", prompt);
756179530Sjkim				return (DITEM_FAILURE | DITEM_RECREATE);
75719872Swollman			}
75819872Swollman
759179530Sjkim			while ((len = read(fd1, buf, sizeof(buf))) > 0)
760208830Sedwin				if ((len = write(fd2, buf, len)) < 0)
761208830Sedwin					break;
76219872Swollman
76319872Swollman			if (len == -1) {
764179530Sjkim				snprintf(title, sizeof(title), "Error");
765179530Sjkim				snprintf(prompt, sizeof(prompt),
766198350Sedwin				    "Error copying %s to %s %s", zoneinfo_file,
767198350Sedwin				    path_localtime, strerror(errno));
768198267Sedwin				if (usedialog)
769227934Sfjoe					dialog_msgbox(title, prompt, 8, 72, 1);
770198267Sedwin				else
771198267Sedwin					fprintf(stderr, "%s\n", prompt);
77219872Swollman				/* Better to leave none than a corrupt one. */
773198350Sedwin				unlink(path_localtime);
774179530Sjkim				return (DITEM_FAILURE | DITEM_RECREATE);
77519872Swollman			}
77619872Swollman			close(fd1);
77719872Swollman			close(fd2);
77819872Swollman		} else {
779198350Sedwin			if (access(zoneinfo_file, R_OK) != 0) {
780179530Sjkim				snprintf(title, sizeof(title), "Error");
781179530Sjkim				snprintf(prompt, sizeof(prompt),
782198350Sedwin				    "Cannot access %s: %s", zoneinfo_file,
783179530Sjkim				    strerror(errno));
784198267Sedwin				if (usedialog)
785227934Sfjoe					dialog_msgbox(title, prompt, 8, 72, 1);
786198267Sedwin				else
787198267Sedwin					fprintf(stderr, "%s\n", prompt);
788179530Sjkim				return (DITEM_FAILURE | DITEM_RECREATE);
78919872Swollman			}
790231181Swollman			if (unlink(path_localtime) < 0 && errno != ENOENT) {
791230005Swollman				snprintf(prompt, sizeof(prompt),
792230005Swollman				    "Could not unlink %s: %s",
793230005Swollman				    path_localtime, strerror(errno));
794230005Swollman				if (usedialog) {
795230005Swollman					snprintf(title, sizeof(title), "Error");
796230005Swollman					dialog_msgbox(title, prompt, 8, 72, 1);
797230005Swollman				} else
798230005Swollman					fprintf(stderr, "%s\n", prompt);
799230005Swollman				return (DITEM_FAILURE | DITEM_RECREATE);
800230005Swollman			}
801198350Sedwin			if (symlink(zoneinfo_file, path_localtime) < 0) {
802179530Sjkim				snprintf(title, sizeof(title), "Error");
803179530Sjkim				snprintf(prompt, sizeof(prompt),
804198350Sedwin				    "Cannot create symbolic link %s to %s: %s",
805198350Sedwin				    path_localtime, zoneinfo_file,
806179530Sjkim				    strerror(errno));
807198267Sedwin				if (usedialog)
808227934Sfjoe					dialog_msgbox(title, prompt, 8, 72, 1);
809198267Sedwin				else
810198267Sedwin					fprintf(stderr, "%s\n", prompt);
811179530Sjkim				return (DITEM_FAILURE | DITEM_RECREATE);
81219872Swollman			}
81319872Swollman		}
81419872Swollman
81521915Sjkh#ifdef VERBOSE
816230299Semaste		snprintf(title, sizeof(title), "Done");
817230299Semaste		if (copymode)
818230299Semaste			snprintf(prompt, sizeof(prompt),
819230299Semaste			    "Copied timezone file from %s to %s",
820230299Semaste			    zoneinfo_file, path_localtime);
821230299Semaste		else
822230299Semaste			snprintf(prompt, sizeof(prompt),
823230299Semaste			    "Created symbolic link from %s to %s",
824230299Semaste			    zoneinfo_file, path_localtime);
825230299Semaste		if (usedialog)
826230299Semaste			dialog_msgbox(title, prompt, 8, 72, 1);
827230299Semaste		else
828230299Semaste			fprintf(stderr, "%s\n", prompt);
82921915Sjkh#endif
830230299Semaste	} /* reallydoit */
831198267Sedwin
832198350Sedwin	return (DITEM_LEAVE_MENU);
833198350Sedwin}
834198350Sedwin
835198350Sedwinstatic int
836198350Sedwininstall_zoneinfo(const char *zoneinfo)
837198350Sedwin{
838198350Sedwin	int		rv;
839198350Sedwin	FILE		*f;
840198350Sedwin	char		path_zoneinfo_file[MAXPATHLEN];
841198350Sedwin
842300706Struckman	if ((size_t)snprintf(path_zoneinfo_file, sizeof(path_zoneinfo_file),
843300706Struckman	    "%s/%s", path_zoneinfo, zoneinfo) >= sizeof(path_zoneinfo_file))
844300706Struckman		errx(1, "%s/%s name too long", path_zoneinfo, zoneinfo);
845198350Sedwin	rv = install_zoneinfo_file(path_zoneinfo_file);
846198350Sedwin
847198267Sedwin	/* Save knowledge for later */
848230296Semaste	if (reallydoit && (rv & DITEM_FAILURE) == 0) {
849230296Semaste		if ((f = fopen(path_db, "w")) != NULL) {
850230296Semaste			fprintf(f, "%s\n", zoneinfo);
851230296Semaste			fclose(f);
852230296Semaste		}
853198267Sedwin	}
854198267Sedwin
855198350Sedwin	return (rv);
85619872Swollman}
85719872Swollman
85819872Swollmanstatic int
85919872Swollmanconfirm_zone(const char *filename)
86019872Swollman{
861179530Sjkim	char		title[64], prompt[64];
862179530Sjkim	time_t		t = time(0);
863179530Sjkim	struct tm	*tm;
864179530Sjkim	int		rv;
865227934Sfjoe
866220172Sedwin	setenv("TZ", filename == NULL ? "" : filename, 1);
86719872Swollman	tzset();
86819872Swollman	tm = localtime(&t);
86919872Swollman
870179530Sjkim	snprintf(title, sizeof(title), "Confirmation");
871179530Sjkim	snprintf(prompt, sizeof(prompt),
872179530Sjkim	    "Does the abbreviation `%s' look reasonable?", tm->tm_zone);
873179530Sjkim	rv = !dialog_yesno(title, prompt, 5, 72);
874179530Sjkim	return (rv);
87519872Swollman}
87619872Swollman
87719872Swollmanstatic int
87819872Swollmanset_zone_multi(dialogMenuItem *dmi)
87919872Swollman{
880179530Sjkim	struct zone	*zp = dmi->data;
881179530Sjkim	int		rv;
88219872Swollman
88319872Swollman	if (!confirm_zone(zp->filename))
884179530Sjkim		return (DITEM_FAILURE | DITEM_RECREATE);
88519872Swollman
886198350Sedwin	rv = install_zoneinfo(zp->filename);
887179530Sjkim	return (rv);
88819872Swollman}
88919872Swollman
89019872Swollmanstatic int
89119872Swollmanset_zone_whole_country(dialogMenuItem *dmi)
89219872Swollman{
893179530Sjkim	struct country	*cp = dmi->data;
894179530Sjkim	int		rv;
89519872Swollman
89619872Swollman	if (!confirm_zone(cp->filename))
897179530Sjkim		return (DITEM_FAILURE | DITEM_RECREATE);
89819872Swollman
899198350Sedwin	rv = install_zoneinfo(cp->filename);
900179530Sjkim	return (rv);
90119872Swollman}
90219872Swollman
90330763Scharnierstatic void
904179530Sjkimusage(void)
90530763Scharnier{
906179530Sjkim
907222139Sru	fprintf(stderr, "usage: tzsetup [-nrs] [-C chroot_directory]"
908222139Sru	    " [zoneinfo_file | zoneinfo_name]\n");
90930763Scharnier	exit(1);
91030763Scharnier}
91130763Scharnier
91219872Swollmanint
91319872Swollmanmain(int argc, char **argv)
91419872Swollman{
915179530Sjkim	char		title[64], prompt[128];
916198267Sedwin	int		c, fd, rv, skiputc;
917274399Sdteske	char		vm_guest[16] = "";
918274399Sdteske	size_t		len = sizeof(vm_guest);
91919872Swollman
920195339Sattilio	skiputc = 0;
921274394Sdteske
922274394Sdteske	/* Default skiputc to 1 for VM guests */
923274399Sdteske	if (sysctlbyname("kern.vm_guest", vm_guest, &len, NULL, 0) == 0 &&
924274394Sdteske	    strcmp(vm_guest, "none") != 0)
925274399Sdteske		skiputc = 1;
926274394Sdteske
927198350Sedwin	while ((c = getopt(argc, argv, "C:nrs")) != -1) {
92819872Swollman		switch(c) {
929198350Sedwin		case 'C':
930198350Sedwin			chrootenv = optarg;
931198350Sedwin			break;
93219872Swollman		case 'n':
93319872Swollman			reallydoit = 0;
93419872Swollman			break;
935198267Sedwin		case 'r':
936198267Sedwin			reinstall = 1;
937198350Sedwin			usedialog = 0;
938198267Sedwin			break;
939195339Sattilio		case 's':
940195339Sattilio			skiputc = 1;
941195339Sattilio			break;
94219872Swollman		default:
94330763Scharnier			usage();
94419872Swollman		}
94519872Swollman	}
94619872Swollman
94743544Swollman	if (argc - optind > 1)
94830763Scharnier		usage();
94919872Swollman
950290031Sdelphij	if (chrootenv == NULL) {
951290031Sdelphij		strcpy(path_zonetab, _PATH_ZONETAB);
952290031Sdelphij		strcpy(path_iso3166, _PATH_ISO3166);
953290031Sdelphij		strcpy(path_zoneinfo, _PATH_ZONEINFO);
954290031Sdelphij		strcpy(path_localtime, _PATH_LOCALTIME);
955290031Sdelphij		strcpy(path_db, _PATH_DB);
956290031Sdelphij		strcpy(path_wall_cmos_clock, _PATH_WALL_CMOS_CLOCK);
957290031Sdelphij	} else {
958290031Sdelphij		sprintf(path_zonetab, "%s/%s", chrootenv, _PATH_ZONETAB);
959290031Sdelphij		sprintf(path_iso3166, "%s/%s", chrootenv, _PATH_ISO3166);
960290031Sdelphij		sprintf(path_zoneinfo, "%s/%s", chrootenv, _PATH_ZONEINFO);
961290031Sdelphij		sprintf(path_localtime, "%s/%s", chrootenv, _PATH_LOCALTIME);
962290031Sdelphij		sprintf(path_db, "%s/%s", chrootenv, _PATH_DB);
963290031Sdelphij		sprintf(path_wall_cmos_clock, "%s/%s", chrootenv,
964290031Sdelphij		    _PATH_WALL_CMOS_CLOCK);
965198350Sedwin	}
966198350Sedwin
967198350Sedwin
96849435Sru	/* Override the user-supplied umask. */
969179530Sjkim	(void)umask(S_IWGRP | S_IWOTH);
97049435Sru
971198267Sedwin	if (reinstall == 1) {
972198267Sedwin		FILE *f;
973230520Semaste		char zoneinfo[MAXPATHLEN];
974198267Sedwin
975198350Sedwin		if ((f = fopen(path_db, "r")) != NULL) {
976230520Semaste			if (fgets(zoneinfo, sizeof(zoneinfo), f) != NULL) {
977230520Semaste				zoneinfo[sizeof(zoneinfo) - 1] = 0;
978230520Semaste				if (strlen(zoneinfo) > 0) {
979230520Semaste					zoneinfo[strlen(zoneinfo) - 1] = 0;
980230520Semaste					rv = install_zoneinfo(zoneinfo);
981198267Sedwin					exit(rv & ~DITEM_LEAVE_MENU);
982198267Sedwin				}
983198350Sedwin				errx(1, "Error reading %s.\n", path_db);
984198267Sedwin			}
985198267Sedwin			fclose(f);
986198267Sedwin			errx(1,
987198267Sedwin			    "Unable to determine earlier installed zoneinfo "
988230520Semaste			    "name. Check %s", path_db);
989198267Sedwin		}
990198350Sedwin		errx(1, "Cannot open %s for reading. Does it exist?", path_db);
991198267Sedwin	}
992198267Sedwin
993198350Sedwin	/*
994198350Sedwin	 * If the arguments on the command-line do not specify a file,
995198350Sedwin	 * then interpret it as a zoneinfo name
996198350Sedwin	 */
997198350Sedwin	if (optind == argc - 1) {
998198350Sedwin		struct stat sb;
999198350Sedwin
1000198350Sedwin		if (stat(argv[optind], &sb) != 0) {
1001198350Sedwin			usedialog = 0;
1002198350Sedwin			rv = install_zoneinfo(argv[optind]);
1003198350Sedwin			exit(rv & ~DITEM_LEAVE_MENU);
1004198350Sedwin		}
1005198350Sedwin		/* FALLTHROUGH */
1006198350Sedwin	}
1007198350Sedwin
1008230520Semaste	read_iso3166_table();
1009230520Semaste	read_zones();
1010230520Semaste	sort_countries();
1011230520Semaste	make_menus();
1012230520Semaste
1013227934Sfjoe	init_dialog(stdin, stdout);
1014195339Sattilio	if (skiputc == 0) {
1015227934Sfjoe		DIALOG_VARS save_vars;
1016227934Sfjoe		int yesno;
1017227934Sfjoe
1018195339Sattilio		snprintf(title, sizeof(title),
1019195339Sattilio		    "Select local or UTC (Greenwich Mean Time) clock");
1020195339Sattilio		snprintf(prompt, sizeof(prompt),
1021195339Sattilio		    "Is this machine's CMOS clock set to UTC?  "
1022195339Sattilio		    "If it is set to local time,\n"
1023195339Sattilio		    "or you don't know, please choose NO here!");
1024227934Sfjoe		dlg_save_vars(&save_vars);
1025227934Sfjoe#if !defined(__sparc64__)
1026227934Sfjoe		dialog_vars.defaultno = TRUE;
1027227934Sfjoe#endif
1028227934Sfjoe		yesno = dialog_yesno(title, prompt, 7, 73);
1029227934Sfjoe		dlg_restore_vars(&save_vars);
1030227934Sfjoe		if (!yesno) {
1031195339Sattilio			if (reallydoit)
1032210243Snork				unlink(path_wall_cmos_clock);
1033195339Sattilio		} else {
1034195339Sattilio			if (reallydoit) {
1035210243Snork				fd = open(path_wall_cmos_clock,
1036195339Sattilio				    O_WRONLY | O_CREAT | O_TRUNC,
1037195339Sattilio				    S_IRUSR | S_IRGRP | S_IROTH);
1038198254Sedwin				if (fd < 0) {
1039198254Sedwin					end_dialog();
1040195339Sattilio					err(1, "create %s",
1041210243Snork					    path_wall_cmos_clock);
1042198254Sedwin				}
1043195339Sattilio				close(fd);
1044195339Sattilio			}
104541852Speter		}
1046227934Sfjoe		dlg_clear();
104732394Ssteve	}
104843544Swollman	if (optind == argc - 1) {
1049179530Sjkim		snprintf(title, sizeof(title), "Default timezone provided");
1050179530Sjkim		snprintf(prompt, sizeof(prompt),
1051179530Sjkim		    "\nUse the default `%s' zone?", argv[optind]);
1052179530Sjkim		if (!dialog_yesno(title, prompt, 7, 72)) {
1053198350Sedwin			rv = install_zoneinfo_file(argv[optind]);
1054227934Sfjoe			dlg_clear();
105543544Swollman			end_dialog();
1056198267Sedwin			exit(rv & ~DITEM_LEAVE_MENU);
105743544Swollman		}
1058227934Sfjoe		dlg_clear();
105943544Swollman	}
1060179530Sjkim	snprintf(title, sizeof(title), "Time Zone Selector");
1061179530Sjkim	snprintf(prompt, sizeof(prompt), "Select a region");
1062227934Sfjoe	xdialog_menu(title, prompt, -1, -1, NCONTINENTS, NCONTINENTS,
1063227934Sfjoe	    continents);
106443544Swollman
1065227934Sfjoe	dlg_clear();
106619872Swollman	end_dialog();
1067179530Sjkim	return (0);
106819872Swollman}
1069