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$");
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>
5019872Swollman
51227934Sfjoe#include <dialog.h>
52227934Sfjoe
53179530Sjkim#define	_PATH_ZONETAB		"/usr/share/zoneinfo/zone.tab"
54179530Sjkim#define	_PATH_ISO3166		"/usr/share/misc/iso3166"
55179530Sjkim#define	_PATH_ZONEINFO		"/usr/share/zoneinfo"
56179530Sjkim#define	_PATH_LOCALTIME		"/etc/localtime"
57198267Sedwin#define	_PATH_DB		"/var/db/zoneinfo"
58179530Sjkim#define	_PATH_WALL_CMOS_CLOCK	"/etc/wall_cmos_clock"
5919872Swollman
60230005Swollman#ifdef PATH_MAX
61230005Swollman#define	SILLY_BUFFER_SIZE	2*PATH_MAX
62230005Swollman#else
63230005Swollman#warning "Somebody needs to fix this to dynamically size this buffer."
64230005Swollman#define	SILLY_BUFFER_SIZE	2048
65230005Swollman#endif
66230005Swollman
67227934Sfjoe/* special return codes for `fire' actions */
68227934Sfjoe#define DITEM_FAILURE           1
69227934Sfjoe
70227934Sfjoe/* flags - returned in upper 16 bits of return status */
71227934Sfjoe#define DITEM_LEAVE_MENU        (1 << 16)
72227934Sfjoe#define DITEM_RECREATE          (1 << 18)
73227934Sfjoe
74227934Sfjoe/* for use in describing more exotic behaviors */
75227934Sfjoetypedef struct dialogMenuItem {
76227934Sfjoe	char *prompt;
77227934Sfjoe	char *title;
78227934Sfjoe	int (*fire)(struct dialogMenuItem *self);
79227934Sfjoe	void *data;
80227934Sfjoe} dialogMenuItem;
81227934Sfjoe
82227934Sfjoestatic int
83228176Sfjoexdialog_count_rows(const char *p)
84228176Sfjoe{
85228176Sfjoe	int rows = 0;
86228176Sfjoe
87228176Sfjoe	while ((p = strchr(p, '\n')) != NULL) {
88228176Sfjoe		p++;
89228176Sfjoe		if (*p == '\0')
90228176Sfjoe			break;
91228176Sfjoe		rows++;
92228176Sfjoe	}
93228176Sfjoe
94228176Sfjoe	return rows ? rows : 1;
95228176Sfjoe}
96228176Sfjoe
97228176Sfjoestatic int
98228176Sfjoexdialog_count_columns(const char *p)
99228176Sfjoe{
100228176Sfjoe	int len;
101228176Sfjoe	int max_len = 0;
102228176Sfjoe	const char *q;
103228176Sfjoe
104228176Sfjoe	for (; (q = strchr(p, '\n')) != NULL; p = q + 1) {
105228176Sfjoe		len = q - p;
106228176Sfjoe		max_len = MAX(max_len, len);
107228176Sfjoe	}
108228176Sfjoe
109228176Sfjoe	len = strlen(p);
110228176Sfjoe	max_len = MAX(max_len, len);
111228176Sfjoe	return max_len;
112228176Sfjoe}
113228176Sfjoe
114228176Sfjoestatic int
115227934Sfjoexdialog_menu(const char *title, const char *cprompt, int height, int width,
116227934Sfjoe	     int menu_height, int item_no, dialogMenuItem *ditems)
117227934Sfjoe{
118227947Sfjoe	int i, result, choice = 0;
119227934Sfjoe	DIALOG_LISTITEM *listitems;
120227934Sfjoe	DIALOG_VARS save_vars;
121227934Sfjoe
122227934Sfjoe	dlg_save_vars(&save_vars);
123227934Sfjoe
124227934Sfjoe	/* initialize list items */
125227947Sfjoe	listitems = dlg_calloc(DIALOG_LISTITEM, item_no + 1);
126227934Sfjoe	assert_ptr(listitems, "xdialog_menu");
127227934Sfjoe	for (i = 0; i < item_no; i++) {
128227934Sfjoe		listitems[i].name = ditems[i].prompt;
129227934Sfjoe		listitems[i].text = ditems[i].title;
130227934Sfjoe	}
131227934Sfjoe
132228176Sfjoe	/* calculate height */
133228176Sfjoe	if (height < 0)
134228176Sfjoe		height = xdialog_count_rows(cprompt) + menu_height + 4 + 2;
135228176Sfjoe	if (height > LINES)
136228176Sfjoe		height = LINES;
137228176Sfjoe
138227934Sfjoe	/* calculate width */
139227934Sfjoe	if (width < 0) {
140227934Sfjoe		int tag_x = 0;
141227934Sfjoe
142227934Sfjoe		for (i = 0; i < item_no; i++) {
143227934Sfjoe			int j, l;
144227934Sfjoe
145227934Sfjoe			l = strlen(listitems[i].name);
146227934Sfjoe			for (j = 0; j < item_no; j++) {
147227934Sfjoe				int k = strlen(listitems[j].text);
148227934Sfjoe				tag_x = MAX(tag_x, l + k + 2);
149227934Sfjoe			}
150227934Sfjoe		}
151228176Sfjoe		width = MAX(xdialog_count_columns(cprompt), title != NULL ? xdialog_count_columns(title) : 0);
152227934Sfjoe		width = MAX(width, tag_x + 4) + 4;
153227934Sfjoe	}
154227934Sfjoe	width = MAX(width, 24);
155227934Sfjoe	if (width > COLS)
156227934Sfjoe		width = COLS;
157227934Sfjoe
158227934Sfjoeagain:
159227947Sfjoe	dialog_vars.default_item = listitems[choice].name;
160227934Sfjoe	result = dlg_menu(title, cprompt, height, width,
161227934Sfjoe	    menu_height, item_no, listitems, &choice, NULL);
162227934Sfjoe	switch (result) {
163227934Sfjoe	case DLG_EXIT_ESC:
164227934Sfjoe		result = -1;
165227934Sfjoe		break;
166227934Sfjoe	case DLG_EXIT_OK:
167227934Sfjoe		if (ditems[choice].fire != NULL) {
168227934Sfjoe			int status;
169227934Sfjoe
170227934Sfjoe			status = ditems[choice].fire(ditems + choice);
171227934Sfjoe			if (status & DITEM_RECREATE) {
172227934Sfjoe				dlg_clear();
173227934Sfjoe				goto again;
174227934Sfjoe			}
175227934Sfjoe		}
176227934Sfjoe		result = 0;
177227934Sfjoe		break;
178227934Sfjoe	case DLG_EXIT_CANCEL:
179227934Sfjoe	default:
180227934Sfjoe		result = 1;
181227934Sfjoe		break;
182227934Sfjoe	}
183227934Sfjoe
184227934Sfjoe	free(listitems);
185227934Sfjoe	dlg_restore_vars(&save_vars);
186227934Sfjoe	return result;
187227934Sfjoe}
188227934Sfjoe
189198350Sedwinstatic char	path_zonetab[MAXPATHLEN], path_iso3166[MAXPATHLEN],
190227934Sfjoe		path_zoneinfo[MAXPATHLEN], path_localtime[MAXPATHLEN],
191198350Sedwin		path_db[MAXPATHLEN], path_wall_cmos_clock[MAXPATHLEN];
192198350Sedwin
19319872Swollmanstatic int reallydoit = 1;
194198267Sedwinstatic int reinstall = 0;
195198350Sedwinstatic int usedialog = 1;
196198350Sedwinstatic char *chrootenv = NULL;
19719872Swollman
198179530Sjkimstatic void	usage(void);
199220172Sedwinstatic int	confirm_zone(const char *filename);
200179530Sjkimstatic int	continent_country_menu(dialogMenuItem *);
201220172Sedwinstatic int	install_zoneinfo_file(const char *zoneinfo_file);
202179530Sjkimstatic int	set_zone_multi(dialogMenuItem *);
203179530Sjkimstatic int	set_zone_whole_country(dialogMenuItem *);
204179530Sjkimstatic int	set_zone_menu(dialogMenuItem *);
205220172Sedwinstatic int	set_zone_utc(void);
20619872Swollman
20719872Swollmanstruct continent {
20819872Swollman	dialogMenuItem *menu;
209179530Sjkim	int		nitems;
21019872Swollman};
21119872Swollman
212179530Sjkimstatic struct continent	africa, america, antarctica, arctic, asia, atlantic;
213220172Sedwinstatic struct continent	australia, europe, indian, pacific, utc;
21419872Swollman
21519872Swollmanstatic struct continent_names {
216179530Sjkim	const char	*name;
21719872Swollman	struct continent *continent;
21819872Swollman} continent_names[] = {
219179530Sjkim	{ "Africa",	&africa },
220179530Sjkim	{ "America",	&america },
221179530Sjkim	{ "Antarctica",	&antarctica },
222179530Sjkim	{ "Arctic",	&arctic },
223179530Sjkim	{ "Asia",	&asia },
224179530Sjkim	{ "Atlantic",	&atlantic },
225179530Sjkim	{ "Australia",	&australia },
226179530Sjkim	{ "Europe",	&europe },
227179530Sjkim	{ "Indian",	&indian },
228220172Sedwin	{ "Pacific",	&pacific },
229227934Sfjoe	{ "UTC",	&utc }
23019872Swollman};
23119872Swollman
232179530Sjkimstatic struct continent_items {
233179530Sjkim	char		prompt[2];
234179530Sjkim	char		title[30];
235179530Sjkim} continent_items[] = {
236179530Sjkim	{ "1",	"Africa" },
237179530Sjkim	{ "2",	"America -- North and South" },
238179530Sjkim	{ "3",	"Antarctica" },
239179530Sjkim	{ "4",	"Arctic Ocean" },
240179530Sjkim	{ "5",	"Asia" },
241179530Sjkim	{ "6",	"Atlantic Ocean" },
242179530Sjkim	{ "7",	"Australia" },
243179530Sjkim	{ "8",	"Europe" },
244179530Sjkim	{ "9",	"Indian Ocean" },
245220172Sedwin	{ "0",	"Pacific Ocean" },
246220172Sedwin	{ "a",	"UTC" }
24719872Swollman};
24819872Swollman
249179530Sjkim#define	NCONTINENTS	\
250179530Sjkim    (int)((sizeof(continent_items)) / (sizeof(continent_items[0])))
251179530Sjkimstatic dialogMenuItem continents[NCONTINENTS];
252179530Sjkim
253179530Sjkim#define	OCEANP(x)	((x) == 3 || (x) == 5 || (x) == 8 || (x) == 9)
254179530Sjkim
25519872Swollmanstatic int
25619872Swollmancontinent_country_menu(dialogMenuItem *continent)
25719872Swollman{
258179530Sjkim	char		title[64], prompt[64];
25919872Swollman	struct continent *contp = continent->data;
260179530Sjkim	int		isocean = OCEANP(continent - continents);
261179530Sjkim	int		menulen;
262179530Sjkim	int		rv;
26319872Swollman
264220172Sedwin	if (strcmp(continent->title, "UTC") == 0)
265227934Sfjoe	        return set_zone_utc();
266220172Sedwin
26719872Swollman	/* Short cut -- if there's only one country, don't post a menu. */
268179497Sjkim	if (contp->nitems == 1)
269179497Sjkim		return (contp->menu[0].fire(&contp->menu[0]));
27019872Swollman
27119872Swollman	/* It's amazing how much good grammar really matters... */
272179530Sjkim	if (!isocean) {
273179530Sjkim		snprintf(title, sizeof(title), "Countries in %s",
274179530Sjkim		    continent->title);
275179530Sjkim		snprintf(prompt, sizeof(prompt), "Select a country or region");
276179530Sjkim	} else {
277179530Sjkim		snprintf(title, sizeof(title), "Islands and groups in the %s",
278179530Sjkim		    continent->title);
279179530Sjkim		snprintf(prompt, sizeof(prompt), "Select an island or group");
280179530Sjkim	}
28119872Swollman
28219872Swollman	menulen = contp->nitems < 16 ? contp->nitems : 16;
283227934Sfjoe	rv = xdialog_menu(title, prompt, -1, -1, menulen, contp->nitems,
284227934Sfjoe	    contp->menu);
28519872Swollman	if (rv == 0)
286179530Sjkim		return (DITEM_LEAVE_MENU);
287179530Sjkim	return (DITEM_RECREATE);
28819872Swollman}
28919872Swollman
29019872Swollmanstatic struct continent *
29119872Swollmanfind_continent(const char *name)
29219872Swollman{
293179530Sjkim	int		i;
29419872Swollman
295179530Sjkim	for (i = 0; i < NCONTINENTS; i++)
29619872Swollman		if (strcmp(name, continent_names[i].name) == 0)
297179530Sjkim			return (continent_names[i].continent);
298179530Sjkim	return (0);
29919872Swollman}
30019872Swollman
30119872Swollmanstruct country {
302179530Sjkim	char		*name;
303179530Sjkim	char		*tlc;
304179530Sjkim	int		nzones;
305179530Sjkim	char		*filename;	/* use iff nzones < 0 */
306179530Sjkim	struct continent *continent;	/* use iff nzones < 0 */
307179530Sjkim	TAILQ_HEAD(, zone) zones;	/* use iff nzones > 0 */
308179530Sjkim	dialogMenuItem	*submenu;	/* use iff nzones > 0 */
30919872Swollman};
31019872Swollman
31119872Swollmanstruct zone {
31260938Sjake	TAILQ_ENTRY(zone) link;
313179530Sjkim	char		*descr;
314179530Sjkim	char		*filename;
31519872Swollman	struct continent *continent;
31619872Swollman};
31719872Swollman
31819872Swollman/*
31919872Swollman * This is the easiest organization... we use ISO 3166 country codes,
32019872Swollman * of the two-letter variety, so we just size this array to suit.
32119872Swollman * Beats worrying about dynamic allocation.
32219872Swollman */
323179530Sjkim#define	NCOUNTRIES	(26 * 26)
32422181Sjhaystatic struct country countries[NCOUNTRIES];
32519872Swollman
326179530Sjkim#define	CODE2INT(s)	((s[0] - 'A') * 26 + (s[1] - 'A'))
327179530Sjkim
32819872Swollman/*
32919872Swollman * Read the ISO 3166 country code database in _PATH_ISO3166
33019872Swollman * (/usr/share/misc/iso3166).  On error, exit via err(3).
33119872Swollman */
33219872Swollmanstatic void
33319872Swollmanread_iso3166_table(void)
33419872Swollman{
335179530Sjkim	FILE		*fp;
336179530Sjkim	struct country	*cp;
337179530Sjkim	size_t		len;
338179530Sjkim	char		*s, *t, *name;
339179530Sjkim	int		lineno;
34019872Swollman
341198350Sedwin	fp = fopen(path_iso3166, "r");
34219872Swollman	if (!fp)
343209190Semaste		err(1, "%s", path_iso3166);
34419872Swollman	lineno = 0;
34519872Swollman
34619872Swollman	while ((s = fgetln(fp, &len)) != 0) {
34719872Swollman		lineno++;
34819872Swollman		if (s[len - 1] != '\n')
349198350Sedwin			errx(1, "%s:%d: invalid format", path_iso3166, lineno);
35019872Swollman		s[len - 1] = '\0';
35130999Sjoerg		if (s[0] == '#' || strspn(s, " \t") == len - 1)
35219872Swollman			continue;
35319872Swollman
35419872Swollman		/* Isolate the two-letter code. */
35519872Swollman		t = strsep(&s, "\t");
35619872Swollman		if (t == 0 || strlen(t) != 2)
357198350Sedwin			errx(1, "%s:%d: invalid format", path_iso3166, lineno);
35819872Swollman		if (t[0] < 'A' || t[0] > 'Z' || t[1] < 'A' || t[1] > 'Z')
359198350Sedwin			errx(1, "%s:%d: invalid code `%s'", path_iso3166,
360179530Sjkim			    lineno, t);
36119872Swollman
36219872Swollman		/* Now skip past the three-letter and numeric codes. */
363179530Sjkim		name = strsep(&s, "\t");	/* 3-let */
36419872Swollman		if (name == 0 || strlen(name) != 3)
365198350Sedwin			errx(1, "%s:%d: invalid format", path_iso3166, lineno);
366179530Sjkim		name = strsep(&s, "\t");	/* numeric */
36719872Swollman		if (name == 0 || strlen(name) != 3)
368198350Sedwin			errx(1, "%s:%d: invalid format", path_iso3166, lineno);
36919872Swollman
37019872Swollman		name = s;
37119872Swollman
37219872Swollman		cp = &countries[CODE2INT(t)];
37319872Swollman		if (cp->name)
374198350Sedwin			errx(1, "%s:%d: country code `%s' multiply defined: %s",
375198350Sedwin			    path_iso3166, lineno, t, cp->name);
37619872Swollman		cp->name = strdup(name);
37756487Scharnier		if (cp->name == NULL)
37856487Scharnier			errx(1, "malloc failed");
37919872Swollman		cp->tlc = strdup(t);
38056487Scharnier		if (cp->tlc == NULL)
38156487Scharnier			errx(1, "malloc failed");
38219872Swollman	}
38319872Swollman
38419872Swollman	fclose(fp);
38519872Swollman}
38619872Swollman
38719872Swollmanstatic void
38819872Swollmanadd_zone_to_country(int lineno, const char *tlc, const char *descr,
389179530Sjkim    const char *file, struct continent *cont)
39019872Swollman{
391179530Sjkim	struct zone	*zp;
392179530Sjkim	struct country	*cp;
39319872Swollman
39419872Swollman	if (tlc[0] < 'A' || tlc[0] > 'Z' || tlc[1] < 'A' || tlc[1] > 'Z')
395198350Sedwin		errx(1, "%s:%d: country code `%s' invalid", path_zonetab,
396179530Sjkim		    lineno, tlc);
397227934Sfjoe
39819872Swollman	cp = &countries[CODE2INT(tlc)];
39919872Swollman	if (cp->name == 0)
400198350Sedwin		errx(1, "%s:%d: country code `%s' unknown", path_zonetab,
401179530Sjkim		    lineno, tlc);
40219872Swollman
40319872Swollman	if (descr) {
40419872Swollman		if (cp->nzones < 0)
405198350Sedwin			errx(1, "%s:%d: conflicting zone definition",
406198350Sedwin			    path_zonetab, lineno);
40719872Swollman
408179530Sjkim		zp = malloc(sizeof(*zp));
40919872Swollman		if (zp == 0)
410179530Sjkim			errx(1, "malloc(%zu)", sizeof(*zp));
411227934Sfjoe
41219872Swollman		if (cp->nzones == 0)
41319872Swollman			TAILQ_INIT(&cp->zones);
41419872Swollman
41519872Swollman		zp->descr = strdup(descr);
41656487Scharnier		if (zp->descr == NULL)
41756487Scharnier			errx(1, "malloc failed");
41819872Swollman		zp->filename = strdup(file);
41956487Scharnier		if (zp->filename == NULL)
42056487Scharnier			errx(1, "malloc failed");
42119872Swollman		zp->continent = cont;
42219872Swollman		TAILQ_INSERT_TAIL(&cp->zones, zp, link);
42319872Swollman		cp->nzones++;
42419872Swollman	} else {
42519872Swollman		if (cp->nzones > 0)
426198350Sedwin			errx(1, "%s:%d: zone must have description",
427198350Sedwin			    path_zonetab, lineno);
42819872Swollman		if (cp->nzones < 0)
429198350Sedwin			errx(1, "%s:%d: zone multiply defined",
430198350Sedwin			    path_zonetab, lineno);
43119872Swollman		cp->nzones = -1;
43219872Swollman		cp->filename = strdup(file);
43356487Scharnier		if (cp->filename == NULL)
43456487Scharnier			errx(1, "malloc failed");
43519872Swollman		cp->continent = cont;
43619872Swollman	}
43719872Swollman}
43819872Swollman
43919872Swollman/*
44019872Swollman * This comparison function intentionally sorts all of the null-named
44119872Swollman * ``countries''---i.e., the codes that don't correspond to a real
44219872Swollman * country---to the end.  Everything else is lexical by country name.
44319872Swollman */
44419872Swollmanstatic int
44519872Swollmancompare_countries(const void *xa, const void *xb)
44619872Swollman{
44719872Swollman	const struct country *a = xa, *b = xb;
44819872Swollman
44919872Swollman	if (a->name == 0 && b->name == 0)
450179530Sjkim		return (0);
45119872Swollman	if (a->name == 0 && b->name != 0)
452179530Sjkim		return (1);
45319872Swollman	if (b->name == 0)
454179530Sjkim		return (-1);
45519872Swollman
456179530Sjkim	return (strcmp(a->name, b->name));
45719872Swollman}
45819872Swollman
45919872Swollman/*
46019872Swollman * This must be done AFTER all zone descriptions are read, since it breaks
46119872Swollman * CODE2INT().
46219872Swollman */
46319872Swollmanstatic void
46419872Swollmansort_countries(void)
46519872Swollman{
466179530Sjkim
467179530Sjkim	qsort(countries, NCOUNTRIES, sizeof(countries[0]), compare_countries);
46819872Swollman}
46919872Swollman
47019872Swollmanstatic void
47119872Swollmanread_zones(void)
47219872Swollman{
473179530Sjkim	char		contbuf[16];
474179530Sjkim	FILE		*fp;
47519872Swollman	struct continent *cont;
476179530Sjkim	size_t		len;
477179530Sjkim	char		*line, *tlc, *coord, *file, *descr, *p;
478179530Sjkim	int		lineno;
47919872Swollman
480198350Sedwin	fp = fopen(path_zonetab, "r");
48119872Swollman	if (!fp)
482209190Semaste		err(1, "%s", path_zonetab);
48319872Swollman	lineno = 0;
48419872Swollman
48519872Swollman	while ((line = fgetln(fp, &len)) != 0) {
48619872Swollman		lineno++;
48719872Swollman		if (line[len - 1] != '\n')
488198350Sedwin			errx(1, "%s:%d: invalid format", path_zonetab, lineno);
48919872Swollman		line[len - 1] = '\0';
49019872Swollman		if (line[0] == '#')
49119872Swollman			continue;
49219872Swollman
49319872Swollman		tlc = strsep(&line, "\t");
49419872Swollman		if (strlen(tlc) != 2)
495198350Sedwin			errx(1, "%s:%d: invalid country code `%s'",
496198350Sedwin			    path_zonetab, lineno, tlc);
497208831Sedwin		coord = strsep(&line, "\t");	 /* Unused */
49819872Swollman		file = strsep(&line, "\t");
49919872Swollman		p = strchr(file, '/');
50019872Swollman		if (p == 0)
501198350Sedwin			errx(1, "%s:%d: invalid zone name `%s'", path_zonetab,
502179530Sjkim			    lineno, file);
50319872Swollman		contbuf[0] = '\0';
50419872Swollman		strncat(contbuf, file, p - file);
50519872Swollman		cont = find_continent(contbuf);
50619872Swollman		if (!cont)
507198350Sedwin			errx(1, "%s:%d: invalid region `%s'", path_zonetab,
508179530Sjkim			    lineno, contbuf);
50919872Swollman
510179530Sjkim		descr = (line != NULL && *line != '\0') ? line : NULL;
51119872Swollman
51219872Swollman		add_zone_to_country(lineno, tlc, descr, file, cont);
51319872Swollman	}
51419872Swollman	fclose(fp);
51519872Swollman}
51619872Swollman
51719872Swollmanstatic void
51819872Swollmanmake_menus(void)
51919872Swollman{
520179530Sjkim	struct country	*cp;
521179530Sjkim	struct zone	*zp, *zp2;
52219872Swollman	struct continent *cont;
523179530Sjkim	dialogMenuItem	*dmi;
524179530Sjkim	int		i;
52519872Swollman
52619872Swollman	/*
52719872Swollman	 * First, count up all the countries in each continent/ocean.
52819872Swollman	 * Be careful to count those countries which have multiple zones
52919872Swollman	 * only once for each.  NB: some countries are in multiple
53019872Swollman	 * continents/oceans.
53119872Swollman	 */
53219872Swollman	for (cp = countries; cp->name; cp++) {
53319872Swollman		if (cp->nzones == 0)
53419872Swollman			continue;
53519872Swollman		if (cp->nzones < 0) {
53619872Swollman			cp->continent->nitems++;
53719872Swollman		} else {
53870486Sben			TAILQ_FOREACH(zp, &cp->zones, link) {
53919872Swollman				cont = zp->continent;
54070486Sben				for (zp2 = TAILQ_FIRST(&cp->zones);
541179530Sjkim				    zp2->continent != cont;
542179530Sjkim				    zp2 = TAILQ_NEXT(zp2, link))
54319872Swollman					;
54419872Swollman				if (zp2 == zp)
54519872Swollman					zp->continent->nitems++;
54619872Swollman			}
54719872Swollman		}
54819872Swollman	}
54919872Swollman
55019872Swollman	/*
551179530Sjkim	 * Now allocate memory for the country menus and initialize
552179530Sjkim	 * continent menus.  We set nitems back to zero so that we can
553179530Sjkim	 * use it for counting again when we actually build the menus.
55419872Swollman	 */
555179530Sjkim	memset(continents, 0, sizeof(continents));
55619872Swollman	for (i = 0; i < NCONTINENTS; i++) {
55719872Swollman		continent_names[i].continent->menu =
558179530Sjkim		    malloc(sizeof(dialogMenuItem) *
559179530Sjkim		    continent_names[i].continent->nitems);
56019872Swollman		if (continent_names[i].continent->menu == 0)
56156487Scharnier			errx(1, "malloc for continent menu");
56219872Swollman		continent_names[i].continent->nitems = 0;
563179530Sjkim		continents[i].prompt = continent_items[i].prompt;
564179530Sjkim		continents[i].title = continent_items[i].title;
565179530Sjkim		continents[i].fire = continent_country_menu;
566179530Sjkim		continents[i].data = continent_names[i].continent;
56719872Swollman	}
56819872Swollman
56919872Swollman	/*
57019872Swollman	 * Now that memory is allocated, create the menu items for
57119872Swollman	 * each continent.  For multiple-zone countries, also create
57219872Swollman	 * the country's zone submenu.
57319872Swollman	 */
57419872Swollman	for (cp = countries; cp->name; cp++) {
57519872Swollman		if (cp->nzones == 0)
57619872Swollman			continue;
57719872Swollman		if (cp->nzones < 0) {
57819872Swollman			dmi = &cp->continent->menu[cp->continent->nitems];
579179530Sjkim			memset(dmi, 0, sizeof(*dmi));
580179530Sjkim			asprintf(&dmi->prompt, "%d", ++cp->continent->nitems);
58119872Swollman			dmi->title = cp->name;
58219872Swollman			dmi->fire = set_zone_whole_country;
58319872Swollman			dmi->data = cp;
58419872Swollman		} else {
585179530Sjkim			cp->submenu = malloc(cp->nzones * sizeof(*dmi));
58619872Swollman			if (cp->submenu == 0)
58756487Scharnier				errx(1, "malloc for submenu");
58819872Swollman			cp->nzones = 0;
58970486Sben			TAILQ_FOREACH(zp, &cp->zones, link) {
59019872Swollman				cont = zp->continent;
59119872Swollman				dmi = &cp->submenu[cp->nzones];
592179530Sjkim				memset(dmi, 0, sizeof(*dmi));
593179530Sjkim				asprintf(&dmi->prompt, "%d", ++cp->nzones);
59419872Swollman				dmi->title = zp->descr;
59519872Swollman				dmi->fire = set_zone_multi;
59619872Swollman				dmi->data = zp;
59719872Swollman
59870486Sben				for (zp2 = TAILQ_FIRST(&cp->zones);
599179530Sjkim				    zp2->continent != cont;
600179530Sjkim				    zp2 = TAILQ_NEXT(zp2, link))
60119872Swollman					;
60219872Swollman				if (zp2 != zp)
60319872Swollman					continue;
60419872Swollman
60519872Swollman				dmi = &cont->menu[cont->nitems];
606179530Sjkim				memset(dmi, 0, sizeof(*dmi));
60719872Swollman				asprintf(&dmi->prompt, "%d", ++cont->nitems);
60819872Swollman				dmi->title = cp->name;
60919872Swollman				dmi->fire = set_zone_menu;
61019872Swollman				dmi->data = cp;
61119872Swollman			}
61219872Swollman		}
61319872Swollman	}
61419872Swollman}
61519872Swollman
61619872Swollmanstatic int
61719872Swollmanset_zone_menu(dialogMenuItem *dmi)
61819872Swollman{
619179530Sjkim	char		title[64], prompt[64];
620179530Sjkim	struct country	*cp = dmi->data;
621179530Sjkim	int		menulen;
622179530Sjkim	int		rv;
62319872Swollman
624179530Sjkim	snprintf(title, sizeof(title), "%s Time Zones", cp->name);
625179530Sjkim	snprintf(prompt, sizeof(prompt),
626179530Sjkim	    "Select a zone which observes the same time as your locality.");
62719872Swollman	menulen = cp->nzones < 16 ? cp->nzones : 16;
628227934Sfjoe	rv = xdialog_menu(title, prompt, -1, -1, menulen, cp->nzones,
629227934Sfjoe	    cp->submenu);
63019872Swollman	if (rv != 0)
631179530Sjkim		return (DITEM_RECREATE);
632179530Sjkim	return (DITEM_LEAVE_MENU);
63319872Swollman}
63419872Swollman
635220172Sedwinint
636220172Sedwinset_zone_utc(void)
637220172Sedwin{
638220172Sedwin	if (!confirm_zone(NULL))
639220172Sedwin		return (DITEM_FAILURE | DITEM_RECREATE);
640227934Sfjoe
641220172Sedwin	return (install_zoneinfo_file(NULL));
642220172Sedwin}
643220172Sedwin
64419872Swollmanstatic int
645198350Sedwininstall_zoneinfo_file(const char *zoneinfo_file)
64619872Swollman{
647179530Sjkim	char		buf[1024];
648230005Swollman	char		title[64], prompt[SILLY_BUFFER_SIZE];
649179530Sjkim	struct stat	sb;
650179530Sjkim	ssize_t		len;
651179530Sjkim	int		fd1, fd2, copymode;
65219872Swollman
653198350Sedwin	if (lstat(path_localtime, &sb) < 0) {
65419872Swollman		/* Nothing there yet... */
65519872Swollman		copymode = 1;
656179530Sjkim	} else if (S_ISLNK(sb.st_mode))
65719872Swollman		copymode = 0;
65819872Swollman	else
65919872Swollman		copymode = 1;
66019872Swollman
66121915Sjkh#ifdef VERBOSE
662230299Semaste	snprintf(title, sizeof(title), "Info");
663230299Semaste	if (zoneinfo_file == NULL)
664179530Sjkim		snprintf(prompt, sizeof(prompt),
665230299Semaste		    "Removing %s", path_localtime);
666230299Semaste	else if (copymode)
667230299Semaste		snprintf(prompt, sizeof(prompt),
668198350Sedwin		    "Copying %s to %s", zoneinfo_file, path_localtime);
66919872Swollman	else
670179530Sjkim		snprintf(prompt, sizeof(prompt),
671198350Sedwin		    "Creating symbolic link %s to %s",
672230299Semaste		    path_localtime, zoneinfo_file);
673198267Sedwin	if (usedialog)
674230299Semaste		dialog_msgbox(title, prompt, 8, 72, 1);
675198267Sedwin	else
676198267Sedwin		fprintf(stderr, "%s\n", prompt);
67721915Sjkh#endif
67819872Swollman
67919872Swollman	if (reallydoit) {
680220172Sedwin		if (zoneinfo_file == NULL) {
681220172Sedwin			if (unlink(path_localtime) < 0 && errno != ENOENT) {
682220172Sedwin				snprintf(title, sizeof(title), "Error");
683220172Sedwin				snprintf(prompt, sizeof(prompt),
684220172Sedwin				     "Could not delete %s: %s", path_localtime,
685220172Sedwin				     strerror(errno));
686220172Sedwin				if (usedialog)
687227934Sfjoe					dialog_msgbox(title, prompt, 8, 72, 1);
688220172Sedwin				else
689220172Sedwin					fprintf(stderr, "%s\n", prompt);
690220172Sedwin
691220172Sedwin				return (DITEM_FAILURE | DITEM_RECREATE);
692220172Sedwin			}
693227011Sdougb			if (unlink(path_db) < 0 && errno != ENOENT) {
694227011Sdougb				snprintf(title, sizeof(title), "Error");
695227011Sdougb				snprintf(prompt, sizeof(prompt),
696227011Sdougb				     "Could not delete %s: %s", path_db,
697227011Sdougb				     strerror(errno));
698227011Sdougb				if (usedialog)
699227934Sfjoe					dialog_msgbox(title, prompt, 8, 72, 1);
700227011Sdougb				else
701227011Sdougb					fprintf(stderr, "%s\n", prompt);
702227011Sdougb
703227011Sdougb				return (DITEM_FAILURE | DITEM_RECREATE);
704227011Sdougb			}
705230299Semaste#ifdef VERBOSE
706247780Sdteske			snprintf(title, sizeof(title), "Done");
707230299Semaste			snprintf(prompt, sizeof(prompt),
708230299Semaste			    "Removed %s", path_localtime);
709247780Sdteske			if (usedialog)
710247780Sdteske				dialog_msgbox(title, prompt, 8, 72, 1);
711247780Sdteske			else
712247780Sdteske				fprintf(stderr, "%s\n", prompt);
713230299Semaste#endif
714220172Sedwin			return (DITEM_LEAVE_MENU);
715220172Sedwin		}
716227934Sfjoe
71719872Swollman		if (copymode) {
718198350Sedwin			fd1 = open(zoneinfo_file, O_RDONLY, 0);
71919872Swollman			if (fd1 < 0) {
720179530Sjkim				snprintf(title, sizeof(title), "Error");
721179530Sjkim				snprintf(prompt, sizeof(prompt),
722198350Sedwin				    "Could not open %s: %s", zoneinfo_file,
723179530Sjkim				    strerror(errno));
724198267Sedwin				if (usedialog)
725227934Sfjoe					dialog_msgbox(title, prompt, 8, 72, 1);
726198267Sedwin				else
727198267Sedwin					fprintf(stderr, "%s\n", prompt);
728179530Sjkim				return (DITEM_FAILURE | DITEM_RECREATE);
72919872Swollman			}
73019872Swollman
731231181Swollman			if (unlink(path_localtime) < 0 && errno != ENOENT) {
732230005Swollman				snprintf(prompt, sizeof(prompt),
733230005Swollman				    "Could not unlink %s: %s",
734230005Swollman				    path_localtime, strerror(errno));
735230005Swollman				if (usedialog) {
736230005Swollman					snprintf(title, sizeof(title), "Error");
737230005Swollman					dialog_msgbox(title, prompt, 8, 72, 1);
738230005Swollman				} else
739230005Swollman					fprintf(stderr, "%s\n", prompt);
740230005Swollman				return (DITEM_FAILURE | DITEM_RECREATE);
741230005Swollman			}
742230005Swollman
743198350Sedwin			fd2 = open(path_localtime, O_CREAT | O_EXCL | O_WRONLY,
744179530Sjkim			    S_IRUSR | S_IRGRP | S_IROTH);
74519872Swollman			if (fd2 < 0) {
746179530Sjkim				snprintf(title, sizeof(title), "Error");
747179530Sjkim				snprintf(prompt, sizeof(prompt),
748198350Sedwin				    "Could not open %s: %s",
749198350Sedwin				    path_localtime, strerror(errno));
750198267Sedwin				if (usedialog)
751227934Sfjoe					dialog_msgbox(title, prompt, 8, 72, 1);
752198267Sedwin				else
753198267Sedwin					fprintf(stderr, "%s\n", prompt);
754179530Sjkim				return (DITEM_FAILURE | DITEM_RECREATE);
75519872Swollman			}
75619872Swollman
757179530Sjkim			while ((len = read(fd1, buf, sizeof(buf))) > 0)
758208830Sedwin				if ((len = write(fd2, buf, len)) < 0)
759208830Sedwin					break;
76019872Swollman
76119872Swollman			if (len == -1) {
762179530Sjkim				snprintf(title, sizeof(title), "Error");
763179530Sjkim				snprintf(prompt, sizeof(prompt),
764198350Sedwin				    "Error copying %s to %s %s", zoneinfo_file,
765198350Sedwin				    path_localtime, strerror(errno));
766198267Sedwin				if (usedialog)
767227934Sfjoe					dialog_msgbox(title, prompt, 8, 72, 1);
768198267Sedwin				else
769198267Sedwin					fprintf(stderr, "%s\n", prompt);
77019872Swollman				/* Better to leave none than a corrupt one. */
771198350Sedwin				unlink(path_localtime);
772179530Sjkim				return (DITEM_FAILURE | DITEM_RECREATE);
77319872Swollman			}
77419872Swollman			close(fd1);
77519872Swollman			close(fd2);
77619872Swollman		} else {
777198350Sedwin			if (access(zoneinfo_file, R_OK) != 0) {
778179530Sjkim				snprintf(title, sizeof(title), "Error");
779179530Sjkim				snprintf(prompt, sizeof(prompt),
780198350Sedwin				    "Cannot access %s: %s", zoneinfo_file,
781179530Sjkim				    strerror(errno));
782198267Sedwin				if (usedialog)
783227934Sfjoe					dialog_msgbox(title, prompt, 8, 72, 1);
784198267Sedwin				else
785198267Sedwin					fprintf(stderr, "%s\n", prompt);
786179530Sjkim				return (DITEM_FAILURE | DITEM_RECREATE);
78719872Swollman			}
788231181Swollman			if (unlink(path_localtime) < 0 && errno != ENOENT) {
789230005Swollman				snprintf(prompt, sizeof(prompt),
790230005Swollman				    "Could not unlink %s: %s",
791230005Swollman				    path_localtime, strerror(errno));
792230005Swollman				if (usedialog) {
793230005Swollman					snprintf(title, sizeof(title), "Error");
794230005Swollman					dialog_msgbox(title, prompt, 8, 72, 1);
795230005Swollman				} else
796230005Swollman					fprintf(stderr, "%s\n", prompt);
797230005Swollman				return (DITEM_FAILURE | DITEM_RECREATE);
798230005Swollman			}
799198350Sedwin			if (symlink(zoneinfo_file, path_localtime) < 0) {
800179530Sjkim				snprintf(title, sizeof(title), "Error");
801179530Sjkim				snprintf(prompt, sizeof(prompt),
802198350Sedwin				    "Cannot create symbolic link %s to %s: %s",
803198350Sedwin				    path_localtime, zoneinfo_file,
804179530Sjkim				    strerror(errno));
805198267Sedwin				if (usedialog)
806227934Sfjoe					dialog_msgbox(title, prompt, 8, 72, 1);
807198267Sedwin				else
808198267Sedwin					fprintf(stderr, "%s\n", prompt);
809179530Sjkim				return (DITEM_FAILURE | DITEM_RECREATE);
81019872Swollman			}
81119872Swollman		}
81219872Swollman
81321915Sjkh#ifdef VERBOSE
814230299Semaste		snprintf(title, sizeof(title), "Done");
815230299Semaste		if (copymode)
816230299Semaste			snprintf(prompt, sizeof(prompt),
817230299Semaste			    "Copied timezone file from %s to %s",
818230299Semaste			    zoneinfo_file, path_localtime);
819230299Semaste		else
820230299Semaste			snprintf(prompt, sizeof(prompt),
821230299Semaste			    "Created symbolic link from %s to %s",
822230299Semaste			    zoneinfo_file, path_localtime);
823230299Semaste		if (usedialog)
824230299Semaste			dialog_msgbox(title, prompt, 8, 72, 1);
825230299Semaste		else
826230299Semaste			fprintf(stderr, "%s\n", prompt);
82721915Sjkh#endif
828230299Semaste	} /* reallydoit */
829198267Sedwin
830198350Sedwin	return (DITEM_LEAVE_MENU);
831198350Sedwin}
832198350Sedwin
833198350Sedwinstatic int
834198350Sedwininstall_zoneinfo(const char *zoneinfo)
835198350Sedwin{
836198350Sedwin	int		rv;
837198350Sedwin	FILE		*f;
838198350Sedwin	char		path_zoneinfo_file[MAXPATHLEN];
839198350Sedwin
840198350Sedwin	sprintf(path_zoneinfo_file, "%s/%s", path_zoneinfo, zoneinfo);
841198350Sedwin	rv = install_zoneinfo_file(path_zoneinfo_file);
842198350Sedwin
843198267Sedwin	/* Save knowledge for later */
844230296Semaste	if (reallydoit && (rv & DITEM_FAILURE) == 0) {
845230296Semaste		if ((f = fopen(path_db, "w")) != NULL) {
846230296Semaste			fprintf(f, "%s\n", zoneinfo);
847230296Semaste			fclose(f);
848230296Semaste		}
849198267Sedwin	}
850198267Sedwin
851198350Sedwin	return (rv);
85219872Swollman}
85319872Swollman
85419872Swollmanstatic int
85519872Swollmanconfirm_zone(const char *filename)
85619872Swollman{
857179530Sjkim	char		title[64], prompt[64];
858179530Sjkim	time_t		t = time(0);
859179530Sjkim	struct tm	*tm;
860179530Sjkim	int		rv;
861227934Sfjoe
862220172Sedwin	setenv("TZ", filename == NULL ? "" : filename, 1);
86319872Swollman	tzset();
86419872Swollman	tm = localtime(&t);
86519872Swollman
866179530Sjkim	snprintf(title, sizeof(title), "Confirmation");
867179530Sjkim	snprintf(prompt, sizeof(prompt),
868179530Sjkim	    "Does the abbreviation `%s' look reasonable?", tm->tm_zone);
869179530Sjkim	rv = !dialog_yesno(title, prompt, 5, 72);
870179530Sjkim	return (rv);
87119872Swollman}
87219872Swollman
87319872Swollmanstatic int
87419872Swollmanset_zone_multi(dialogMenuItem *dmi)
87519872Swollman{
876179530Sjkim	struct zone	*zp = dmi->data;
877179530Sjkim	int		rv;
87819872Swollman
87919872Swollman	if (!confirm_zone(zp->filename))
880179530Sjkim		return (DITEM_FAILURE | DITEM_RECREATE);
88119872Swollman
882198350Sedwin	rv = install_zoneinfo(zp->filename);
883179530Sjkim	return (rv);
88419872Swollman}
88519872Swollman
88619872Swollmanstatic int
88719872Swollmanset_zone_whole_country(dialogMenuItem *dmi)
88819872Swollman{
889179530Sjkim	struct country	*cp = dmi->data;
890179530Sjkim	int		rv;
89119872Swollman
89219872Swollman	if (!confirm_zone(cp->filename))
893179530Sjkim		return (DITEM_FAILURE | DITEM_RECREATE);
89419872Swollman
895198350Sedwin	rv = install_zoneinfo(cp->filename);
896179530Sjkim	return (rv);
89719872Swollman}
89819872Swollman
89930763Scharnierstatic void
900179530Sjkimusage(void)
90130763Scharnier{
902179530Sjkim
903222139Sru	fprintf(stderr, "usage: tzsetup [-nrs] [-C chroot_directory]"
904222139Sru	    " [zoneinfo_file | zoneinfo_name]\n");
90530763Scharnier	exit(1);
90630763Scharnier}
90730763Scharnier
90819872Swollmanint
90919872Swollmanmain(int argc, char **argv)
91019872Swollman{
911179530Sjkim	char		title[64], prompt[128];
912198267Sedwin	int		c, fd, rv, skiputc;
91319872Swollman
914195339Sattilio	skiputc = 0;
915198350Sedwin	while ((c = getopt(argc, argv, "C:nrs")) != -1) {
91619872Swollman		switch(c) {
917198350Sedwin		case 'C':
918198350Sedwin			chrootenv = optarg;
919198350Sedwin			break;
92019872Swollman		case 'n':
92119872Swollman			reallydoit = 0;
92219872Swollman			break;
923198267Sedwin		case 'r':
924198267Sedwin			reinstall = 1;
925198350Sedwin			usedialog = 0;
926198267Sedwin			break;
927195339Sattilio		case 's':
928195339Sattilio			skiputc = 1;
929195339Sattilio			break;
93019872Swollman		default:
93130763Scharnier			usage();
93219872Swollman		}
93319872Swollman	}
93419872Swollman
93543544Swollman	if (argc - optind > 1)
93630763Scharnier		usage();
93719872Swollman
938198350Sedwin	if (chrootenv == NULL) {
939198350Sedwin		strcpy(path_zonetab, _PATH_ZONETAB);
940198350Sedwin		strcpy(path_iso3166, _PATH_ISO3166);
941198350Sedwin		strcpy(path_zoneinfo, _PATH_ZONEINFO);
942198350Sedwin		strcpy(path_localtime, _PATH_LOCALTIME);
943198350Sedwin		strcpy(path_db, _PATH_DB);
944198350Sedwin		strcpy(path_wall_cmos_clock, _PATH_WALL_CMOS_CLOCK);
945198350Sedwin	} else {
946198350Sedwin		sprintf(path_zonetab, "%s/%s", chrootenv, _PATH_ZONETAB);
947198350Sedwin		sprintf(path_iso3166, "%s/%s", chrootenv, _PATH_ISO3166);
948198350Sedwin		sprintf(path_zoneinfo, "%s/%s", chrootenv, _PATH_ZONEINFO);
949198350Sedwin		sprintf(path_localtime, "%s/%s", chrootenv, _PATH_LOCALTIME);
950198350Sedwin		sprintf(path_db, "%s/%s", chrootenv, _PATH_DB);
951198350Sedwin		sprintf(path_wall_cmos_clock, "%s/%s", chrootenv,
952198350Sedwin		    _PATH_WALL_CMOS_CLOCK);
953198350Sedwin	}
954198350Sedwin
955198350Sedwin
95649435Sru	/* Override the user-supplied umask. */
957179530Sjkim	(void)umask(S_IWGRP | S_IWOTH);
95849435Sru
959198267Sedwin	if (reinstall == 1) {
960198267Sedwin		FILE *f;
961230520Semaste		char zoneinfo[MAXPATHLEN];
962198267Sedwin
963198350Sedwin		if ((f = fopen(path_db, "r")) != NULL) {
964230520Semaste			if (fgets(zoneinfo, sizeof(zoneinfo), f) != NULL) {
965230520Semaste				zoneinfo[sizeof(zoneinfo) - 1] = 0;
966230520Semaste				if (strlen(zoneinfo) > 0) {
967230520Semaste					zoneinfo[strlen(zoneinfo) - 1] = 0;
968230520Semaste					rv = install_zoneinfo(zoneinfo);
969198267Sedwin					exit(rv & ~DITEM_LEAVE_MENU);
970198267Sedwin				}
971198350Sedwin				errx(1, "Error reading %s.\n", path_db);
972198267Sedwin			}
973198267Sedwin			fclose(f);
974198267Sedwin			errx(1,
975198267Sedwin			    "Unable to determine earlier installed zoneinfo "
976230520Semaste			    "name. Check %s", path_db);
977198267Sedwin		}
978198350Sedwin		errx(1, "Cannot open %s for reading. Does it exist?", path_db);
979198267Sedwin	}
980198267Sedwin
981198350Sedwin	/*
982198350Sedwin	 * If the arguments on the command-line do not specify a file,
983198350Sedwin	 * then interpret it as a zoneinfo name
984198350Sedwin	 */
985198350Sedwin	if (optind == argc - 1) {
986198350Sedwin		struct stat sb;
987198350Sedwin
988198350Sedwin		if (stat(argv[optind], &sb) != 0) {
989198350Sedwin			usedialog = 0;
990198350Sedwin			rv = install_zoneinfo(argv[optind]);
991198350Sedwin			exit(rv & ~DITEM_LEAVE_MENU);
992198350Sedwin		}
993198350Sedwin		/* FALLTHROUGH */
994198350Sedwin	}
995198350Sedwin
996230520Semaste	read_iso3166_table();
997230520Semaste	read_zones();
998230520Semaste	sort_countries();
999230520Semaste	make_menus();
1000230520Semaste
1001227934Sfjoe	init_dialog(stdin, stdout);
1002195339Sattilio	if (skiputc == 0) {
1003227934Sfjoe		DIALOG_VARS save_vars;
1004227934Sfjoe		int yesno;
1005227934Sfjoe
1006195339Sattilio		snprintf(title, sizeof(title),
1007195339Sattilio		    "Select local or UTC (Greenwich Mean Time) clock");
1008195339Sattilio		snprintf(prompt, sizeof(prompt),
1009195339Sattilio		    "Is this machine's CMOS clock set to UTC?  "
1010195339Sattilio		    "If it is set to local time,\n"
1011195339Sattilio		    "or you don't know, please choose NO here!");
1012227934Sfjoe		dlg_save_vars(&save_vars);
1013227934Sfjoe#if !defined(__sparc64__)
1014227934Sfjoe		dialog_vars.defaultno = TRUE;
1015227934Sfjoe#endif
1016227934Sfjoe		yesno = dialog_yesno(title, prompt, 7, 73);
1017227934Sfjoe		dlg_restore_vars(&save_vars);
1018227934Sfjoe		if (!yesno) {
1019195339Sattilio			if (reallydoit)
1020210243Snork				unlink(path_wall_cmos_clock);
1021195339Sattilio		} else {
1022195339Sattilio			if (reallydoit) {
1023210243Snork				fd = open(path_wall_cmos_clock,
1024195339Sattilio				    O_WRONLY | O_CREAT | O_TRUNC,
1025195339Sattilio				    S_IRUSR | S_IRGRP | S_IROTH);
1026198254Sedwin				if (fd < 0) {
1027198254Sedwin					end_dialog();
1028195339Sattilio					err(1, "create %s",
1029210243Snork					    path_wall_cmos_clock);
1030198254Sedwin				}
1031195339Sattilio				close(fd);
1032195339Sattilio			}
103341852Speter		}
1034227934Sfjoe		dlg_clear();
103532394Ssteve	}
103643544Swollman	if (optind == argc - 1) {
1037179530Sjkim		snprintf(title, sizeof(title), "Default timezone provided");
1038179530Sjkim		snprintf(prompt, sizeof(prompt),
1039179530Sjkim		    "\nUse the default `%s' zone?", argv[optind]);
1040179530Sjkim		if (!dialog_yesno(title, prompt, 7, 72)) {
1041198350Sedwin			rv = install_zoneinfo_file(argv[optind]);
1042227934Sfjoe			dlg_clear();
104343544Swollman			end_dialog();
1044198267Sedwin			exit(rv & ~DITEM_LEAVE_MENU);
104543544Swollman		}
1046227934Sfjoe		dlg_clear();
104743544Swollman	}
1048179530Sjkim	snprintf(title, sizeof(title), "Time Zone Selector");
1049179530Sjkim	snprintf(prompt, sizeof(prompt), "Select a region");
1050227934Sfjoe	xdialog_menu(title, prompt, -1, -1, NCONTINENTS, NCONTINENTS,
1051227934Sfjoe	    continents);
105243544Swollman
1053227934Sfjoe	dlg_clear();
105419872Swollman	end_dialog();
1055179530Sjkim	return (0);
105619872Swollman}
1057