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: stable/11/usr.sbin/tzsetup/tzsetup.c 364903 2020-08-28 08:14:19Z philip $");
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
52319192Sngie#ifdef HAVE_DIALOG
53227934Sfjoe#include <dialog.h>
54319192Sngie#endif
55227934Sfjoe
56364903Sphilip#define	_PATH_ZONETAB		"/usr/share/zoneinfo/zone1970.tab"
57179530Sjkim#define	_PATH_ISO3166		"/usr/share/misc/iso3166"
58179530Sjkim#define	_PATH_ZONEINFO		"/usr/share/zoneinfo"
59179530Sjkim#define	_PATH_LOCALTIME		"/etc/localtime"
60198267Sedwin#define	_PATH_DB		"/var/db/zoneinfo"
61179530Sjkim#define	_PATH_WALL_CMOS_CLOCK	"/etc/wall_cmos_clock"
6219872Swollman
63230005Swollman#ifdef PATH_MAX
64230005Swollman#define	SILLY_BUFFER_SIZE	2*PATH_MAX
65230005Swollman#else
66230005Swollman#warning "Somebody needs to fix this to dynamically size this buffer."
67230005Swollman#define	SILLY_BUFFER_SIZE	2048
68230005Swollman#endif
69230005Swollman
70227934Sfjoe/* special return codes for `fire' actions */
71227934Sfjoe#define DITEM_FAILURE           1
72227934Sfjoe
73227934Sfjoe/* flags - returned in upper 16 bits of return status */
74227934Sfjoe#define DITEM_LEAVE_MENU        (1 << 16)
75227934Sfjoe#define DITEM_RECREATE          (1 << 18)
76227934Sfjoe
77319192Sngiestatic char	path_zonetab[MAXPATHLEN], path_iso3166[MAXPATHLEN],
78319192Sngie		path_zoneinfo[MAXPATHLEN], path_localtime[MAXPATHLEN],
79319192Sngie		path_db[MAXPATHLEN], path_wall_cmos_clock[MAXPATHLEN];
80319192Sngie
81319192Sngiestatic int reallydoit = 1;
82319192Sngiestatic int reinstall = 0;
83319192Sngiestatic char *chrootenv = NULL;
84319192Sngie
85319192Sngiestatic void	usage(void);
86319192Sngiestatic int	install_zoneinfo(const char *zoneinfo);
87319192Sngiestatic int	install_zoneinfo_file(const char *zoneinfo_file);
88319192Sngie
89319192Sngie#ifdef HAVE_DIALOG
90227934Sfjoe/* for use in describing more exotic behaviors */
91227934Sfjoetypedef struct dialogMenuItem {
92227934Sfjoe	char *prompt;
93227934Sfjoe	char *title;
94227934Sfjoe	int (*fire)(struct dialogMenuItem *self);
95227934Sfjoe	void *data;
96227934Sfjoe} dialogMenuItem;
97227934Sfjoe
98227934Sfjoestatic int
99228176Sfjoexdialog_count_rows(const char *p)
100228176Sfjoe{
101228176Sfjoe	int rows = 0;
102228176Sfjoe
103228176Sfjoe	while ((p = strchr(p, '\n')) != NULL) {
104228176Sfjoe		p++;
105228176Sfjoe		if (*p == '\0')
106228176Sfjoe			break;
107228176Sfjoe		rows++;
108228176Sfjoe	}
109228176Sfjoe
110322375Smarius	return (rows ? rows : 1);
111228176Sfjoe}
112228176Sfjoe
113228176Sfjoestatic int
114228176Sfjoexdialog_count_columns(const char *p)
115228176Sfjoe{
116228176Sfjoe	int len;
117228176Sfjoe	int max_len = 0;
118228176Sfjoe	const char *q;
119228176Sfjoe
120228176Sfjoe	for (; (q = strchr(p, '\n')) != NULL; p = q + 1) {
121228176Sfjoe		len = q - p;
122228176Sfjoe		max_len = MAX(max_len, len);
123228176Sfjoe	}
124228176Sfjoe
125228176Sfjoe	len = strlen(p);
126228176Sfjoe	max_len = MAX(max_len, len);
127322375Smarius	return (max_len);
128228176Sfjoe}
129228176Sfjoe
130228176Sfjoestatic int
131227934Sfjoexdialog_menu(const char *title, const char *cprompt, int height, int width,
132227934Sfjoe	     int menu_height, int item_no, dialogMenuItem *ditems)
133227934Sfjoe{
134227947Sfjoe	int i, result, choice = 0;
135227934Sfjoe	DIALOG_LISTITEM *listitems;
136227934Sfjoe	DIALOG_VARS save_vars;
137227934Sfjoe
138227934Sfjoe	dlg_save_vars(&save_vars);
139227934Sfjoe
140227934Sfjoe	/* initialize list items */
141227947Sfjoe	listitems = dlg_calloc(DIALOG_LISTITEM, item_no + 1);
142227934Sfjoe	assert_ptr(listitems, "xdialog_menu");
143227934Sfjoe	for (i = 0; i < item_no; i++) {
144227934Sfjoe		listitems[i].name = ditems[i].prompt;
145227934Sfjoe		listitems[i].text = ditems[i].title;
146227934Sfjoe	}
147227934Sfjoe
148228176Sfjoe	/* calculate height */
149228176Sfjoe	if (height < 0)
150228176Sfjoe		height = xdialog_count_rows(cprompt) + menu_height + 4 + 2;
151228176Sfjoe	if (height > LINES)
152228176Sfjoe		height = LINES;
153228176Sfjoe
154227934Sfjoe	/* calculate width */
155227934Sfjoe	if (width < 0) {
156227934Sfjoe		int tag_x = 0;
157227934Sfjoe
158227934Sfjoe		for (i = 0; i < item_no; i++) {
159227934Sfjoe			int j, l;
160227934Sfjoe
161227934Sfjoe			l = strlen(listitems[i].name);
162227934Sfjoe			for (j = 0; j < item_no; j++) {
163227934Sfjoe				int k = strlen(listitems[j].text);
164227934Sfjoe				tag_x = MAX(tag_x, l + k + 2);
165227934Sfjoe			}
166227934Sfjoe		}
167322375Smarius		width = MAX(xdialog_count_columns(cprompt), title != NULL ?
168322375Smarius		    xdialog_count_columns(title) : 0);
169227934Sfjoe		width = MAX(width, tag_x + 4) + 4;
170227934Sfjoe	}
171227934Sfjoe	width = MAX(width, 24);
172227934Sfjoe	if (width > COLS)
173227934Sfjoe		width = COLS;
174227934Sfjoe
175227934Sfjoeagain:
176227947Sfjoe	dialog_vars.default_item = listitems[choice].name;
177227934Sfjoe	result = dlg_menu(title, cprompt, height, width,
178227934Sfjoe	    menu_height, item_no, listitems, &choice, NULL);
179227934Sfjoe	switch (result) {
180227934Sfjoe	case DLG_EXIT_ESC:
181227934Sfjoe		result = -1;
182227934Sfjoe		break;
183227934Sfjoe	case DLG_EXIT_OK:
184227934Sfjoe		if (ditems[choice].fire != NULL) {
185227934Sfjoe			int status;
186227934Sfjoe
187227934Sfjoe			status = ditems[choice].fire(ditems + choice);
188227934Sfjoe			if (status & DITEM_RECREATE) {
189227934Sfjoe				dlg_clear();
190227934Sfjoe				goto again;
191227934Sfjoe			}
192227934Sfjoe		}
193227934Sfjoe		result = 0;
194227934Sfjoe		break;
195227934Sfjoe	case DLG_EXIT_CANCEL:
196227934Sfjoe	default:
197227934Sfjoe		result = 1;
198227934Sfjoe		break;
199227934Sfjoe	}
200227934Sfjoe
201227934Sfjoe	free(listitems);
202227934Sfjoe	dlg_restore_vars(&save_vars);
203322375Smarius	return (result);
204227934Sfjoe}
205227934Sfjoe
206198350Sedwinstatic int usedialog = 1;
20719872Swollman
208220172Sedwinstatic int	confirm_zone(const char *filename);
209179530Sjkimstatic int	continent_country_menu(dialogMenuItem *);
210179530Sjkimstatic int	set_zone_multi(dialogMenuItem *);
211179530Sjkimstatic int	set_zone_whole_country(dialogMenuItem *);
212179530Sjkimstatic int	set_zone_menu(dialogMenuItem *);
213220172Sedwinstatic int	set_zone_utc(void);
21419872Swollman
21519872Swollmanstruct continent {
21619872Swollman	dialogMenuItem *menu;
217179530Sjkim	int		nitems;
21819872Swollman};
21919872Swollman
220364903Sphilipstatic struct continent	africa, america, antarctica, asia, atlantic;
221220172Sedwinstatic struct continent	australia, europe, indian, pacific, utc;
22219872Swollman
22319872Swollmanstatic struct continent_names {
224179530Sjkim	const char	*name;
22519872Swollman	struct continent *continent;
22619872Swollman} continent_names[] = {
227179530Sjkim	{ "Africa",	&africa },
228179530Sjkim	{ "America",	&america },
229179530Sjkim	{ "Antarctica",	&antarctica },
230179530Sjkim	{ "Asia",	&asia },
231179530Sjkim	{ "Atlantic",	&atlantic },
232179530Sjkim	{ "Australia",	&australia },
233179530Sjkim	{ "Europe",	&europe },
234179530Sjkim	{ "Indian",	&indian },
235220172Sedwin	{ "Pacific",	&pacific },
236227934Sfjoe	{ "UTC",	&utc }
23719872Swollman};
23819872Swollman
239179530Sjkimstatic struct continent_items {
240179530Sjkim	char		prompt[2];
241179530Sjkim	char		title[30];
242179530Sjkim} continent_items[] = {
243179530Sjkim	{ "1",	"Africa" },
244179530Sjkim	{ "2",	"America -- North and South" },
245179530Sjkim	{ "3",	"Antarctica" },
246364903Sphilip	{ "4",	"Asia" },
247364903Sphilip	{ "5",	"Atlantic Ocean" },
248364903Sphilip	{ "6",	"Australia" },
249364903Sphilip	{ "7",	"Europe" },
250364903Sphilip	{ "8",	"Indian Ocean" },
251364903Sphilip	{ "9",	"Pacific Ocean" },
252364903Sphilip	{ "0",	"UTC" }
25319872Swollman};
25419872Swollman
255179530Sjkim#define	NCONTINENTS	\
256179530Sjkim    (int)((sizeof(continent_items)) / (sizeof(continent_items[0])))
257179530Sjkimstatic dialogMenuItem continents[NCONTINENTS];
258179530Sjkim
259364903Sphilip#define	OCEANP(x)	((x) == 4 || (x) == 7 || (x) == 8)
260179530Sjkim
26119872Swollmanstatic int
26219872Swollmancontinent_country_menu(dialogMenuItem *continent)
26319872Swollman{
264179530Sjkim	char		title[64], prompt[64];
26519872Swollman	struct continent *contp = continent->data;
266179530Sjkim	int		isocean = OCEANP(continent - continents);
267179530Sjkim	int		menulen;
268179530Sjkim	int		rv;
26919872Swollman
270220172Sedwin	if (strcmp(continent->title, "UTC") == 0)
271322375Smarius		return (set_zone_utc());
272220172Sedwin
27319872Swollman	/* Short cut -- if there's only one country, don't post a menu. */
274179497Sjkim	if (contp->nitems == 1)
275179497Sjkim		return (contp->menu[0].fire(&contp->menu[0]));
27619872Swollman
27719872Swollman	/* It's amazing how much good grammar really matters... */
278179530Sjkim	if (!isocean) {
279179530Sjkim		snprintf(title, sizeof(title), "Countries in %s",
280179530Sjkim		    continent->title);
281179530Sjkim		snprintf(prompt, sizeof(prompt), "Select a country or region");
282179530Sjkim	} else {
283179530Sjkim		snprintf(title, sizeof(title), "Islands and groups in the %s",
284179530Sjkim		    continent->title);
285179530Sjkim		snprintf(prompt, sizeof(prompt), "Select an island or group");
286179530Sjkim	}
28719872Swollman
28819872Swollman	menulen = contp->nitems < 16 ? contp->nitems : 16;
289227934Sfjoe	rv = xdialog_menu(title, prompt, -1, -1, menulen, contp->nitems,
290227934Sfjoe	    contp->menu);
29119872Swollman	if (rv == 0)
292179530Sjkim		return (DITEM_LEAVE_MENU);
293179530Sjkim	return (DITEM_RECREATE);
29419872Swollman}
29519872Swollman
29619872Swollmanstatic struct continent *
29719872Swollmanfind_continent(const char *name)
29819872Swollman{
299179530Sjkim	int		i;
30019872Swollman
301179530Sjkim	for (i = 0; i < NCONTINENTS; i++)
30219872Swollman		if (strcmp(name, continent_names[i].name) == 0)
303179530Sjkim			return (continent_names[i].continent);
304179530Sjkim	return (0);
30519872Swollman}
30619872Swollman
30719872Swollmanstruct country {
308179530Sjkim	char		*name;
309179530Sjkim	char		*tlc;
310179530Sjkim	int		nzones;
311179530Sjkim	char		*filename;	/* use iff nzones < 0 */
312179530Sjkim	struct continent *continent;	/* use iff nzones < 0 */
313179530Sjkim	TAILQ_HEAD(, zone) zones;	/* use iff nzones > 0 */
314179530Sjkim	dialogMenuItem	*submenu;	/* use iff nzones > 0 */
31519872Swollman};
31619872Swollman
31719872Swollmanstruct zone {
31860938Sjake	TAILQ_ENTRY(zone) link;
319179530Sjkim	char		*descr;
320179530Sjkim	char		*filename;
32119872Swollman	struct continent *continent;
32219872Swollman};
32319872Swollman
32419872Swollman/*
32519872Swollman * This is the easiest organization... we use ISO 3166 country codes,
32619872Swollman * of the two-letter variety, so we just size this array to suit.
32719872Swollman * Beats worrying about dynamic allocation.
32819872Swollman */
329179530Sjkim#define	NCOUNTRIES	(26 * 26)
33022181Sjhaystatic struct country countries[NCOUNTRIES];
33119872Swollman
332179530Sjkim#define	CODE2INT(s)	((s[0] - 'A') * 26 + (s[1] - 'A'))
333179530Sjkim
33419872Swollman/*
33519872Swollman * Read the ISO 3166 country code database in _PATH_ISO3166
33619872Swollman * (/usr/share/misc/iso3166).  On error, exit via err(3).
33719872Swollman */
33819872Swollmanstatic void
33919872Swollmanread_iso3166_table(void)
34019872Swollman{
341179530Sjkim	FILE		*fp;
342179530Sjkim	struct country	*cp;
343179530Sjkim	size_t		len;
344179530Sjkim	char		*s, *t, *name;
345179530Sjkim	int		lineno;
34619872Swollman
347198350Sedwin	fp = fopen(path_iso3166, "r");
34819872Swollman	if (!fp)
349209190Semaste		err(1, "%s", path_iso3166);
35019872Swollman	lineno = 0;
35119872Swollman
352298033Saraujo	while ((s = fgetln(fp, &len)) != NULL) {
35319872Swollman		lineno++;
35419872Swollman		if (s[len - 1] != '\n')
355198350Sedwin			errx(1, "%s:%d: invalid format", path_iso3166, lineno);
35619872Swollman		s[len - 1] = '\0';
35730999Sjoerg		if (s[0] == '#' || strspn(s, " \t") == len - 1)
35819872Swollman			continue;
35919872Swollman
36019872Swollman		/* Isolate the two-letter code. */
36119872Swollman		t = strsep(&s, "\t");
362298033Saraujo		if (t == NULL || strlen(t) != 2)
363198350Sedwin			errx(1, "%s:%d: invalid format", path_iso3166, lineno);
36419872Swollman		if (t[0] < 'A' || t[0] > 'Z' || t[1] < 'A' || t[1] > 'Z')
365198350Sedwin			errx(1, "%s:%d: invalid code `%s'", path_iso3166,
366179530Sjkim			    lineno, t);
36719872Swollman
36819872Swollman		/* Now skip past the three-letter and numeric codes. */
369179530Sjkim		name = strsep(&s, "\t");	/* 3-let */
370298033Saraujo		if (name == NULL || strlen(name) != 3)
371198350Sedwin			errx(1, "%s:%d: invalid format", path_iso3166, lineno);
372179530Sjkim		name = strsep(&s, "\t");	/* numeric */
373298033Saraujo		if (name == NULL || strlen(name) != 3)
374198350Sedwin			errx(1, "%s:%d: invalid format", path_iso3166, lineno);
37519872Swollman
37619872Swollman		name = s;
37719872Swollman
37819872Swollman		cp = &countries[CODE2INT(t)];
37919872Swollman		if (cp->name)
380198350Sedwin			errx(1, "%s:%d: country code `%s' multiply defined: %s",
381198350Sedwin			    path_iso3166, lineno, t, cp->name);
38219872Swollman		cp->name = strdup(name);
38356487Scharnier		if (cp->name == NULL)
38456487Scharnier			errx(1, "malloc failed");
38519872Swollman		cp->tlc = strdup(t);
38656487Scharnier		if (cp->tlc == NULL)
38756487Scharnier			errx(1, "malloc failed");
38819872Swollman	}
38919872Swollman
39019872Swollman	fclose(fp);
39119872Swollman}
39219872Swollman
39319872Swollmanstatic void
39419872Swollmanadd_zone_to_country(int lineno, const char *tlc, const char *descr,
395179530Sjkim    const char *file, struct continent *cont)
39619872Swollman{
397179530Sjkim	struct zone	*zp;
398179530Sjkim	struct country	*cp;
39919872Swollman
40019872Swollman	if (tlc[0] < 'A' || tlc[0] > 'Z' || tlc[1] < 'A' || tlc[1] > 'Z')
401198350Sedwin		errx(1, "%s:%d: country code `%s' invalid", path_zonetab,
402179530Sjkim		    lineno, tlc);
403227934Sfjoe
40419872Swollman	cp = &countries[CODE2INT(tlc)];
40519872Swollman	if (cp->name == 0)
406198350Sedwin		errx(1, "%s:%d: country code `%s' unknown", path_zonetab,
407179530Sjkim		    lineno, tlc);
40819872Swollman
40919872Swollman	if (descr) {
41019872Swollman		if (cp->nzones < 0)
411198350Sedwin			errx(1, "%s:%d: conflicting zone definition",
412198350Sedwin			    path_zonetab, lineno);
41319872Swollman
414179530Sjkim		zp = malloc(sizeof(*zp));
415298033Saraujo		if (zp == NULL)
416179530Sjkim			errx(1, "malloc(%zu)", sizeof(*zp));
417227934Sfjoe
41819872Swollman		if (cp->nzones == 0)
41919872Swollman			TAILQ_INIT(&cp->zones);
42019872Swollman
42119872Swollman		zp->descr = strdup(descr);
42256487Scharnier		if (zp->descr == NULL)
42356487Scharnier			errx(1, "malloc failed");
42419872Swollman		zp->filename = strdup(file);
42556487Scharnier		if (zp->filename == NULL)
42656487Scharnier			errx(1, "malloc failed");
42719872Swollman		zp->continent = cont;
42819872Swollman		TAILQ_INSERT_TAIL(&cp->zones, zp, link);
42919872Swollman		cp->nzones++;
43019872Swollman	} else {
43119872Swollman		if (cp->nzones > 0)
432198350Sedwin			errx(1, "%s:%d: zone must have description",
433198350Sedwin			    path_zonetab, lineno);
43419872Swollman		if (cp->nzones < 0)
435198350Sedwin			errx(1, "%s:%d: zone multiply defined",
436198350Sedwin			    path_zonetab, lineno);
43719872Swollman		cp->nzones = -1;
43819872Swollman		cp->filename = strdup(file);
43956487Scharnier		if (cp->filename == NULL)
44056487Scharnier			errx(1, "malloc failed");
44119872Swollman		cp->continent = cont;
44219872Swollman	}
44319872Swollman}
44419872Swollman
44519872Swollman/*
44619872Swollman * This comparison function intentionally sorts all of the null-named
44719872Swollman * ``countries''---i.e., the codes that don't correspond to a real
44819872Swollman * country---to the end.  Everything else is lexical by country name.
44919872Swollman */
45019872Swollmanstatic int
45119872Swollmancompare_countries(const void *xa, const void *xb)
45219872Swollman{
45319872Swollman	const struct country *a = xa, *b = xb;
45419872Swollman
45519872Swollman	if (a->name == 0 && b->name == 0)
456179530Sjkim		return (0);
45719872Swollman	if (a->name == 0 && b->name != 0)
458179530Sjkim		return (1);
45919872Swollman	if (b->name == 0)
460179530Sjkim		return (-1);
46119872Swollman
462179530Sjkim	return (strcmp(a->name, b->name));
46319872Swollman}
46419872Swollman
46519872Swollman/*
46619872Swollman * This must be done AFTER all zone descriptions are read, since it breaks
46719872Swollman * CODE2INT().
46819872Swollman */
46919872Swollmanstatic void
47019872Swollmansort_countries(void)
47119872Swollman{
472179530Sjkim
473179530Sjkim	qsort(countries, NCOUNTRIES, sizeof(countries[0]), compare_countries);
47419872Swollman}
47519872Swollman
47619872Swollmanstatic void
47719872Swollmanread_zones(void)
47819872Swollman{
479179530Sjkim	char		contbuf[16];
480179530Sjkim	FILE		*fp;
48119872Swollman	struct continent *cont;
482338440Sdelphij	size_t		len, contlen;
483364903Sphilip	char		*line, *country_list, *tlc, *file, *descr, *p;
484179530Sjkim	int		lineno;
48519872Swollman
486198350Sedwin	fp = fopen(path_zonetab, "r");
48719872Swollman	if (!fp)
488209190Semaste		err(1, "%s", path_zonetab);
48919872Swollman	lineno = 0;
49019872Swollman
491298033Saraujo	while ((line = fgetln(fp, &len)) != NULL) {
49219872Swollman		lineno++;
49319872Swollman		if (line[len - 1] != '\n')
494198350Sedwin			errx(1, "%s:%d: invalid format", path_zonetab, lineno);
49519872Swollman		line[len - 1] = '\0';
49619872Swollman		if (line[0] == '#')
49719872Swollman			continue;
49819872Swollman
499364903Sphilip		country_list = strsep(&line, "\t");
500281733Seadler		/* coord = */ strsep(&line, "\t");	 /* Unused */
50119872Swollman		file = strsep(&line, "\t");
502338440Sdelphij		/* get continent portion from continent/country */
50319872Swollman		p = strchr(file, '/');
504298033Saraujo		if (p == NULL)
505198350Sedwin			errx(1, "%s:%d: invalid zone name `%s'", path_zonetab,
506179530Sjkim			    lineno, file);
507338440Sdelphij		contlen = p - file + 1;		/* trailing nul */
508338440Sdelphij		if (contlen > sizeof(contbuf))
509338440Sdelphij			errx(1, "%s:%d: continent name in zone name `%s' too long",
510338440Sdelphij			    path_zonetab, lineno, file);
511338440Sdelphij		strlcpy(contbuf, file, contlen);
51219872Swollman		cont = find_continent(contbuf);
51319872Swollman		if (!cont)
514198350Sedwin			errx(1, "%s:%d: invalid region `%s'", path_zonetab,
515179530Sjkim			    lineno, contbuf);
51619872Swollman
517179530Sjkim		descr = (line != NULL && *line != '\0') ? line : NULL;
51819872Swollman
519364903Sphilip		while (country_list != NULL) {
520364903Sphilip			tlc = strsep(&country_list, ",");
521364903Sphilip			if (strlen(tlc) != 2)
522364903Sphilip				errx(1, "%s:%d: invalid country code `%s'",
523364903Sphilip				    path_zonetab, lineno, tlc);
524364903Sphilip			add_zone_to_country(lineno, tlc, descr, file, cont);
525364903Sphilip		}
52619872Swollman	}
52719872Swollman	fclose(fp);
52819872Swollman}
52919872Swollman
53019872Swollmanstatic void
53119872Swollmanmake_menus(void)
53219872Swollman{
533179530Sjkim	struct country	*cp;
534179530Sjkim	struct zone	*zp, *zp2;
53519872Swollman	struct continent *cont;
536179530Sjkim	dialogMenuItem	*dmi;
537179530Sjkim	int		i;
53819872Swollman
53919872Swollman	/*
54019872Swollman	 * First, count up all the countries in each continent/ocean.
54119872Swollman	 * Be careful to count those countries which have multiple zones
54219872Swollman	 * only once for each.  NB: some countries are in multiple
54319872Swollman	 * continents/oceans.
54419872Swollman	 */
54519872Swollman	for (cp = countries; cp->name; cp++) {
54619872Swollman		if (cp->nzones == 0)
54719872Swollman			continue;
54819872Swollman		if (cp->nzones < 0) {
54919872Swollman			cp->continent->nitems++;
55019872Swollman		} else {
55170486Sben			TAILQ_FOREACH(zp, &cp->zones, link) {
55219872Swollman				cont = zp->continent;
55370486Sben				for (zp2 = TAILQ_FIRST(&cp->zones);
554179530Sjkim				    zp2->continent != cont;
555179530Sjkim				    zp2 = TAILQ_NEXT(zp2, link))
55619872Swollman					;
55719872Swollman				if (zp2 == zp)
55819872Swollman					zp->continent->nitems++;
55919872Swollman			}
56019872Swollman		}
56119872Swollman	}
56219872Swollman
56319872Swollman	/*
564179530Sjkim	 * Now allocate memory for the country menus and initialize
565179530Sjkim	 * continent menus.  We set nitems back to zero so that we can
566179530Sjkim	 * use it for counting again when we actually build the menus.
56719872Swollman	 */
568179530Sjkim	memset(continents, 0, sizeof(continents));
56919872Swollman	for (i = 0; i < NCONTINENTS; i++) {
57019872Swollman		continent_names[i].continent->menu =
571179530Sjkim		    malloc(sizeof(dialogMenuItem) *
572179530Sjkim		    continent_names[i].continent->nitems);
573298033Saraujo		if (continent_names[i].continent->menu == NULL)
57456487Scharnier			errx(1, "malloc for continent menu");
57519872Swollman		continent_names[i].continent->nitems = 0;
576179530Sjkim		continents[i].prompt = continent_items[i].prompt;
577179530Sjkim		continents[i].title = continent_items[i].title;
578179530Sjkim		continents[i].fire = continent_country_menu;
579179530Sjkim		continents[i].data = continent_names[i].continent;
58019872Swollman	}
58119872Swollman
58219872Swollman	/*
58319872Swollman	 * Now that memory is allocated, create the menu items for
58419872Swollman	 * each continent.  For multiple-zone countries, also create
58519872Swollman	 * the country's zone submenu.
58619872Swollman	 */
58719872Swollman	for (cp = countries; cp->name; cp++) {
58819872Swollman		if (cp->nzones == 0)
58919872Swollman			continue;
59019872Swollman		if (cp->nzones < 0) {
59119872Swollman			dmi = &cp->continent->menu[cp->continent->nitems];
592179530Sjkim			memset(dmi, 0, sizeof(*dmi));
593179530Sjkim			asprintf(&dmi->prompt, "%d", ++cp->continent->nitems);
59419872Swollman			dmi->title = cp->name;
59519872Swollman			dmi->fire = set_zone_whole_country;
59619872Swollman			dmi->data = cp;
59719872Swollman		} else {
598179530Sjkim			cp->submenu = malloc(cp->nzones * sizeof(*dmi));
59919872Swollman			if (cp->submenu == 0)
60056487Scharnier				errx(1, "malloc for submenu");
60119872Swollman			cp->nzones = 0;
60270486Sben			TAILQ_FOREACH(zp, &cp->zones, link) {
60319872Swollman				cont = zp->continent;
60419872Swollman				dmi = &cp->submenu[cp->nzones];
605179530Sjkim				memset(dmi, 0, sizeof(*dmi));
606179530Sjkim				asprintf(&dmi->prompt, "%d", ++cp->nzones);
60719872Swollman				dmi->title = zp->descr;
60819872Swollman				dmi->fire = set_zone_multi;
60919872Swollman				dmi->data = zp;
61019872Swollman
61170486Sben				for (zp2 = TAILQ_FIRST(&cp->zones);
612179530Sjkim				    zp2->continent != cont;
613179530Sjkim				    zp2 = TAILQ_NEXT(zp2, link))
61419872Swollman					;
61519872Swollman				if (zp2 != zp)
61619872Swollman					continue;
61719872Swollman
61819872Swollman				dmi = &cont->menu[cont->nitems];
619179530Sjkim				memset(dmi, 0, sizeof(*dmi));
62019872Swollman				asprintf(&dmi->prompt, "%d", ++cont->nitems);
62119872Swollman				dmi->title = cp->name;
62219872Swollman				dmi->fire = set_zone_menu;
62319872Swollman				dmi->data = cp;
62419872Swollman			}
62519872Swollman		}
62619872Swollman	}
62719872Swollman}
62819872Swollman
62919872Swollmanstatic int
63019872Swollmanset_zone_menu(dialogMenuItem *dmi)
63119872Swollman{
632179530Sjkim	char		title[64], prompt[64];
633179530Sjkim	struct country	*cp = dmi->data;
634179530Sjkim	int		menulen;
635179530Sjkim	int		rv;
63619872Swollman
637179530Sjkim	snprintf(title, sizeof(title), "%s Time Zones", cp->name);
638179530Sjkim	snprintf(prompt, sizeof(prompt),
639179530Sjkim	    "Select a zone which observes the same time as your locality.");
64019872Swollman	menulen = cp->nzones < 16 ? cp->nzones : 16;
641227934Sfjoe	rv = xdialog_menu(title, prompt, -1, -1, menulen, cp->nzones,
642227934Sfjoe	    cp->submenu);
64319872Swollman	if (rv != 0)
644179530Sjkim		return (DITEM_RECREATE);
645179530Sjkim	return (DITEM_LEAVE_MENU);
64619872Swollman}
64719872Swollman
648301131Ssmhstatic int
649220172Sedwinset_zone_utc(void)
650220172Sedwin{
651322375Smarius	if (!confirm_zone("UTC"))
652220172Sedwin		return (DITEM_FAILURE | DITEM_RECREATE);
653227934Sfjoe
654301131Ssmh	return (install_zoneinfo("UTC"));
655220172Sedwin}
656220172Sedwin
65719872Swollmanstatic int
658319192Sngieconfirm_zone(const char *filename)
659319192Sngie{
660319192Sngie	char		title[64], prompt[64];
661319192Sngie	time_t		t = time(0);
662319192Sngie	struct tm	*tm;
663319192Sngie	int		rv;
664319192Sngie
665322375Smarius	setenv("TZ", filename, 1);
666319192Sngie	tzset();
667319192Sngie	tm = localtime(&t);
668319192Sngie
669319192Sngie	snprintf(title, sizeof(title), "Confirmation");
670319192Sngie	snprintf(prompt, sizeof(prompt),
671319192Sngie	    "Does the abbreviation `%s' look reasonable?", tm->tm_zone);
672319192Sngie	rv = !dialog_yesno(title, prompt, 5, 72);
673319192Sngie	return (rv);
674319192Sngie}
675319192Sngie
676319192Sngiestatic int
677319192Sngieset_zone_multi(dialogMenuItem *dmi)
678319192Sngie{
679319192Sngie	struct zone	*zp = dmi->data;
680319192Sngie	int		rv;
681319192Sngie
682319192Sngie	if (!confirm_zone(zp->filename))
683319192Sngie		return (DITEM_FAILURE | DITEM_RECREATE);
684319192Sngie
685319192Sngie	rv = install_zoneinfo(zp->filename);
686319192Sngie	return (rv);
687319192Sngie}
688319192Sngie
689319192Sngiestatic int
690319192Sngieset_zone_whole_country(dialogMenuItem *dmi)
691319192Sngie{
692319192Sngie	struct country	*cp = dmi->data;
693319192Sngie	int		rv;
694319192Sngie
695319192Sngie	if (!confirm_zone(cp->filename))
696319192Sngie		return (DITEM_FAILURE | DITEM_RECREATE);
697319192Sngie
698319192Sngie	rv = install_zoneinfo(cp->filename);
699319192Sngie	return (rv);
700319192Sngie}
701319192Sngie
702319192Sngie#endif
703319192Sngie
704319192Sngiestatic int
705198350Sedwininstall_zoneinfo_file(const char *zoneinfo_file)
70619872Swollman{
707179530Sjkim	char		buf[1024];
708230005Swollman	char		title[64], prompt[SILLY_BUFFER_SIZE];
709179530Sjkim	struct stat	sb;
710179530Sjkim	ssize_t		len;
711179530Sjkim	int		fd1, fd2, copymode;
71219872Swollman
713198350Sedwin	if (lstat(path_localtime, &sb) < 0) {
71419872Swollman		/* Nothing there yet... */
71519872Swollman		copymode = 1;
716179530Sjkim	} else if (S_ISLNK(sb.st_mode))
71719872Swollman		copymode = 0;
71819872Swollman	else
71919872Swollman		copymode = 1;
72019872Swollman
72121915Sjkh#ifdef VERBOSE
722230299Semaste	snprintf(title, sizeof(title), "Info");
723322375Smarius	if (copymode)
724179530Sjkim		snprintf(prompt, sizeof(prompt),
725198350Sedwin		    "Copying %s to %s", zoneinfo_file, path_localtime);
72619872Swollman	else
727179530Sjkim		snprintf(prompt, sizeof(prompt),
728198350Sedwin		    "Creating symbolic link %s to %s",
729230299Semaste		    path_localtime, zoneinfo_file);
730319192Sngie#ifdef HAVE_DIALOG
731198267Sedwin	if (usedialog)
732230299Semaste		dialog_msgbox(title, prompt, 8, 72, 1);
733198267Sedwin	else
734319192Sngie#endif
735198267Sedwin		fprintf(stderr, "%s\n", prompt);
73621915Sjkh#endif
73719872Swollman
73819872Swollman	if (reallydoit) {
73919872Swollman		if (copymode) {
740198350Sedwin			fd1 = open(zoneinfo_file, O_RDONLY, 0);
74119872Swollman			if (fd1 < 0) {
742179530Sjkim				snprintf(title, sizeof(title), "Error");
743179530Sjkim				snprintf(prompt, sizeof(prompt),
744198350Sedwin				    "Could not open %s: %s", zoneinfo_file,
745179530Sjkim				    strerror(errno));
746319192Sngie#ifdef HAVE_DIALOG
747198267Sedwin				if (usedialog)
748227934Sfjoe					dialog_msgbox(title, prompt, 8, 72, 1);
749198267Sedwin				else
750319192Sngie#endif
751198267Sedwin					fprintf(stderr, "%s\n", prompt);
752179530Sjkim				return (DITEM_FAILURE | DITEM_RECREATE);
75319872Swollman			}
75419872Swollman
755231181Swollman			if (unlink(path_localtime) < 0 && errno != ENOENT) {
756230005Swollman				snprintf(prompt, sizeof(prompt),
757322375Smarius				    "Could not delete %s: %s",
758230005Swollman				    path_localtime, strerror(errno));
759319192Sngie#ifdef HAVE_DIALOG
760230005Swollman				if (usedialog) {
761230005Swollman					snprintf(title, sizeof(title), "Error");
762230005Swollman					dialog_msgbox(title, prompt, 8, 72, 1);
763230005Swollman				} else
764319192Sngie#endif
765230005Swollman					fprintf(stderr, "%s\n", prompt);
766230005Swollman				return (DITEM_FAILURE | DITEM_RECREATE);
767230005Swollman			}
768230005Swollman
769198350Sedwin			fd2 = open(path_localtime, O_CREAT | O_EXCL | O_WRONLY,
770179530Sjkim			    S_IRUSR | S_IRGRP | S_IROTH);
77119872Swollman			if (fd2 < 0) {
772179530Sjkim				snprintf(title, sizeof(title), "Error");
773179530Sjkim				snprintf(prompt, sizeof(prompt),
774198350Sedwin				    "Could not open %s: %s",
775198350Sedwin				    path_localtime, strerror(errno));
776319192Sngie#ifdef HAVE_DIALOG
777198267Sedwin				if (usedialog)
778227934Sfjoe					dialog_msgbox(title, prompt, 8, 72, 1);
779198267Sedwin				else
780319192Sngie#endif
781198267Sedwin					fprintf(stderr, "%s\n", prompt);
782179530Sjkim				return (DITEM_FAILURE | DITEM_RECREATE);
78319872Swollman			}
78419872Swollman
785179530Sjkim			while ((len = read(fd1, buf, sizeof(buf))) > 0)
786208830Sedwin				if ((len = write(fd2, buf, len)) < 0)
787208830Sedwin					break;
78819872Swollman
78919872Swollman			if (len == -1) {
790179530Sjkim				snprintf(title, sizeof(title), "Error");
791179530Sjkim				snprintf(prompt, sizeof(prompt),
792198350Sedwin				    "Error copying %s to %s %s", zoneinfo_file,
793198350Sedwin				    path_localtime, strerror(errno));
794319192Sngie#ifdef HAVE_DIALOG
795198267Sedwin				if (usedialog)
796227934Sfjoe					dialog_msgbox(title, prompt, 8, 72, 1);
797198267Sedwin				else
798319192Sngie#endif
799198267Sedwin					fprintf(stderr, "%s\n", prompt);
80019872Swollman				/* Better to leave none than a corrupt one. */
801198350Sedwin				unlink(path_localtime);
802179530Sjkim				return (DITEM_FAILURE | DITEM_RECREATE);
80319872Swollman			}
80419872Swollman			close(fd1);
80519872Swollman			close(fd2);
80619872Swollman		} else {
807198350Sedwin			if (access(zoneinfo_file, R_OK) != 0) {
808179530Sjkim				snprintf(title, sizeof(title), "Error");
809179530Sjkim				snprintf(prompt, sizeof(prompt),
810198350Sedwin				    "Cannot access %s: %s", zoneinfo_file,
811179530Sjkim				    strerror(errno));
812319192Sngie#ifdef HAVE_DIALOG
813198267Sedwin				if (usedialog)
814227934Sfjoe					dialog_msgbox(title, prompt, 8, 72, 1);
815198267Sedwin				else
816319192Sngie#endif
817198267Sedwin					fprintf(stderr, "%s\n", prompt);
818179530Sjkim				return (DITEM_FAILURE | DITEM_RECREATE);
81919872Swollman			}
820231181Swollman			if (unlink(path_localtime) < 0 && errno != ENOENT) {
821230005Swollman				snprintf(prompt, sizeof(prompt),
822322375Smarius				    "Could not delete %s: %s",
823230005Swollman				    path_localtime, strerror(errno));
824319192Sngie#ifdef HAVE_DIALOG
825230005Swollman				if (usedialog) {
826230005Swollman					snprintf(title, sizeof(title), "Error");
827230005Swollman					dialog_msgbox(title, prompt, 8, 72, 1);
828230005Swollman				} else
829319192Sngie#endif
830230005Swollman					fprintf(stderr, "%s\n", prompt);
831230005Swollman				return (DITEM_FAILURE | DITEM_RECREATE);
832230005Swollman			}
833198350Sedwin			if (symlink(zoneinfo_file, path_localtime) < 0) {
834179530Sjkim				snprintf(title, sizeof(title), "Error");
835179530Sjkim				snprintf(prompt, sizeof(prompt),
836198350Sedwin				    "Cannot create symbolic link %s to %s: %s",
837198350Sedwin				    path_localtime, zoneinfo_file,
838179530Sjkim				    strerror(errno));
839319192Sngie#ifdef HAVE_DIALOG
840198267Sedwin				if (usedialog)
841227934Sfjoe					dialog_msgbox(title, prompt, 8, 72, 1);
842198267Sedwin				else
843319192Sngie#endif
844198267Sedwin					fprintf(stderr, "%s\n", prompt);
845179530Sjkim				return (DITEM_FAILURE | DITEM_RECREATE);
84619872Swollman			}
84719872Swollman		}
84819872Swollman
84921915Sjkh#ifdef VERBOSE
850230299Semaste		snprintf(title, sizeof(title), "Done");
851230299Semaste		if (copymode)
852230299Semaste			snprintf(prompt, sizeof(prompt),
853230299Semaste			    "Copied timezone file from %s to %s",
854230299Semaste			    zoneinfo_file, path_localtime);
855230299Semaste		else
856230299Semaste			snprintf(prompt, sizeof(prompt),
857230299Semaste			    "Created symbolic link from %s to %s",
858230299Semaste			    zoneinfo_file, path_localtime);
859319192Sngie#ifdef HAVE_DIALOG
860230299Semaste		if (usedialog)
861230299Semaste			dialog_msgbox(title, prompt, 8, 72, 1);
862230299Semaste		else
863319192Sngie#endif
864230299Semaste			fprintf(stderr, "%s\n", prompt);
86521915Sjkh#endif
866230299Semaste	} /* reallydoit */
867198267Sedwin
868198350Sedwin	return (DITEM_LEAVE_MENU);
869198350Sedwin}
870198350Sedwin
871198350Sedwinstatic int
872198350Sedwininstall_zoneinfo(const char *zoneinfo)
873198350Sedwin{
874198350Sedwin	int		rv;
875198350Sedwin	FILE		*f;
876198350Sedwin	char		path_zoneinfo_file[MAXPATHLEN];
877198350Sedwin
878300706Struckman	if ((size_t)snprintf(path_zoneinfo_file, sizeof(path_zoneinfo_file),
879300706Struckman	    "%s/%s", path_zoneinfo, zoneinfo) >= sizeof(path_zoneinfo_file))
880300706Struckman		errx(1, "%s/%s name too long", path_zoneinfo, zoneinfo);
881198350Sedwin	rv = install_zoneinfo_file(path_zoneinfo_file);
882198350Sedwin
883198267Sedwin	/* Save knowledge for later */
884230296Semaste	if (reallydoit && (rv & DITEM_FAILURE) == 0) {
885230296Semaste		if ((f = fopen(path_db, "w")) != NULL) {
886230296Semaste			fprintf(f, "%s\n", zoneinfo);
887230296Semaste			fclose(f);
888230296Semaste		}
889198267Sedwin	}
890198267Sedwin
891198350Sedwin	return (rv);
89219872Swollman}
89319872Swollman
89430763Scharnierstatic void
895179530Sjkimusage(void)
89630763Scharnier{
897179530Sjkim
898222139Sru	fprintf(stderr, "usage: tzsetup [-nrs] [-C chroot_directory]"
899222139Sru	    " [zoneinfo_file | zoneinfo_name]\n");
90030763Scharnier	exit(1);
90130763Scharnier}
90230763Scharnier
90319872Swollmanint
90419872Swollmanmain(int argc, char **argv)
90519872Swollman{
906319192Sngie#ifdef HAVE_DIALOG
907179530Sjkim	char		title[64], prompt[128];
908319192Sngie	int		fd;
909319192Sngie#endif
910319192Sngie	int		c, rv, skiputc;
911274399Sdteske	char		vm_guest[16] = "";
912274399Sdteske	size_t		len = sizeof(vm_guest);
91319872Swollman
914195339Sattilio	skiputc = 0;
915274394Sdteske
916274394Sdteske	/* Default skiputc to 1 for VM guests */
917274399Sdteske	if (sysctlbyname("kern.vm_guest", vm_guest, &len, NULL, 0) == 0 &&
918274394Sdteske	    strcmp(vm_guest, "none") != 0)
919274399Sdteske		skiputc = 1;
920274394Sdteske
921198350Sedwin	while ((c = getopt(argc, argv, "C:nrs")) != -1) {
92219872Swollman		switch(c) {
923198350Sedwin		case 'C':
924198350Sedwin			chrootenv = optarg;
925198350Sedwin			break;
92619872Swollman		case 'n':
92719872Swollman			reallydoit = 0;
92819872Swollman			break;
929198267Sedwin		case 'r':
930198267Sedwin			reinstall = 1;
931319192Sngie#ifdef HAVE_DIALOG
932198350Sedwin			usedialog = 0;
933319192Sngie#endif
934198267Sedwin			break;
935195339Sattilio		case 's':
936195339Sattilio			skiputc = 1;
937195339Sattilio			break;
93819872Swollman		default:
93930763Scharnier			usage();
94019872Swollman		}
94119872Swollman	}
94219872Swollman
94343544Swollman	if (argc - optind > 1)
94430763Scharnier		usage();
94519872Swollman
946290031Sdelphij	if (chrootenv == NULL) {
947290031Sdelphij		strcpy(path_zonetab, _PATH_ZONETAB);
948290031Sdelphij		strcpy(path_iso3166, _PATH_ISO3166);
949290031Sdelphij		strcpy(path_zoneinfo, _PATH_ZONEINFO);
950290031Sdelphij		strcpy(path_localtime, _PATH_LOCALTIME);
951290031Sdelphij		strcpy(path_db, _PATH_DB);
952290031Sdelphij		strcpy(path_wall_cmos_clock, _PATH_WALL_CMOS_CLOCK);
953290031Sdelphij	} else {
954290031Sdelphij		sprintf(path_zonetab, "%s/%s", chrootenv, _PATH_ZONETAB);
955290031Sdelphij		sprintf(path_iso3166, "%s/%s", chrootenv, _PATH_ISO3166);
956290031Sdelphij		sprintf(path_zoneinfo, "%s/%s", chrootenv, _PATH_ZONEINFO);
957290031Sdelphij		sprintf(path_localtime, "%s/%s", chrootenv, _PATH_LOCALTIME);
958290031Sdelphij		sprintf(path_db, "%s/%s", chrootenv, _PATH_DB);
959290031Sdelphij		sprintf(path_wall_cmos_clock, "%s/%s", chrootenv,
960290031Sdelphij		    _PATH_WALL_CMOS_CLOCK);
961198350Sedwin	}
962198350Sedwin
96349435Sru	/* Override the user-supplied umask. */
964179530Sjkim	(void)umask(S_IWGRP | S_IWOTH);
96549435Sru
966198267Sedwin	if (reinstall == 1) {
967198267Sedwin		FILE *f;
968230520Semaste		char zoneinfo[MAXPATHLEN];
969198267Sedwin
970198350Sedwin		if ((f = fopen(path_db, "r")) != NULL) {
971230520Semaste			if (fgets(zoneinfo, sizeof(zoneinfo), f) != NULL) {
972230520Semaste				zoneinfo[sizeof(zoneinfo) - 1] = 0;
973230520Semaste				if (strlen(zoneinfo) > 0) {
974230520Semaste					zoneinfo[strlen(zoneinfo) - 1] = 0;
975230520Semaste					rv = install_zoneinfo(zoneinfo);
976198267Sedwin					exit(rv & ~DITEM_LEAVE_MENU);
977198267Sedwin				}
978198350Sedwin				errx(1, "Error reading %s.\n", path_db);
979198267Sedwin			}
980198267Sedwin			fclose(f);
981198267Sedwin			errx(1,
982198267Sedwin			    "Unable to determine earlier installed zoneinfo "
983230520Semaste			    "name. Check %s", path_db);
984198267Sedwin		}
985198350Sedwin		errx(1, "Cannot open %s for reading. Does it exist?", path_db);
986198267Sedwin	}
987198267Sedwin
988198350Sedwin	/*
989198350Sedwin	 * If the arguments on the command-line do not specify a file,
990198350Sedwin	 * then interpret it as a zoneinfo name
991198350Sedwin	 */
992198350Sedwin	if (optind == argc - 1) {
993198350Sedwin		struct stat sb;
994198350Sedwin
995198350Sedwin		if (stat(argv[optind], &sb) != 0) {
996319192Sngie#ifdef HAVE_DIALOG
997198350Sedwin			usedialog = 0;
998319192Sngie#endif
999198350Sedwin			rv = install_zoneinfo(argv[optind]);
1000198350Sedwin			exit(rv & ~DITEM_LEAVE_MENU);
1001198350Sedwin		}
1002198350Sedwin		/* FALLTHROUGH */
1003198350Sedwin	}
1004319192Sngie#ifdef HAVE_DIALOG
1005198350Sedwin
1006230520Semaste	read_iso3166_table();
1007230520Semaste	read_zones();
1008230520Semaste	sort_countries();
1009230520Semaste	make_menus();
1010230520Semaste
1011227934Sfjoe	init_dialog(stdin, stdout);
1012195339Sattilio	if (skiputc == 0) {
1013227934Sfjoe		DIALOG_VARS save_vars;
1014227934Sfjoe		int yesno;
1015227934Sfjoe
1016195339Sattilio		snprintf(title, sizeof(title),
1017195339Sattilio		    "Select local or UTC (Greenwich Mean Time) clock");
1018195339Sattilio		snprintf(prompt, sizeof(prompt),
1019195339Sattilio		    "Is this machine's CMOS clock set to UTC?  "
1020195339Sattilio		    "If it is set to local time,\n"
1021195339Sattilio		    "or you don't know, please choose NO here!");
1022227934Sfjoe		dlg_save_vars(&save_vars);
1023227934Sfjoe#if !defined(__sparc64__)
1024227934Sfjoe		dialog_vars.defaultno = TRUE;
1025227934Sfjoe#endif
1026227934Sfjoe		yesno = dialog_yesno(title, prompt, 7, 73);
1027227934Sfjoe		dlg_restore_vars(&save_vars);
1028227934Sfjoe		if (!yesno) {
1029195339Sattilio			if (reallydoit)
1030210243Snork				unlink(path_wall_cmos_clock);
1031195339Sattilio		} else {
1032195339Sattilio			if (reallydoit) {
1033210243Snork				fd = open(path_wall_cmos_clock,
1034195339Sattilio				    O_WRONLY | O_CREAT | O_TRUNC,
1035195339Sattilio				    S_IRUSR | S_IRGRP | S_IROTH);
1036198254Sedwin				if (fd < 0) {
1037198254Sedwin					end_dialog();
1038195339Sattilio					err(1, "create %s",
1039210243Snork					    path_wall_cmos_clock);
1040198254Sedwin				}
1041195339Sattilio				close(fd);
1042195339Sattilio			}
104341852Speter		}
1044227934Sfjoe		dlg_clear();
104532394Ssteve	}
104643544Swollman	if (optind == argc - 1) {
1047179530Sjkim		snprintf(title, sizeof(title), "Default timezone provided");
1048179530Sjkim		snprintf(prompt, sizeof(prompt),
1049179530Sjkim		    "\nUse the default `%s' zone?", argv[optind]);
1050179530Sjkim		if (!dialog_yesno(title, prompt, 7, 72)) {
1051198350Sedwin			rv = install_zoneinfo_file(argv[optind]);
1052227934Sfjoe			dlg_clear();
105343544Swollman			end_dialog();
1054198267Sedwin			exit(rv & ~DITEM_LEAVE_MENU);
105543544Swollman		}
1056227934Sfjoe		dlg_clear();
105743544Swollman	}
1058179530Sjkim	snprintf(title, sizeof(title), "Time Zone Selector");
1059179530Sjkim	snprintf(prompt, sizeof(prompt), "Select a region");
1060227934Sfjoe	xdialog_menu(title, prompt, -1, -1, NCONTINENTS, NCONTINENTS,
1061227934Sfjoe	    continents);
106243544Swollman
1063227934Sfjoe	dlg_clear();
106419872Swollman	end_dialog();
1065319192Sngie#else
1066319192Sngie	usage();
1067319192Sngie#endif
1068179530Sjkim	return (0);
106919872Swollman}
1070