tzsetup.c revision 208830
1/*
2 * Copyright 1996 Massachusetts Institute of Technology
3 *
4 * Permission to use, copy, modify, and distribute this software and
5 * its documentation for any purpose and without fee is hereby
6 * granted, provided that both the above copyright notice and this
7 * permission notice appear in all copies, that both the above
8 * copyright notice and this permission notice appear in all
9 * supporting documentation, and that the name of M.I.T. not be used
10 * in advertising or publicity pertaining to distribution of the
11 * software without specific, written prior permission.  M.I.T. makes
12 * no representations about the suitability of this software for any
13 * purpose.  It is provided "as is" without express or implied
14 * warranty.
15 *
16 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
17 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
20 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30/*
31 * Second attempt at a `tzmenu' program, using the separate description
32 * files provided in newer tzdata releases.
33 */
34
35#include <sys/cdefs.h>
36__FBSDID("$FreeBSD: head/usr.sbin/tzsetup/tzsetup.c 208830 2010-06-05 12:49:39Z edwin $");
37
38#include <dialog.h>
39#include <err.h>
40#include <errno.h>
41#include <stdio.h>
42#include <stdlib.h>
43#include <string.h>
44#include <time.h>
45#include <unistd.h>
46
47#include <sys/fcntl.h>
48#include <sys/param.h>
49#include <sys/queue.h>
50#include <sys/stat.h>
51
52#define	_PATH_ZONETAB		"/usr/share/zoneinfo/zone.tab"
53#define	_PATH_ISO3166		"/usr/share/misc/iso3166"
54#define	_PATH_ZONEINFO		"/usr/share/zoneinfo"
55#define	_PATH_LOCALTIME		"/etc/localtime"
56#define	_PATH_DB		"/var/db/zoneinfo"
57#define	_PATH_WALL_CMOS_CLOCK	"/etc/wall_cmos_clock"
58
59static char	path_zonetab[MAXPATHLEN], path_iso3166[MAXPATHLEN],
60		path_zoneinfo[MAXPATHLEN], path_localtime[MAXPATHLEN],
61		path_db[MAXPATHLEN], path_wall_cmos_clock[MAXPATHLEN];
62
63static int reallydoit = 1;
64static int reinstall = 0;
65static int usedialog = 1;
66static char *chrootenv = NULL;
67
68static void	usage(void);
69static int	continent_country_menu(dialogMenuItem *);
70static int	set_zone_multi(dialogMenuItem *);
71static int	set_zone_whole_country(dialogMenuItem *);
72static int	set_zone_menu(dialogMenuItem *);
73
74struct continent {
75	dialogMenuItem *menu;
76	int		nitems;
77	int		ch;
78	int		sc;
79};
80
81static struct continent	africa, america, antarctica, arctic, asia, atlantic;
82static struct continent	australia, europe, indian, pacific;
83
84static struct continent_names {
85	const char	*name;
86	struct continent *continent;
87} continent_names[] = {
88	{ "Africa",	&africa },
89	{ "America",	&america },
90	{ "Antarctica",	&antarctica },
91	{ "Arctic",	&arctic },
92	{ "Asia",	&asia },
93	{ "Atlantic",	&atlantic },
94	{ "Australia",	&australia },
95	{ "Europe",	&europe },
96	{ "Indian",	&indian },
97	{ "Pacific",	&pacific }
98};
99
100static struct continent_items {
101	char		prompt[2];
102	char		title[30];
103} continent_items[] = {
104	{ "1",	"Africa" },
105	{ "2",	"America -- North and South" },
106	{ "3",	"Antarctica" },
107	{ "4",	"Arctic Ocean" },
108	{ "5",	"Asia" },
109	{ "6",	"Atlantic Ocean" },
110	{ "7",	"Australia" },
111	{ "8",	"Europe" },
112	{ "9",	"Indian Ocean" },
113	{ "0",	"Pacific Ocean" }
114};
115
116#define	NCONTINENTS	\
117    (int)((sizeof(continent_items)) / (sizeof(continent_items[0])))
118static dialogMenuItem continents[NCONTINENTS];
119
120#define	OCEANP(x)	((x) == 3 || (x) == 5 || (x) == 8 || (x) == 9)
121
122static int
123continent_country_menu(dialogMenuItem *continent)
124{
125	char		title[64], prompt[64];
126	struct continent *contp = continent->data;
127	int		isocean = OCEANP(continent - continents);
128	int		menulen;
129	int		rv;
130
131	/* Short cut -- if there's only one country, don't post a menu. */
132	if (contp->nitems == 1)
133		return (contp->menu[0].fire(&contp->menu[0]));
134
135	/* It's amazing how much good grammar really matters... */
136	if (!isocean) {
137		snprintf(title, sizeof(title), "Countries in %s",
138		    continent->title);
139		snprintf(prompt, sizeof(prompt), "Select a country or region");
140	} else {
141		snprintf(title, sizeof(title), "Islands and groups in the %s",
142		    continent->title);
143		snprintf(prompt, sizeof(prompt), "Select an island or group");
144	}
145
146	menulen = contp->nitems < 16 ? contp->nitems : 16;
147	rv = dialog_menu(title, prompt, -1, -1, menulen, -contp->nitems,
148	    contp->menu, 0, &contp->ch, &contp->sc);
149	if (rv == 0)
150		return (DITEM_LEAVE_MENU);
151	return (DITEM_RECREATE);
152}
153
154static struct continent *
155find_continent(const char *name)
156{
157	int		i;
158
159	for (i = 0; i < NCONTINENTS; i++)
160		if (strcmp(name, continent_names[i].name) == 0)
161			return (continent_names[i].continent);
162	return (0);
163}
164
165struct country {
166	char		*name;
167	char		*tlc;
168	int		nzones;
169	char		*filename;	/* use iff nzones < 0 */
170	struct continent *continent;	/* use iff nzones < 0 */
171	TAILQ_HEAD(, zone) zones;	/* use iff nzones > 0 */
172	dialogMenuItem	*submenu;	/* use iff nzones > 0 */
173};
174
175struct zone {
176	TAILQ_ENTRY(zone) link;
177	char		*descr;
178	char		*filename;
179	struct continent *continent;
180};
181
182/*
183 * This is the easiest organization... we use ISO 3166 country codes,
184 * of the two-letter variety, so we just size this array to suit.
185 * Beats worrying about dynamic allocation.
186 */
187#define	NCOUNTRIES	(26 * 26)
188static struct country countries[NCOUNTRIES];
189
190#define	CODE2INT(s)	((s[0] - 'A') * 26 + (s[1] - 'A'))
191
192/*
193 * Read the ISO 3166 country code database in _PATH_ISO3166
194 * (/usr/share/misc/iso3166).  On error, exit via err(3).
195 */
196static void
197read_iso3166_table(void)
198{
199	FILE		*fp;
200	struct country	*cp;
201	size_t		len;
202	char		*s, *t, *name;
203	int		lineno;
204
205	fp = fopen(path_iso3166, "r");
206	if (!fp)
207		err(1, path_iso3166);
208	lineno = 0;
209
210	while ((s = fgetln(fp, &len)) != 0) {
211		lineno++;
212		if (s[len - 1] != '\n')
213			errx(1, "%s:%d: invalid format", path_iso3166, lineno);
214		s[len - 1] = '\0';
215		if (s[0] == '#' || strspn(s, " \t") == len - 1)
216			continue;
217
218		/* Isolate the two-letter code. */
219		t = strsep(&s, "\t");
220		if (t == 0 || strlen(t) != 2)
221			errx(1, "%s:%d: invalid format", path_iso3166, lineno);
222		if (t[0] < 'A' || t[0] > 'Z' || t[1] < 'A' || t[1] > 'Z')
223			errx(1, "%s:%d: invalid code `%s'", path_iso3166,
224			    lineno, t);
225
226		/* Now skip past the three-letter and numeric codes. */
227		name = strsep(&s, "\t");	/* 3-let */
228		if (name == 0 || strlen(name) != 3)
229			errx(1, "%s:%d: invalid format", path_iso3166, lineno);
230		name = strsep(&s, "\t");	/* numeric */
231		if (name == 0 || strlen(name) != 3)
232			errx(1, "%s:%d: invalid format", path_iso3166, lineno);
233
234		name = s;
235
236		cp = &countries[CODE2INT(t)];
237		if (cp->name)
238			errx(1, "%s:%d: country code `%s' multiply defined: %s",
239			    path_iso3166, lineno, t, cp->name);
240		cp->name = strdup(name);
241		if (cp->name == NULL)
242			errx(1, "malloc failed");
243		cp->tlc = strdup(t);
244		if (cp->tlc == NULL)
245			errx(1, "malloc failed");
246	}
247
248	fclose(fp);
249}
250
251static void
252add_zone_to_country(int lineno, const char *tlc, const char *descr,
253    const char *file, struct continent *cont)
254{
255	struct zone	*zp;
256	struct country	*cp;
257
258	if (tlc[0] < 'A' || tlc[0] > 'Z' || tlc[1] < 'A' || tlc[1] > 'Z')
259		errx(1, "%s:%d: country code `%s' invalid", path_zonetab,
260		    lineno, tlc);
261
262	cp = &countries[CODE2INT(tlc)];
263	if (cp->name == 0)
264		errx(1, "%s:%d: country code `%s' unknown", path_zonetab,
265		    lineno, tlc);
266
267	if (descr) {
268		if (cp->nzones < 0)
269			errx(1, "%s:%d: conflicting zone definition",
270			    path_zonetab, lineno);
271
272		zp = malloc(sizeof(*zp));
273		if (zp == 0)
274			errx(1, "malloc(%zu)", sizeof(*zp));
275
276		if (cp->nzones == 0)
277			TAILQ_INIT(&cp->zones);
278
279		zp->descr = strdup(descr);
280		if (zp->descr == NULL)
281			errx(1, "malloc failed");
282		zp->filename = strdup(file);
283		if (zp->filename == NULL)
284			errx(1, "malloc failed");
285		zp->continent = cont;
286		TAILQ_INSERT_TAIL(&cp->zones, zp, link);
287		cp->nzones++;
288	} else {
289		if (cp->nzones > 0)
290			errx(1, "%s:%d: zone must have description",
291			    path_zonetab, lineno);
292		if (cp->nzones < 0)
293			errx(1, "%s:%d: zone multiply defined",
294			    path_zonetab, lineno);
295		cp->nzones = -1;
296		cp->filename = strdup(file);
297		if (cp->filename == NULL)
298			errx(1, "malloc failed");
299		cp->continent = cont;
300	}
301}
302
303/*
304 * This comparison function intentionally sorts all of the null-named
305 * ``countries''---i.e., the codes that don't correspond to a real
306 * country---to the end.  Everything else is lexical by country name.
307 */
308static int
309compare_countries(const void *xa, const void *xb)
310{
311	const struct country *a = xa, *b = xb;
312
313	if (a->name == 0 && b->name == 0)
314		return (0);
315	if (a->name == 0 && b->name != 0)
316		return (1);
317	if (b->name == 0)
318		return (-1);
319
320	return (strcmp(a->name, b->name));
321}
322
323/*
324 * This must be done AFTER all zone descriptions are read, since it breaks
325 * CODE2INT().
326 */
327static void
328sort_countries(void)
329{
330
331	qsort(countries, NCOUNTRIES, sizeof(countries[0]), compare_countries);
332}
333
334static void
335read_zones(void)
336{
337	char		contbuf[16];
338	FILE		*fp;
339	struct continent *cont;
340	size_t		len;
341	char		*line, *tlc, *coord, *file, *descr, *p;
342	int		lineno;
343
344	fp = fopen(path_zonetab, "r");
345	if (!fp)
346		err(1, path_zonetab);
347	lineno = 0;
348
349	while ((line = fgetln(fp, &len)) != 0) {
350		lineno++;
351		if (line[len - 1] != '\n')
352			errx(1, "%s:%d: invalid format", path_zonetab, lineno);
353		line[len - 1] = '\0';
354		if (line[0] == '#')
355			continue;
356
357		tlc = strsep(&line, "\t");
358		if (strlen(tlc) != 2)
359			errx(1, "%s:%d: invalid country code `%s'",
360			    path_zonetab, lineno, tlc);
361		coord = strsep(&line, "\t");
362		file = strsep(&line, "\t");
363		p = strchr(file, '/');
364		if (p == 0)
365			errx(1, "%s:%d: invalid zone name `%s'", path_zonetab,
366			    lineno, file);
367		contbuf[0] = '\0';
368		strncat(contbuf, file, p - file);
369		cont = find_continent(contbuf);
370		if (!cont)
371			errx(1, "%s:%d: invalid region `%s'", path_zonetab,
372			    lineno, contbuf);
373
374		descr = (line != NULL && *line != '\0') ? line : NULL;
375
376		add_zone_to_country(lineno, tlc, descr, file, cont);
377	}
378	fclose(fp);
379}
380
381static void
382make_menus(void)
383{
384	struct country	*cp;
385	struct zone	*zp, *zp2;
386	struct continent *cont;
387	dialogMenuItem	*dmi;
388	int		i;
389
390	/*
391	 * First, count up all the countries in each continent/ocean.
392	 * Be careful to count those countries which have multiple zones
393	 * only once for each.  NB: some countries are in multiple
394	 * continents/oceans.
395	 */
396	for (cp = countries; cp->name; cp++) {
397		if (cp->nzones == 0)
398			continue;
399		if (cp->nzones < 0) {
400			cp->continent->nitems++;
401		} else {
402			TAILQ_FOREACH(zp, &cp->zones, link) {
403				cont = zp->continent;
404				for (zp2 = TAILQ_FIRST(&cp->zones);
405				    zp2->continent != cont;
406				    zp2 = TAILQ_NEXT(zp2, link))
407					;
408				if (zp2 == zp)
409					zp->continent->nitems++;
410			}
411		}
412	}
413
414	/*
415	 * Now allocate memory for the country menus and initialize
416	 * continent menus.  We set nitems back to zero so that we can
417	 * use it for counting again when we actually build the menus.
418	 */
419	memset(continents, 0, sizeof(continents));
420	for (i = 0; i < NCONTINENTS; i++) {
421		continent_names[i].continent->menu =
422		    malloc(sizeof(dialogMenuItem) *
423		    continent_names[i].continent->nitems);
424		if (continent_names[i].continent->menu == 0)
425			errx(1, "malloc for continent menu");
426		continent_names[i].continent->nitems = 0;
427		continents[i].prompt = continent_items[i].prompt;
428		continents[i].title = continent_items[i].title;
429		continents[i].fire = continent_country_menu;
430		continents[i].data = continent_names[i].continent;
431	}
432
433	/*
434	 * Now that memory is allocated, create the menu items for
435	 * each continent.  For multiple-zone countries, also create
436	 * the country's zone submenu.
437	 */
438	for (cp = countries; cp->name; cp++) {
439		if (cp->nzones == 0)
440			continue;
441		if (cp->nzones < 0) {
442			dmi = &cp->continent->menu[cp->continent->nitems];
443			memset(dmi, 0, sizeof(*dmi));
444			asprintf(&dmi->prompt, "%d", ++cp->continent->nitems);
445			dmi->title = cp->name;
446			dmi->checked = 0;
447			dmi->fire = set_zone_whole_country;
448			dmi->selected = 0;
449			dmi->data = cp;
450		} else {
451			cp->submenu = malloc(cp->nzones * sizeof(*dmi));
452			if (cp->submenu == 0)
453				errx(1, "malloc for submenu");
454			cp->nzones = 0;
455			TAILQ_FOREACH(zp, &cp->zones, link) {
456				cont = zp->continent;
457				dmi = &cp->submenu[cp->nzones];
458				memset(dmi, 0, sizeof(*dmi));
459				asprintf(&dmi->prompt, "%d", ++cp->nzones);
460				dmi->title = zp->descr;
461				dmi->checked = 0;
462				dmi->fire = set_zone_multi;
463				dmi->selected = 0;
464				dmi->data = zp;
465
466				for (zp2 = TAILQ_FIRST(&cp->zones);
467				    zp2->continent != cont;
468				    zp2 = TAILQ_NEXT(zp2, link))
469					;
470				if (zp2 != zp)
471					continue;
472
473				dmi = &cont->menu[cont->nitems];
474				memset(dmi, 0, sizeof(*dmi));
475				asprintf(&dmi->prompt, "%d", ++cont->nitems);
476				dmi->title = cp->name;
477				dmi->checked = 0;
478				dmi->fire = set_zone_menu;
479				dmi->selected = 0;
480				dmi->data = cp;
481			}
482		}
483	}
484}
485
486static int
487set_zone_menu(dialogMenuItem *dmi)
488{
489	char		title[64], prompt[64];
490	struct country	*cp = dmi->data;
491	int		menulen;
492	int		rv;
493
494	snprintf(title, sizeof(title), "%s Time Zones", cp->name);
495	snprintf(prompt, sizeof(prompt),
496	    "Select a zone which observes the same time as your locality.");
497	menulen = cp->nzones < 16 ? cp->nzones : 16;
498	rv = dialog_menu(title, prompt, -1, -1, menulen, -cp->nzones,
499	    cp->submenu, 0, 0, 0);
500	if (rv != 0)
501		return (DITEM_RECREATE);
502	return (DITEM_LEAVE_MENU);
503}
504
505static int
506install_zoneinfo_file(const char *zoneinfo_file)
507{
508	char		buf[1024];
509	char		title[64], prompt[64];
510	struct stat	sb;
511	ssize_t		len;
512	int		fd1, fd2, copymode;
513
514	if (lstat(path_localtime, &sb) < 0) {
515		/* Nothing there yet... */
516		copymode = 1;
517	} else if (S_ISLNK(sb.st_mode))
518		copymode = 0;
519	else
520		copymode = 1;
521
522#ifdef VERBOSE
523	if (copymode)
524		snprintf(prompt, sizeof(prompt),
525		    "Copying %s to %s", zoneinfo_file, path_localtime);
526	else
527		snprintf(prompt, sizeof(prompt),
528		    "Creating symbolic link %s to %s",
529		    path_localtime, zoneinfo_file);
530	if (usedialog)
531		dialog_notify(prompt);
532	else
533		fprintf(stderr, "%s\n", prompt);
534#endif
535
536	if (reallydoit) {
537		if (copymode) {
538			fd1 = open(zoneinfo_file, O_RDONLY, 0);
539			if (fd1 < 0) {
540				snprintf(title, sizeof(title), "Error");
541				snprintf(prompt, sizeof(prompt),
542				    "Could not open %s: %s", zoneinfo_file,
543				    strerror(errno));
544				if (usedialog)
545					dialog_mesgbox(title, prompt, 8, 72);
546				else
547					fprintf(stderr, "%s\n", prompt);
548				return (DITEM_FAILURE | DITEM_RECREATE);
549			}
550
551			unlink(path_localtime);
552			fd2 = open(path_localtime, O_CREAT | O_EXCL | O_WRONLY,
553			    S_IRUSR | S_IRGRP | S_IROTH);
554			if (fd2 < 0) {
555				snprintf(title, sizeof(title), "Error");
556				snprintf(prompt, sizeof(prompt),
557				    "Could not open %s: %s",
558				    path_localtime, strerror(errno));
559				if (usedialog)
560					dialog_mesgbox(title, prompt, 8, 72);
561				else
562					fprintf(stderr, "%s\n", prompt);
563				return (DITEM_FAILURE | DITEM_RECREATE);
564			}
565
566			while ((len = read(fd1, buf, sizeof(buf))) > 0)
567				if ((len = write(fd2, buf, len)) < 0)
568					break;
569
570			if (len == -1) {
571				snprintf(title, sizeof(title), "Error");
572				snprintf(prompt, sizeof(prompt),
573				    "Error copying %s to %s %s", zoneinfo_file,
574				    path_localtime, strerror(errno));
575				if (usedialog)
576					dialog_mesgbox(title, prompt, 8, 72);
577				else
578					fprintf(stderr, "%s\n", prompt);
579				/* Better to leave none than a corrupt one. */
580				unlink(path_localtime);
581				return (DITEM_FAILURE | DITEM_RECREATE);
582			}
583			close(fd1);
584			close(fd2);
585		} else {
586			if (access(zoneinfo_file, R_OK) != 0) {
587				snprintf(title, sizeof(title), "Error");
588				snprintf(prompt, sizeof(prompt),
589				    "Cannot access %s: %s", zoneinfo_file,
590				    strerror(errno));
591				if (usedialog)
592					dialog_mesgbox(title, prompt, 8, 72);
593				else
594					fprintf(stderr, "%s\n", prompt);
595				return (DITEM_FAILURE | DITEM_RECREATE);
596			}
597			unlink(path_localtime);
598			if (symlink(zoneinfo_file, path_localtime) < 0) {
599				snprintf(title, sizeof(title), "Error");
600				snprintf(prompt, sizeof(prompt),
601				    "Cannot create symbolic link %s to %s: %s",
602				    path_localtime, zoneinfo_file,
603				    strerror(errno));
604				if (usedialog)
605					dialog_mesgbox(title, prompt, 8, 72);
606				else
607					fprintf(stderr, "%s\n", prompt);
608				return (DITEM_FAILURE | DITEM_RECREATE);
609			}
610		}
611	}
612
613#ifdef VERBOSE
614	snprintf(title, sizeof(title), "Done");
615	if (copymode)
616		snprintf(prompt, sizeof(prompt),
617		    "Copied timezone file from %s to %s", zoneinfo_file,
618		    path_localtime);
619	else
620		snprintf(prompt, sizeof(prompt),
621		    "Created symbolic link from %s to %s", zoneinfo_file,
622		    path_localtime);
623	if (usedialog)
624		dialog_mesgbox(title, prompt, 8, 72);
625	else
626		fprintf(stderr, "%s\n", prompt);
627#endif
628
629	return (DITEM_LEAVE_MENU);
630}
631
632static int
633install_zoneinfo(const char *zoneinfo)
634{
635	int		rv;
636	FILE		*f;
637	char		path_zoneinfo_file[MAXPATHLEN];
638
639	sprintf(path_zoneinfo_file, "%s/%s", path_zoneinfo, zoneinfo);
640	rv = install_zoneinfo_file(path_zoneinfo_file);
641
642	/* Save knowledge for later */
643	if ((f = fopen(path_db, "w")) != NULL) {
644		fprintf(f, "%s\n", zoneinfo);
645		fclose(f);
646	}
647
648	return (rv);
649}
650
651static int
652confirm_zone(const char *filename)
653{
654	char		title[64], prompt[64];
655	time_t		t = time(0);
656	struct tm	*tm;
657	int		rv;
658
659	setenv("TZ", filename, 1);
660	tzset();
661	tm = localtime(&t);
662
663	snprintf(title, sizeof(title), "Confirmation");
664	snprintf(prompt, sizeof(prompt),
665	    "Does the abbreviation `%s' look reasonable?", tm->tm_zone);
666	rv = !dialog_yesno(title, prompt, 5, 72);
667	return (rv);
668}
669
670static int
671set_zone_multi(dialogMenuItem *dmi)
672{
673	struct zone	*zp = dmi->data;
674	int		rv;
675
676	if (!confirm_zone(zp->filename))
677		return (DITEM_FAILURE | DITEM_RECREATE);
678
679	rv = install_zoneinfo(zp->filename);
680	return (rv);
681}
682
683static int
684set_zone_whole_country(dialogMenuItem *dmi)
685{
686	struct country	*cp = dmi->data;
687	int		rv;
688
689	if (!confirm_zone(cp->filename))
690		return (DITEM_FAILURE | DITEM_RECREATE);
691
692	rv = install_zoneinfo(cp->filename);
693	return (rv);
694}
695
696static void
697usage(void)
698{
699
700	fprintf(stderr, "usage: tzsetup [-nrs] [zoneinfo file]\n");
701	exit(1);
702}
703
704#if defined(__sparc64__)
705#define	DIALOG_UTC	dialog_yesno
706#else
707#define	DIALOG_UTC	dialog_noyes
708#endif
709
710int
711main(int argc, char **argv)
712{
713	char		title[64], prompt[128];
714	int		c, fd, rv, skiputc;
715
716	skiputc = 0;
717	while ((c = getopt(argc, argv, "C:nrs")) != -1) {
718		switch(c) {
719		case 'C':
720			chrootenv = optarg;
721			break;
722		case 'n':
723			reallydoit = 0;
724			break;
725		case 'r':
726			reinstall = 1;
727			usedialog = 0;
728			break;
729		case 's':
730			skiputc = 1;
731			break;
732		default:
733			usage();
734		}
735	}
736
737	if (argc - optind > 1)
738		usage();
739
740	if (chrootenv == NULL) {
741		strcpy(path_zonetab, _PATH_ZONETAB);
742		strcpy(path_iso3166, _PATH_ISO3166);
743		strcpy(path_zoneinfo, _PATH_ZONEINFO);
744		strcpy(path_localtime, _PATH_LOCALTIME);
745		strcpy(path_db, _PATH_DB);
746		strcpy(path_wall_cmos_clock, _PATH_WALL_CMOS_CLOCK);
747	} else {
748		sprintf(path_zonetab, "%s/%s", chrootenv, _PATH_ZONETAB);
749		sprintf(path_iso3166, "%s/%s", chrootenv, _PATH_ISO3166);
750		sprintf(path_zoneinfo, "%s/%s", chrootenv, _PATH_ZONEINFO);
751		sprintf(path_localtime, "%s/%s", chrootenv, _PATH_LOCALTIME);
752		sprintf(path_db, "%s/%s", chrootenv, _PATH_DB);
753		sprintf(path_wall_cmos_clock, "%s/%s", chrootenv,
754		    _PATH_WALL_CMOS_CLOCK);
755	}
756
757
758	/* Override the user-supplied umask. */
759	(void)umask(S_IWGRP | S_IWOTH);
760
761	read_iso3166_table();
762	read_zones();
763	sort_countries();
764	make_menus();
765
766	if (reinstall == 1) {
767		FILE *f;
768		char zonefile[MAXPATHLEN];
769		char path_db[MAXPATHLEN];
770
771		zonefile[0] = '\0';
772		path_db[0] = '\0';
773		if (chrootenv != NULL) {
774			sprintf(zonefile, "%s/", chrootenv);
775			sprintf(path_db, "%s/", chrootenv);
776		}
777		strcat(zonefile, _PATH_ZONEINFO);
778		strcat(zonefile, "/");
779		strcat(path_db, _PATH_DB);
780
781		if ((f = fopen(path_db, "r")) != NULL) {
782			if (fgets(zonefile, sizeof(zonefile), f) != NULL) {
783				zonefile[sizeof(zonefile) - 1] = 0;
784				if (strlen(zonefile) > 0) {
785					zonefile[strlen(zonefile) - 1] = 0;
786					rv = install_zoneinfo(zonefile);
787					exit(rv & ~DITEM_LEAVE_MENU);
788				}
789				errx(1, "Error reading %s.\n", path_db);
790			}
791			fclose(f);
792			errx(1,
793			    "Unable to determine earlier installed zoneinfo "
794			    "file. Check %s", path_db);
795		}
796		errx(1, "Cannot open %s for reading. Does it exist?", path_db);
797	}
798
799	/*
800	 * If the arguments on the command-line do not specify a file,
801	 * then interpret it as a zoneinfo name
802	 */
803	if (optind == argc - 1) {
804		struct stat sb;
805
806		if (stat(argv[optind], &sb) != 0) {
807			usedialog = 0;
808			rv = install_zoneinfo(argv[optind]);
809			exit(rv & ~DITEM_LEAVE_MENU);
810		}
811		/* FALLTHROUGH */
812	}
813
814	init_dialog();
815	if (skiputc == 0) {
816		snprintf(title, sizeof(title),
817		    "Select local or UTC (Greenwich Mean Time) clock");
818		snprintf(prompt, sizeof(prompt),
819		    "Is this machine's CMOS clock set to UTC?  "
820		    "If it is set to local time,\n"
821		    "or you don't know, please choose NO here!");
822		if (!DIALOG_UTC(title, prompt, 7, 72)) {
823			if (reallydoit)
824				unlink(_PATH_WALL_CMOS_CLOCK);
825		} else {
826			if (reallydoit) {
827				fd = open(_PATH_WALL_CMOS_CLOCK,
828				    O_WRONLY | O_CREAT | O_TRUNC,
829				    S_IRUSR | S_IRGRP | S_IROTH);
830				if (fd < 0) {
831					end_dialog();
832					err(1, "create %s",
833					    _PATH_WALL_CMOS_CLOCK);
834				}
835				close(fd);
836			}
837		}
838		dialog_clear_norefresh();
839	}
840	if (optind == argc - 1) {
841		snprintf(title, sizeof(title), "Default timezone provided");
842		snprintf(prompt, sizeof(prompt),
843		    "\nUse the default `%s' zone?", argv[optind]);
844		if (!dialog_yesno(title, prompt, 7, 72)) {
845			rv = install_zoneinfo_file(argv[optind]);
846			dialog_clear();
847			end_dialog();
848			exit(rv & ~DITEM_LEAVE_MENU);
849		}
850		dialog_clear_norefresh();
851	}
852	snprintf(title, sizeof(title), "Time Zone Selector");
853	snprintf(prompt, sizeof(prompt), "Select a region");
854	dialog_menu(title, prompt, -1, -1, NCONTINENTS, -NCONTINENTS,
855	    continents, 0, NULL, NULL);
856
857	dialog_clear();
858	end_dialog();
859	return (0);
860}
861