tzsetup.c revision 274394
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: head/usr.sbin/tzsetup/tzsetup.c 274394 2014-11-11 19:37:17Z dteske $");
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 *);
202220172Sedwinstatic int	install_zoneinfo_file(const char *zoneinfo_file);
203179530Sjkimstatic int	set_zone_multi(dialogMenuItem *);
204179530Sjkimstatic int	set_zone_whole_country(dialogMenuItem *);
205179530Sjkimstatic int	set_zone_menu(dialogMenuItem *);
206220172Sedwinstatic int	set_zone_utc(void);
20719872Swollman
20819872Swollmanstruct continent {
20919872Swollman	dialogMenuItem *menu;
210179530Sjkim	int		nitems;
21119872Swollman};
21219872Swollman
213179530Sjkimstatic struct continent	africa, america, antarctica, arctic, asia, atlantic;
214220172Sedwinstatic struct continent	australia, europe, indian, pacific, utc;
21519872Swollman
21619872Swollmanstatic struct continent_names {
217179530Sjkim	const char	*name;
21819872Swollman	struct continent *continent;
21919872Swollman} continent_names[] = {
220179530Sjkim	{ "Africa",	&africa },
221179530Sjkim	{ "America",	&america },
222179530Sjkim	{ "Antarctica",	&antarctica },
223179530Sjkim	{ "Arctic",	&arctic },
224179530Sjkim	{ "Asia",	&asia },
225179530Sjkim	{ "Atlantic",	&atlantic },
226179530Sjkim	{ "Australia",	&australia },
227179530Sjkim	{ "Europe",	&europe },
228179530Sjkim	{ "Indian",	&indian },
229220172Sedwin	{ "Pacific",	&pacific },
230227934Sfjoe	{ "UTC",	&utc }
23119872Swollman};
23219872Swollman
233179530Sjkimstatic struct continent_items {
234179530Sjkim	char		prompt[2];
235179530Sjkim	char		title[30];
236179530Sjkim} continent_items[] = {
237179530Sjkim	{ "1",	"Africa" },
238179530Sjkim	{ "2",	"America -- North and South" },
239179530Sjkim	{ "3",	"Antarctica" },
240179530Sjkim	{ "4",	"Arctic Ocean" },
241179530Sjkim	{ "5",	"Asia" },
242179530Sjkim	{ "6",	"Atlantic Ocean" },
243179530Sjkim	{ "7",	"Australia" },
244179530Sjkim	{ "8",	"Europe" },
245179530Sjkim	{ "9",	"Indian Ocean" },
246220172Sedwin	{ "0",	"Pacific Ocean" },
247220172Sedwin	{ "a",	"UTC" }
24819872Swollman};
24919872Swollman
250179530Sjkim#define	NCONTINENTS	\
251179530Sjkim    (int)((sizeof(continent_items)) / (sizeof(continent_items[0])))
252179530Sjkimstatic dialogMenuItem continents[NCONTINENTS];
253179530Sjkim
254179530Sjkim#define	OCEANP(x)	((x) == 3 || (x) == 5 || (x) == 8 || (x) == 9)
255179530Sjkim
25619872Swollmanstatic int
25719872Swollmancontinent_country_menu(dialogMenuItem *continent)
25819872Swollman{
259179530Sjkim	char		title[64], prompt[64];
26019872Swollman	struct continent *contp = continent->data;
261179530Sjkim	int		isocean = OCEANP(continent - continents);
262179530Sjkim	int		menulen;
263179530Sjkim	int		rv;
26419872Swollman
265220172Sedwin	if (strcmp(continent->title, "UTC") == 0)
266227934Sfjoe	        return set_zone_utc();
267220172Sedwin
26819872Swollman	/* Short cut -- if there's only one country, don't post a menu. */
269179497Sjkim	if (contp->nitems == 1)
270179497Sjkim		return (contp->menu[0].fire(&contp->menu[0]));
27119872Swollman
27219872Swollman	/* It's amazing how much good grammar really matters... */
273179530Sjkim	if (!isocean) {
274179530Sjkim		snprintf(title, sizeof(title), "Countries in %s",
275179530Sjkim		    continent->title);
276179530Sjkim		snprintf(prompt, sizeof(prompt), "Select a country or region");
277179530Sjkim	} else {
278179530Sjkim		snprintf(title, sizeof(title), "Islands and groups in the %s",
279179530Sjkim		    continent->title);
280179530Sjkim		snprintf(prompt, sizeof(prompt), "Select an island or group");
281179530Sjkim	}
28219872Swollman
28319872Swollman	menulen = contp->nitems < 16 ? contp->nitems : 16;
284227934Sfjoe	rv = xdialog_menu(title, prompt, -1, -1, menulen, contp->nitems,
285227934Sfjoe	    contp->menu);
28619872Swollman	if (rv == 0)
287179530Sjkim		return (DITEM_LEAVE_MENU);
288179530Sjkim	return (DITEM_RECREATE);
28919872Swollman}
29019872Swollman
29119872Swollmanstatic struct continent *
29219872Swollmanfind_continent(const char *name)
29319872Swollman{
294179530Sjkim	int		i;
29519872Swollman
296179530Sjkim	for (i = 0; i < NCONTINENTS; i++)
29719872Swollman		if (strcmp(name, continent_names[i].name) == 0)
298179530Sjkim			return (continent_names[i].continent);
299179530Sjkim	return (0);
30019872Swollman}
30119872Swollman
30219872Swollmanstruct country {
303179530Sjkim	char		*name;
304179530Sjkim	char		*tlc;
305179530Sjkim	int		nzones;
306179530Sjkim	char		*filename;	/* use iff nzones < 0 */
307179530Sjkim	struct continent *continent;	/* use iff nzones < 0 */
308179530Sjkim	TAILQ_HEAD(, zone) zones;	/* use iff nzones > 0 */
309179530Sjkim	dialogMenuItem	*submenu;	/* use iff nzones > 0 */
31019872Swollman};
31119872Swollman
31219872Swollmanstruct zone {
31360938Sjake	TAILQ_ENTRY(zone) link;
314179530Sjkim	char		*descr;
315179530Sjkim	char		*filename;
31619872Swollman	struct continent *continent;
31719872Swollman};
31819872Swollman
31919872Swollman/*
32019872Swollman * This is the easiest organization... we use ISO 3166 country codes,
32119872Swollman * of the two-letter variety, so we just size this array to suit.
32219872Swollman * Beats worrying about dynamic allocation.
32319872Swollman */
324179530Sjkim#define	NCOUNTRIES	(26 * 26)
32522181Sjhaystatic struct country countries[NCOUNTRIES];
32619872Swollman
327179530Sjkim#define	CODE2INT(s)	((s[0] - 'A') * 26 + (s[1] - 'A'))
328179530Sjkim
32919872Swollman/*
33019872Swollman * Read the ISO 3166 country code database in _PATH_ISO3166
33119872Swollman * (/usr/share/misc/iso3166).  On error, exit via err(3).
33219872Swollman */
33319872Swollmanstatic void
33419872Swollmanread_iso3166_table(void)
33519872Swollman{
336179530Sjkim	FILE		*fp;
337179530Sjkim	struct country	*cp;
338179530Sjkim	size_t		len;
339179530Sjkim	char		*s, *t, *name;
340179530Sjkim	int		lineno;
34119872Swollman
342198350Sedwin	fp = fopen(path_iso3166, "r");
34319872Swollman	if (!fp)
344209190Semaste		err(1, "%s", path_iso3166);
34519872Swollman	lineno = 0;
34619872Swollman
34719872Swollman	while ((s = fgetln(fp, &len)) != 0) {
34819872Swollman		lineno++;
34919872Swollman		if (s[len - 1] != '\n')
350198350Sedwin			errx(1, "%s:%d: invalid format", path_iso3166, lineno);
35119872Swollman		s[len - 1] = '\0';
35230999Sjoerg		if (s[0] == '#' || strspn(s, " \t") == len - 1)
35319872Swollman			continue;
35419872Swollman
35519872Swollman		/* Isolate the two-letter code. */
35619872Swollman		t = strsep(&s, "\t");
35719872Swollman		if (t == 0 || strlen(t) != 2)
358198350Sedwin			errx(1, "%s:%d: invalid format", path_iso3166, lineno);
35919872Swollman		if (t[0] < 'A' || t[0] > 'Z' || t[1] < 'A' || t[1] > 'Z')
360198350Sedwin			errx(1, "%s:%d: invalid code `%s'", path_iso3166,
361179530Sjkim			    lineno, t);
36219872Swollman
36319872Swollman		/* Now skip past the three-letter and numeric codes. */
364179530Sjkim		name = strsep(&s, "\t");	/* 3-let */
36519872Swollman		if (name == 0 || strlen(name) != 3)
366198350Sedwin			errx(1, "%s:%d: invalid format", path_iso3166, lineno);
367179530Sjkim		name = strsep(&s, "\t");	/* numeric */
36819872Swollman		if (name == 0 || strlen(name) != 3)
369198350Sedwin			errx(1, "%s:%d: invalid format", path_iso3166, lineno);
37019872Swollman
37119872Swollman		name = s;
37219872Swollman
37319872Swollman		cp = &countries[CODE2INT(t)];
37419872Swollman		if (cp->name)
375198350Sedwin			errx(1, "%s:%d: country code `%s' multiply defined: %s",
376198350Sedwin			    path_iso3166, lineno, t, cp->name);
37719872Swollman		cp->name = strdup(name);
37856487Scharnier		if (cp->name == NULL)
37956487Scharnier			errx(1, "malloc failed");
38019872Swollman		cp->tlc = strdup(t);
38156487Scharnier		if (cp->tlc == NULL)
38256487Scharnier			errx(1, "malloc failed");
38319872Swollman	}
38419872Swollman
38519872Swollman	fclose(fp);
38619872Swollman}
38719872Swollman
38819872Swollmanstatic void
38919872Swollmanadd_zone_to_country(int lineno, const char *tlc, const char *descr,
390179530Sjkim    const char *file, struct continent *cont)
39119872Swollman{
392179530Sjkim	struct zone	*zp;
393179530Sjkim	struct country	*cp;
39419872Swollman
39519872Swollman	if (tlc[0] < 'A' || tlc[0] > 'Z' || tlc[1] < 'A' || tlc[1] > 'Z')
396198350Sedwin		errx(1, "%s:%d: country code `%s' invalid", path_zonetab,
397179530Sjkim		    lineno, tlc);
398227934Sfjoe
39919872Swollman	cp = &countries[CODE2INT(tlc)];
40019872Swollman	if (cp->name == 0)
401198350Sedwin		errx(1, "%s:%d: country code `%s' unknown", path_zonetab,
402179530Sjkim		    lineno, tlc);
40319872Swollman
40419872Swollman	if (descr) {
40519872Swollman		if (cp->nzones < 0)
406198350Sedwin			errx(1, "%s:%d: conflicting zone definition",
407198350Sedwin			    path_zonetab, lineno);
40819872Swollman
409179530Sjkim		zp = malloc(sizeof(*zp));
41019872Swollman		if (zp == 0)
411179530Sjkim			errx(1, "malloc(%zu)", sizeof(*zp));
412227934Sfjoe
41319872Swollman		if (cp->nzones == 0)
41419872Swollman			TAILQ_INIT(&cp->zones);
41519872Swollman
41619872Swollman		zp->descr = strdup(descr);
41756487Scharnier		if (zp->descr == NULL)
41856487Scharnier			errx(1, "malloc failed");
41919872Swollman		zp->filename = strdup(file);
42056487Scharnier		if (zp->filename == NULL)
42156487Scharnier			errx(1, "malloc failed");
42219872Swollman		zp->continent = cont;
42319872Swollman		TAILQ_INSERT_TAIL(&cp->zones, zp, link);
42419872Swollman		cp->nzones++;
42519872Swollman	} else {
42619872Swollman		if (cp->nzones > 0)
427198350Sedwin			errx(1, "%s:%d: zone must have description",
428198350Sedwin			    path_zonetab, lineno);
42919872Swollman		if (cp->nzones < 0)
430198350Sedwin			errx(1, "%s:%d: zone multiply defined",
431198350Sedwin			    path_zonetab, lineno);
43219872Swollman		cp->nzones = -1;
43319872Swollman		cp->filename = strdup(file);
43456487Scharnier		if (cp->filename == NULL)
43556487Scharnier			errx(1, "malloc failed");
43619872Swollman		cp->continent = cont;
43719872Swollman	}
43819872Swollman}
43919872Swollman
44019872Swollman/*
44119872Swollman * This comparison function intentionally sorts all of the null-named
44219872Swollman * ``countries''---i.e., the codes that don't correspond to a real
44319872Swollman * country---to the end.  Everything else is lexical by country name.
44419872Swollman */
44519872Swollmanstatic int
44619872Swollmancompare_countries(const void *xa, const void *xb)
44719872Swollman{
44819872Swollman	const struct country *a = xa, *b = xb;
44919872Swollman
45019872Swollman	if (a->name == 0 && b->name == 0)
451179530Sjkim		return (0);
45219872Swollman	if (a->name == 0 && b->name != 0)
453179530Sjkim		return (1);
45419872Swollman	if (b->name == 0)
455179530Sjkim		return (-1);
45619872Swollman
457179530Sjkim	return (strcmp(a->name, b->name));
45819872Swollman}
45919872Swollman
46019872Swollman/*
46119872Swollman * This must be done AFTER all zone descriptions are read, since it breaks
46219872Swollman * CODE2INT().
46319872Swollman */
46419872Swollmanstatic void
46519872Swollmansort_countries(void)
46619872Swollman{
467179530Sjkim
468179530Sjkim	qsort(countries, NCOUNTRIES, sizeof(countries[0]), compare_countries);
46919872Swollman}
47019872Swollman
47119872Swollmanstatic void
47219872Swollmanread_zones(void)
47319872Swollman{
474179530Sjkim	char		contbuf[16];
475179530Sjkim	FILE		*fp;
47619872Swollman	struct continent *cont;
477179530Sjkim	size_t		len;
478179530Sjkim	char		*line, *tlc, *coord, *file, *descr, *p;
479179530Sjkim	int		lineno;
48019872Swollman
481198350Sedwin	fp = fopen(path_zonetab, "r");
48219872Swollman	if (!fp)
483209190Semaste		err(1, "%s", path_zonetab);
48419872Swollman	lineno = 0;
48519872Swollman
48619872Swollman	while ((line = fgetln(fp, &len)) != 0) {
48719872Swollman		lineno++;
48819872Swollman		if (line[len - 1] != '\n')
489198350Sedwin			errx(1, "%s:%d: invalid format", path_zonetab, lineno);
49019872Swollman		line[len - 1] = '\0';
49119872Swollman		if (line[0] == '#')
49219872Swollman			continue;
49319872Swollman
49419872Swollman		tlc = strsep(&line, "\t");
49519872Swollman		if (strlen(tlc) != 2)
496198350Sedwin			errx(1, "%s:%d: invalid country code `%s'",
497198350Sedwin			    path_zonetab, lineno, tlc);
498208831Sedwin		coord = strsep(&line, "\t");	 /* Unused */
49919872Swollman		file = strsep(&line, "\t");
50019872Swollman		p = strchr(file, '/');
50119872Swollman		if (p == 0)
502198350Sedwin			errx(1, "%s:%d: invalid zone name `%s'", path_zonetab,
503179530Sjkim			    lineno, file);
50419872Swollman		contbuf[0] = '\0';
50519872Swollman		strncat(contbuf, file, p - file);
50619872Swollman		cont = find_continent(contbuf);
50719872Swollman		if (!cont)
508198350Sedwin			errx(1, "%s:%d: invalid region `%s'", path_zonetab,
509179530Sjkim			    lineno, contbuf);
51019872Swollman
511179530Sjkim		descr = (line != NULL && *line != '\0') ? line : NULL;
51219872Swollman
51319872Swollman		add_zone_to_country(lineno, tlc, descr, file, cont);
51419872Swollman	}
51519872Swollman	fclose(fp);
51619872Swollman}
51719872Swollman
51819872Swollmanstatic void
51919872Swollmanmake_menus(void)
52019872Swollman{
521179530Sjkim	struct country	*cp;
522179530Sjkim	struct zone	*zp, *zp2;
52319872Swollman	struct continent *cont;
524179530Sjkim	dialogMenuItem	*dmi;
525179530Sjkim	int		i;
52619872Swollman
52719872Swollman	/*
52819872Swollman	 * First, count up all the countries in each continent/ocean.
52919872Swollman	 * Be careful to count those countries which have multiple zones
53019872Swollman	 * only once for each.  NB: some countries are in multiple
53119872Swollman	 * continents/oceans.
53219872Swollman	 */
53319872Swollman	for (cp = countries; cp->name; cp++) {
53419872Swollman		if (cp->nzones == 0)
53519872Swollman			continue;
53619872Swollman		if (cp->nzones < 0) {
53719872Swollman			cp->continent->nitems++;
53819872Swollman		} else {
53970486Sben			TAILQ_FOREACH(zp, &cp->zones, link) {
54019872Swollman				cont = zp->continent;
54170486Sben				for (zp2 = TAILQ_FIRST(&cp->zones);
542179530Sjkim				    zp2->continent != cont;
543179530Sjkim				    zp2 = TAILQ_NEXT(zp2, link))
54419872Swollman					;
54519872Swollman				if (zp2 == zp)
54619872Swollman					zp->continent->nitems++;
54719872Swollman			}
54819872Swollman		}
54919872Swollman	}
55019872Swollman
55119872Swollman	/*
552179530Sjkim	 * Now allocate memory for the country menus and initialize
553179530Sjkim	 * continent menus.  We set nitems back to zero so that we can
554179530Sjkim	 * use it for counting again when we actually build the menus.
55519872Swollman	 */
556179530Sjkim	memset(continents, 0, sizeof(continents));
55719872Swollman	for (i = 0; i < NCONTINENTS; i++) {
55819872Swollman		continent_names[i].continent->menu =
559179530Sjkim		    malloc(sizeof(dialogMenuItem) *
560179530Sjkim		    continent_names[i].continent->nitems);
56119872Swollman		if (continent_names[i].continent->menu == 0)
56256487Scharnier			errx(1, "malloc for continent menu");
56319872Swollman		continent_names[i].continent->nitems = 0;
564179530Sjkim		continents[i].prompt = continent_items[i].prompt;
565179530Sjkim		continents[i].title = continent_items[i].title;
566179530Sjkim		continents[i].fire = continent_country_menu;
567179530Sjkim		continents[i].data = continent_names[i].continent;
56819872Swollman	}
56919872Swollman
57019872Swollman	/*
57119872Swollman	 * Now that memory is allocated, create the menu items for
57219872Swollman	 * each continent.  For multiple-zone countries, also create
57319872Swollman	 * the country's zone submenu.
57419872Swollman	 */
57519872Swollman	for (cp = countries; cp->name; cp++) {
57619872Swollman		if (cp->nzones == 0)
57719872Swollman			continue;
57819872Swollman		if (cp->nzones < 0) {
57919872Swollman			dmi = &cp->continent->menu[cp->continent->nitems];
580179530Sjkim			memset(dmi, 0, sizeof(*dmi));
581179530Sjkim			asprintf(&dmi->prompt, "%d", ++cp->continent->nitems);
58219872Swollman			dmi->title = cp->name;
58319872Swollman			dmi->fire = set_zone_whole_country;
58419872Swollman			dmi->data = cp;
58519872Swollman		} else {
586179530Sjkim			cp->submenu = malloc(cp->nzones * sizeof(*dmi));
58719872Swollman			if (cp->submenu == 0)
58856487Scharnier				errx(1, "malloc for submenu");
58919872Swollman			cp->nzones = 0;
59070486Sben			TAILQ_FOREACH(zp, &cp->zones, link) {
59119872Swollman				cont = zp->continent;
59219872Swollman				dmi = &cp->submenu[cp->nzones];
593179530Sjkim				memset(dmi, 0, sizeof(*dmi));
594179530Sjkim				asprintf(&dmi->prompt, "%d", ++cp->nzones);
59519872Swollman				dmi->title = zp->descr;
59619872Swollman				dmi->fire = set_zone_multi;
59719872Swollman				dmi->data = zp;
59819872Swollman
59970486Sben				for (zp2 = TAILQ_FIRST(&cp->zones);
600179530Sjkim				    zp2->continent != cont;
601179530Sjkim				    zp2 = TAILQ_NEXT(zp2, link))
60219872Swollman					;
60319872Swollman				if (zp2 != zp)
60419872Swollman					continue;
60519872Swollman
60619872Swollman				dmi = &cont->menu[cont->nitems];
607179530Sjkim				memset(dmi, 0, sizeof(*dmi));
60819872Swollman				asprintf(&dmi->prompt, "%d", ++cont->nitems);
60919872Swollman				dmi->title = cp->name;
61019872Swollman				dmi->fire = set_zone_menu;
61119872Swollman				dmi->data = cp;
61219872Swollman			}
61319872Swollman		}
61419872Swollman	}
61519872Swollman}
61619872Swollman
61719872Swollmanstatic int
61819872Swollmanset_zone_menu(dialogMenuItem *dmi)
61919872Swollman{
620179530Sjkim	char		title[64], prompt[64];
621179530Sjkim	struct country	*cp = dmi->data;
622179530Sjkim	int		menulen;
623179530Sjkim	int		rv;
62419872Swollman
625179530Sjkim	snprintf(title, sizeof(title), "%s Time Zones", cp->name);
626179530Sjkim	snprintf(prompt, sizeof(prompt),
627179530Sjkim	    "Select a zone which observes the same time as your locality.");
62819872Swollman	menulen = cp->nzones < 16 ? cp->nzones : 16;
629227934Sfjoe	rv = xdialog_menu(title, prompt, -1, -1, menulen, cp->nzones,
630227934Sfjoe	    cp->submenu);
63119872Swollman	if (rv != 0)
632179530Sjkim		return (DITEM_RECREATE);
633179530Sjkim	return (DITEM_LEAVE_MENU);
63419872Swollman}
63519872Swollman
636220172Sedwinint
637220172Sedwinset_zone_utc(void)
638220172Sedwin{
639220172Sedwin	if (!confirm_zone(NULL))
640220172Sedwin		return (DITEM_FAILURE | DITEM_RECREATE);
641227934Sfjoe
642220172Sedwin	return (install_zoneinfo_file(NULL));
643220172Sedwin}
644220172Sedwin
64519872Swollmanstatic int
646198350Sedwininstall_zoneinfo_file(const char *zoneinfo_file)
64719872Swollman{
648179530Sjkim	char		buf[1024];
649230005Swollman	char		title[64], prompt[SILLY_BUFFER_SIZE];
650179530Sjkim	struct stat	sb;
651179530Sjkim	ssize_t		len;
652179530Sjkim	int		fd1, fd2, copymode;
65319872Swollman
654198350Sedwin	if (lstat(path_localtime, &sb) < 0) {
65519872Swollman		/* Nothing there yet... */
65619872Swollman		copymode = 1;
657179530Sjkim	} else if (S_ISLNK(sb.st_mode))
65819872Swollman		copymode = 0;
65919872Swollman	else
66019872Swollman		copymode = 1;
66119872Swollman
66221915Sjkh#ifdef VERBOSE
663230299Semaste	snprintf(title, sizeof(title), "Info");
664230299Semaste	if (zoneinfo_file == NULL)
665179530Sjkim		snprintf(prompt, sizeof(prompt),
666230299Semaste		    "Removing %s", path_localtime);
667230299Semaste	else if (copymode)
668230299Semaste		snprintf(prompt, sizeof(prompt),
669198350Sedwin		    "Copying %s to %s", zoneinfo_file, path_localtime);
67019872Swollman	else
671179530Sjkim		snprintf(prompt, sizeof(prompt),
672198350Sedwin		    "Creating symbolic link %s to %s",
673230299Semaste		    path_localtime, zoneinfo_file);
674198267Sedwin	if (usedialog)
675230299Semaste		dialog_msgbox(title, prompt, 8, 72, 1);
676198267Sedwin	else
677198267Sedwin		fprintf(stderr, "%s\n", prompt);
67821915Sjkh#endif
67919872Swollman
68019872Swollman	if (reallydoit) {
681220172Sedwin		if (zoneinfo_file == NULL) {
682220172Sedwin			if (unlink(path_localtime) < 0 && errno != ENOENT) {
683220172Sedwin				snprintf(title, sizeof(title), "Error");
684220172Sedwin				snprintf(prompt, sizeof(prompt),
685220172Sedwin				     "Could not delete %s: %s", path_localtime,
686220172Sedwin				     strerror(errno));
687220172Sedwin				if (usedialog)
688227934Sfjoe					dialog_msgbox(title, prompt, 8, 72, 1);
689220172Sedwin				else
690220172Sedwin					fprintf(stderr, "%s\n", prompt);
691220172Sedwin
692220172Sedwin				return (DITEM_FAILURE | DITEM_RECREATE);
693220172Sedwin			}
694227011Sdougb			if (unlink(path_db) < 0 && errno != ENOENT) {
695227011Sdougb				snprintf(title, sizeof(title), "Error");
696227011Sdougb				snprintf(prompt, sizeof(prompt),
697227011Sdougb				     "Could not delete %s: %s", path_db,
698227011Sdougb				     strerror(errno));
699227011Sdougb				if (usedialog)
700227934Sfjoe					dialog_msgbox(title, prompt, 8, 72, 1);
701227011Sdougb				else
702227011Sdougb					fprintf(stderr, "%s\n", prompt);
703227011Sdougb
704227011Sdougb				return (DITEM_FAILURE | DITEM_RECREATE);
705227011Sdougb			}
706230299Semaste#ifdef VERBOSE
707247780Sdteske			snprintf(title, sizeof(title), "Done");
708230299Semaste			snprintf(prompt, sizeof(prompt),
709230299Semaste			    "Removed %s", path_localtime);
710247780Sdteske			if (usedialog)
711247780Sdteske				dialog_msgbox(title, prompt, 8, 72, 1);
712247780Sdteske			else
713247780Sdteske				fprintf(stderr, "%s\n", prompt);
714230299Semaste#endif
715220172Sedwin			return (DITEM_LEAVE_MENU);
716220172Sedwin		}
717227934Sfjoe
71819872Swollman		if (copymode) {
719198350Sedwin			fd1 = open(zoneinfo_file, O_RDONLY, 0);
72019872Swollman			if (fd1 < 0) {
721179530Sjkim				snprintf(title, sizeof(title), "Error");
722179530Sjkim				snprintf(prompt, sizeof(prompt),
723198350Sedwin				    "Could not open %s: %s", zoneinfo_file,
724179530Sjkim				    strerror(errno));
725198267Sedwin				if (usedialog)
726227934Sfjoe					dialog_msgbox(title, prompt, 8, 72, 1);
727198267Sedwin				else
728198267Sedwin					fprintf(stderr, "%s\n", prompt);
729179530Sjkim				return (DITEM_FAILURE | DITEM_RECREATE);
73019872Swollman			}
73119872Swollman
732231181Swollman			if (unlink(path_localtime) < 0 && errno != ENOENT) {
733230005Swollman				snprintf(prompt, sizeof(prompt),
734230005Swollman				    "Could not unlink %s: %s",
735230005Swollman				    path_localtime, strerror(errno));
736230005Swollman				if (usedialog) {
737230005Swollman					snprintf(title, sizeof(title), "Error");
738230005Swollman					dialog_msgbox(title, prompt, 8, 72, 1);
739230005Swollman				} else
740230005Swollman					fprintf(stderr, "%s\n", prompt);
741230005Swollman				return (DITEM_FAILURE | DITEM_RECREATE);
742230005Swollman			}
743230005Swollman
744198350Sedwin			fd2 = open(path_localtime, O_CREAT | O_EXCL | O_WRONLY,
745179530Sjkim			    S_IRUSR | S_IRGRP | S_IROTH);
74619872Swollman			if (fd2 < 0) {
747179530Sjkim				snprintf(title, sizeof(title), "Error");
748179530Sjkim				snprintf(prompt, sizeof(prompt),
749198350Sedwin				    "Could not open %s: %s",
750198350Sedwin				    path_localtime, strerror(errno));
751198267Sedwin				if (usedialog)
752227934Sfjoe					dialog_msgbox(title, prompt, 8, 72, 1);
753198267Sedwin				else
754198267Sedwin					fprintf(stderr, "%s\n", prompt);
755179530Sjkim				return (DITEM_FAILURE | DITEM_RECREATE);
75619872Swollman			}
75719872Swollman
758179530Sjkim			while ((len = read(fd1, buf, sizeof(buf))) > 0)
759208830Sedwin				if ((len = write(fd2, buf, len)) < 0)
760208830Sedwin					break;
76119872Swollman
76219872Swollman			if (len == -1) {
763179530Sjkim				snprintf(title, sizeof(title), "Error");
764179530Sjkim				snprintf(prompt, sizeof(prompt),
765198350Sedwin				    "Error copying %s to %s %s", zoneinfo_file,
766198350Sedwin				    path_localtime, strerror(errno));
767198267Sedwin				if (usedialog)
768227934Sfjoe					dialog_msgbox(title, prompt, 8, 72, 1);
769198267Sedwin				else
770198267Sedwin					fprintf(stderr, "%s\n", prompt);
77119872Swollman				/* Better to leave none than a corrupt one. */
772198350Sedwin				unlink(path_localtime);
773179530Sjkim				return (DITEM_FAILURE | DITEM_RECREATE);
77419872Swollman			}
77519872Swollman			close(fd1);
77619872Swollman			close(fd2);
77719872Swollman		} else {
778198350Sedwin			if (access(zoneinfo_file, R_OK) != 0) {
779179530Sjkim				snprintf(title, sizeof(title), "Error");
780179530Sjkim				snprintf(prompt, sizeof(prompt),
781198350Sedwin				    "Cannot access %s: %s", zoneinfo_file,
782179530Sjkim				    strerror(errno));
783198267Sedwin				if (usedialog)
784227934Sfjoe					dialog_msgbox(title, prompt, 8, 72, 1);
785198267Sedwin				else
786198267Sedwin					fprintf(stderr, "%s\n", prompt);
787179530Sjkim				return (DITEM_FAILURE | DITEM_RECREATE);
78819872Swollman			}
789231181Swollman			if (unlink(path_localtime) < 0 && errno != ENOENT) {
790230005Swollman				snprintf(prompt, sizeof(prompt),
791230005Swollman				    "Could not unlink %s: %s",
792230005Swollman				    path_localtime, strerror(errno));
793230005Swollman				if (usedialog) {
794230005Swollman					snprintf(title, sizeof(title), "Error");
795230005Swollman					dialog_msgbox(title, prompt, 8, 72, 1);
796230005Swollman				} else
797230005Swollman					fprintf(stderr, "%s\n", prompt);
798230005Swollman				return (DITEM_FAILURE | DITEM_RECREATE);
799230005Swollman			}
800198350Sedwin			if (symlink(zoneinfo_file, path_localtime) < 0) {
801179530Sjkim				snprintf(title, sizeof(title), "Error");
802179530Sjkim				snprintf(prompt, sizeof(prompt),
803198350Sedwin				    "Cannot create symbolic link %s to %s: %s",
804198350Sedwin				    path_localtime, zoneinfo_file,
805179530Sjkim				    strerror(errno));
806198267Sedwin				if (usedialog)
807227934Sfjoe					dialog_msgbox(title, prompt, 8, 72, 1);
808198267Sedwin				else
809198267Sedwin					fprintf(stderr, "%s\n", prompt);
810179530Sjkim				return (DITEM_FAILURE | DITEM_RECREATE);
81119872Swollman			}
81219872Swollman		}
81319872Swollman
81421915Sjkh#ifdef VERBOSE
815230299Semaste		snprintf(title, sizeof(title), "Done");
816230299Semaste		if (copymode)
817230299Semaste			snprintf(prompt, sizeof(prompt),
818230299Semaste			    "Copied timezone file from %s to %s",
819230299Semaste			    zoneinfo_file, path_localtime);
820230299Semaste		else
821230299Semaste			snprintf(prompt, sizeof(prompt),
822230299Semaste			    "Created symbolic link from %s to %s",
823230299Semaste			    zoneinfo_file, path_localtime);
824230299Semaste		if (usedialog)
825230299Semaste			dialog_msgbox(title, prompt, 8, 72, 1);
826230299Semaste		else
827230299Semaste			fprintf(stderr, "%s\n", prompt);
82821915Sjkh#endif
829230299Semaste	} /* reallydoit */
830198267Sedwin
831198350Sedwin	return (DITEM_LEAVE_MENU);
832198350Sedwin}
833198350Sedwin
834198350Sedwinstatic int
835198350Sedwininstall_zoneinfo(const char *zoneinfo)
836198350Sedwin{
837198350Sedwin	int		rv;
838198350Sedwin	FILE		*f;
839198350Sedwin	char		path_zoneinfo_file[MAXPATHLEN];
840198350Sedwin
841198350Sedwin	sprintf(path_zoneinfo_file, "%s/%s", path_zoneinfo, zoneinfo);
842198350Sedwin	rv = install_zoneinfo_file(path_zoneinfo_file);
843198350Sedwin
844198267Sedwin	/* Save knowledge for later */
845230296Semaste	if (reallydoit && (rv & DITEM_FAILURE) == 0) {
846230296Semaste		if ((f = fopen(path_db, "w")) != NULL) {
847230296Semaste			fprintf(f, "%s\n", zoneinfo);
848230296Semaste			fclose(f);
849230296Semaste		}
850198267Sedwin	}
851198267Sedwin
852198350Sedwin	return (rv);
85319872Swollman}
85419872Swollman
85519872Swollmanstatic int
85619872Swollmanconfirm_zone(const char *filename)
85719872Swollman{
858179530Sjkim	char		title[64], prompt[64];
859179530Sjkim	time_t		t = time(0);
860179530Sjkim	struct tm	*tm;
861179530Sjkim	int		rv;
862227934Sfjoe
863220172Sedwin	setenv("TZ", filename == NULL ? "" : filename, 1);
86419872Swollman	tzset();
86519872Swollman	tm = localtime(&t);
86619872Swollman
867179530Sjkim	snprintf(title, sizeof(title), "Confirmation");
868179530Sjkim	snprintf(prompt, sizeof(prompt),
869179530Sjkim	    "Does the abbreviation `%s' look reasonable?", tm->tm_zone);
870179530Sjkim	rv = !dialog_yesno(title, prompt, 5, 72);
871179530Sjkim	return (rv);
87219872Swollman}
87319872Swollman
87419872Swollmanstatic int
87519872Swollmanset_zone_multi(dialogMenuItem *dmi)
87619872Swollman{
877179530Sjkim	struct zone	*zp = dmi->data;
878179530Sjkim	int		rv;
87919872Swollman
88019872Swollman	if (!confirm_zone(zp->filename))
881179530Sjkim		return (DITEM_FAILURE | DITEM_RECREATE);
88219872Swollman
883198350Sedwin	rv = install_zoneinfo(zp->filename);
884179530Sjkim	return (rv);
88519872Swollman}
88619872Swollman
88719872Swollmanstatic int
88819872Swollmanset_zone_whole_country(dialogMenuItem *dmi)
88919872Swollman{
890179530Sjkim	struct country	*cp = dmi->data;
891179530Sjkim	int		rv;
89219872Swollman
89319872Swollman	if (!confirm_zone(cp->filename))
894179530Sjkim		return (DITEM_FAILURE | DITEM_RECREATE);
89519872Swollman
896198350Sedwin	rv = install_zoneinfo(cp->filename);
897179530Sjkim	return (rv);
89819872Swollman}
89919872Swollman
90030763Scharnierstatic void
901179530Sjkimusage(void)
90230763Scharnier{
903179530Sjkim
904222139Sru	fprintf(stderr, "usage: tzsetup [-nrs] [-C chroot_directory]"
905222139Sru	    " [zoneinfo_file | zoneinfo_name]\n");
90630763Scharnier	exit(1);
90730763Scharnier}
90830763Scharnier
90919872Swollmanint
91019872Swollmanmain(int argc, char **argv)
91119872Swollman{
912179530Sjkim	char		title[64], prompt[128];
913198267Sedwin	int		c, fd, rv, skiputc;
914274394Sdteske        char		vm_guest[16] = "";
915274394Sdteske        size_t		len = sizeof(vm_guest);
91619872Swollman
917195339Sattilio	skiputc = 0;
918274394Sdteske
919274394Sdteske	/* Default skiputc to 1 for VM guests */
920274394Sdteske        if (sysctlbyname("kern.vm_guest", vm_guest, &len, NULL, 0) == 0 &&
921274394Sdteske	    strcmp(vm_guest, "none") != 0)
922274394Sdteske                skiputc = 1;
923274394Sdteske
924198350Sedwin	while ((c = getopt(argc, argv, "C:nrs")) != -1) {
92519872Swollman		switch(c) {
926198350Sedwin		case 'C':
927198350Sedwin			chrootenv = optarg;
928198350Sedwin			break;
92919872Swollman		case 'n':
93019872Swollman			reallydoit = 0;
93119872Swollman			break;
932198267Sedwin		case 'r':
933198267Sedwin			reinstall = 1;
934198350Sedwin			usedialog = 0;
935198267Sedwin			break;
936195339Sattilio		case 's':
937195339Sattilio			skiputc = 1;
938195339Sattilio			break;
93919872Swollman		default:
94030763Scharnier			usage();
94119872Swollman		}
94219872Swollman	}
94319872Swollman
94443544Swollman	if (argc - optind > 1)
94530763Scharnier		usage();
94619872Swollman
947198350Sedwin	if (chrootenv == NULL) {
948198350Sedwin		strcpy(path_zonetab, _PATH_ZONETAB);
949198350Sedwin		strcpy(path_iso3166, _PATH_ISO3166);
950198350Sedwin		strcpy(path_zoneinfo, _PATH_ZONEINFO);
951198350Sedwin		strcpy(path_localtime, _PATH_LOCALTIME);
952198350Sedwin		strcpy(path_db, _PATH_DB);
953198350Sedwin		strcpy(path_wall_cmos_clock, _PATH_WALL_CMOS_CLOCK);
954198350Sedwin	} else {
955198350Sedwin		sprintf(path_zonetab, "%s/%s", chrootenv, _PATH_ZONETAB);
956198350Sedwin		sprintf(path_iso3166, "%s/%s", chrootenv, _PATH_ISO3166);
957198350Sedwin		sprintf(path_zoneinfo, "%s/%s", chrootenv, _PATH_ZONEINFO);
958198350Sedwin		sprintf(path_localtime, "%s/%s", chrootenv, _PATH_LOCALTIME);
959198350Sedwin		sprintf(path_db, "%s/%s", chrootenv, _PATH_DB);
960198350Sedwin		sprintf(path_wall_cmos_clock, "%s/%s", chrootenv,
961198350Sedwin		    _PATH_WALL_CMOS_CLOCK);
962198350Sedwin	}
963198350Sedwin
964198350Sedwin
96549435Sru	/* Override the user-supplied umask. */
966179530Sjkim	(void)umask(S_IWGRP | S_IWOTH);
96749435Sru
968198267Sedwin	if (reinstall == 1) {
969198267Sedwin		FILE *f;
970230520Semaste		char zoneinfo[MAXPATHLEN];
971198267Sedwin
972198350Sedwin		if ((f = fopen(path_db, "r")) != NULL) {
973230520Semaste			if (fgets(zoneinfo, sizeof(zoneinfo), f) != NULL) {
974230520Semaste				zoneinfo[sizeof(zoneinfo) - 1] = 0;
975230520Semaste				if (strlen(zoneinfo) > 0) {
976230520Semaste					zoneinfo[strlen(zoneinfo) - 1] = 0;
977230520Semaste					rv = install_zoneinfo(zoneinfo);
978198267Sedwin					exit(rv & ~DITEM_LEAVE_MENU);
979198267Sedwin				}
980198350Sedwin				errx(1, "Error reading %s.\n", path_db);
981198267Sedwin			}
982198267Sedwin			fclose(f);
983198267Sedwin			errx(1,
984198267Sedwin			    "Unable to determine earlier installed zoneinfo "
985230520Semaste			    "name. Check %s", path_db);
986198267Sedwin		}
987198350Sedwin		errx(1, "Cannot open %s for reading. Does it exist?", path_db);
988198267Sedwin	}
989198267Sedwin
990198350Sedwin	/*
991198350Sedwin	 * If the arguments on the command-line do not specify a file,
992198350Sedwin	 * then interpret it as a zoneinfo name
993198350Sedwin	 */
994198350Sedwin	if (optind == argc - 1) {
995198350Sedwin		struct stat sb;
996198350Sedwin
997198350Sedwin		if (stat(argv[optind], &sb) != 0) {
998198350Sedwin			usedialog = 0;
999198350Sedwin			rv = install_zoneinfo(argv[optind]);
1000198350Sedwin			exit(rv & ~DITEM_LEAVE_MENU);
1001198350Sedwin		}
1002198350Sedwin		/* FALLTHROUGH */
1003198350Sedwin	}
1004198350Sedwin
1005230520Semaste	read_iso3166_table();
1006230520Semaste	read_zones();
1007230520Semaste	sort_countries();
1008230520Semaste	make_menus();
1009230520Semaste
1010227934Sfjoe	init_dialog(stdin, stdout);
1011195339Sattilio	if (skiputc == 0) {
1012227934Sfjoe		DIALOG_VARS save_vars;
1013227934Sfjoe		int yesno;
1014227934Sfjoe
1015195339Sattilio		snprintf(title, sizeof(title),
1016195339Sattilio		    "Select local or UTC (Greenwich Mean Time) clock");
1017195339Sattilio		snprintf(prompt, sizeof(prompt),
1018195339Sattilio		    "Is this machine's CMOS clock set to UTC?  "
1019195339Sattilio		    "If it is set to local time,\n"
1020195339Sattilio		    "or you don't know, please choose NO here!");
1021227934Sfjoe		dlg_save_vars(&save_vars);
1022227934Sfjoe#if !defined(__sparc64__)
1023227934Sfjoe		dialog_vars.defaultno = TRUE;
1024227934Sfjoe#endif
1025227934Sfjoe		yesno = dialog_yesno(title, prompt, 7, 73);
1026227934Sfjoe		dlg_restore_vars(&save_vars);
1027227934Sfjoe		if (!yesno) {
1028195339Sattilio			if (reallydoit)
1029210243Snork				unlink(path_wall_cmos_clock);
1030195339Sattilio		} else {
1031195339Sattilio			if (reallydoit) {
1032210243Snork				fd = open(path_wall_cmos_clock,
1033195339Sattilio				    O_WRONLY | O_CREAT | O_TRUNC,
1034195339Sattilio				    S_IRUSR | S_IRGRP | S_IROTH);
1035198254Sedwin				if (fd < 0) {
1036198254Sedwin					end_dialog();
1037195339Sattilio					err(1, "create %s",
1038210243Snork					    path_wall_cmos_clock);
1039198254Sedwin				}
1040195339Sattilio				close(fd);
1041195339Sattilio			}
104241852Speter		}
1043227934Sfjoe		dlg_clear();
104432394Ssteve	}
104543544Swollman	if (optind == argc - 1) {
1046179530Sjkim		snprintf(title, sizeof(title), "Default timezone provided");
1047179530Sjkim		snprintf(prompt, sizeof(prompt),
1048179530Sjkim		    "\nUse the default `%s' zone?", argv[optind]);
1049179530Sjkim		if (!dialog_yesno(title, prompt, 7, 72)) {
1050198350Sedwin			rv = install_zoneinfo_file(argv[optind]);
1051227934Sfjoe			dlg_clear();
105243544Swollman			end_dialog();
1053198267Sedwin			exit(rv & ~DITEM_LEAVE_MENU);
105443544Swollman		}
1055227934Sfjoe		dlg_clear();
105643544Swollman	}
1057179530Sjkim	snprintf(title, sizeof(title), "Time Zone Selector");
1058179530Sjkim	snprintf(prompt, sizeof(prompt), "Select a region");
1059227934Sfjoe	xdialog_menu(title, prompt, -1, -1, NCONTINENTS, NCONTINENTS,
1060227934Sfjoe	    continents);
106143544Swollman
1062227934Sfjoe	dlg_clear();
106319872Swollman	end_dialog();
1064179530Sjkim	return (0);
106519872Swollman}
1066