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