1996-11-19 18:09:41 +00:00
|
|
|
/*
|
|
|
|
* Copyright 1996 Massachusetts Institute of Technology
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software and
|
|
|
|
* its documentation for any purpose and without fee is hereby
|
|
|
|
* granted, provided that both the above copyright notice and this
|
|
|
|
* permission notice appear in all copies, that both the above
|
|
|
|
* copyright notice and this permission notice appear in all
|
|
|
|
* supporting documentation, and that the name of M.I.T. not be used
|
|
|
|
* in advertising or publicity pertaining to distribution of the
|
|
|
|
* software without specific, written prior permission. M.I.T. makes
|
|
|
|
* no representations about the suitability of this software for any
|
|
|
|
* purpose. It is provided "as is" without express or implied
|
|
|
|
* warranty.
|
2008-06-03 22:34:52 +00:00
|
|
|
*
|
1996-11-19 18:09:41 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS
|
|
|
|
* ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
|
|
|
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
|
|
|
|
* SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
|
|
|
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
|
|
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Second attempt at a `tzmenu' program, using the separate description
|
|
|
|
* files provided in newer tzdata releases.
|
|
|
|
*/
|
|
|
|
|
2008-06-03 22:34:52 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
1997-10-27 07:49:47 +00:00
|
|
|
|
1996-11-19 18:09:41 +00:00
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2000-10-10 01:50:26 +00:00
|
|
|
#include <time.h>
|
1996-11-19 18:09:41 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <sys/fcntl.h>
|
2009-10-20 06:54:31 +00:00
|
|
|
#include <sys/param.h>
|
1996-11-19 18:09:41 +00:00
|
|
|
#include <sys/queue.h>
|
|
|
|
#include <sys/stat.h>
|
2014-11-11 19:37:17 +00:00
|
|
|
#include <sys/sysctl.h>
|
1996-11-19 18:09:41 +00:00
|
|
|
|
2011-11-24 13:24:06 +00:00
|
|
|
#include <dialog.h>
|
|
|
|
|
2008-06-03 22:34:52 +00:00
|
|
|
#define _PATH_ZONETAB "/usr/share/zoneinfo/zone.tab"
|
|
|
|
#define _PATH_ISO3166 "/usr/share/misc/iso3166"
|
|
|
|
#define _PATH_ZONEINFO "/usr/share/zoneinfo"
|
|
|
|
#define _PATH_LOCALTIME "/etc/localtime"
|
2009-10-20 06:54:31 +00:00
|
|
|
#define _PATH_DB "/var/db/zoneinfo"
|
2008-06-03 22:34:52 +00:00
|
|
|
#define _PATH_WALL_CMOS_CLOCK "/etc/wall_cmos_clock"
|
1996-11-19 18:09:41 +00:00
|
|
|
|
2012-01-12 05:50:32 +00:00
|
|
|
#ifdef PATH_MAX
|
|
|
|
#define SILLY_BUFFER_SIZE 2*PATH_MAX
|
|
|
|
#else
|
|
|
|
#warning "Somebody needs to fix this to dynamically size this buffer."
|
|
|
|
#define SILLY_BUFFER_SIZE 2048
|
|
|
|
#endif
|
|
|
|
|
2011-11-24 13:24:06 +00:00
|
|
|
/* special return codes for `fire' actions */
|
|
|
|
#define DITEM_FAILURE 1
|
|
|
|
|
|
|
|
/* flags - returned in upper 16 bits of return status */
|
|
|
|
#define DITEM_LEAVE_MENU (1 << 16)
|
|
|
|
#define DITEM_RECREATE (1 << 18)
|
|
|
|
|
|
|
|
/* for use in describing more exotic behaviors */
|
|
|
|
typedef struct dialogMenuItem {
|
|
|
|
char *prompt;
|
|
|
|
char *title;
|
|
|
|
int (*fire)(struct dialogMenuItem *self);
|
|
|
|
void *data;
|
|
|
|
} dialogMenuItem;
|
|
|
|
|
2011-12-01 09:02:57 +00:00
|
|
|
static int
|
|
|
|
xdialog_count_rows(const char *p)
|
|
|
|
{
|
|
|
|
int rows = 0;
|
|
|
|
|
|
|
|
while ((p = strchr(p, '\n')) != NULL) {
|
|
|
|
p++;
|
|
|
|
if (*p == '\0')
|
|
|
|
break;
|
|
|
|
rows++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rows ? rows : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
xdialog_count_columns(const char *p)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
int max_len = 0;
|
|
|
|
const char *q;
|
|
|
|
|
|
|
|
for (; (q = strchr(p, '\n')) != NULL; p = q + 1) {
|
|
|
|
len = q - p;
|
|
|
|
max_len = MAX(max_len, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
len = strlen(p);
|
|
|
|
max_len = MAX(max_len, len);
|
|
|
|
return max_len;
|
|
|
|
}
|
|
|
|
|
2011-11-24 13:24:06 +00:00
|
|
|
static int
|
|
|
|
xdialog_menu(const char *title, const char *cprompt, int height, int width,
|
|
|
|
int menu_height, int item_no, dialogMenuItem *ditems)
|
|
|
|
{
|
2011-11-24 18:45:23 +00:00
|
|
|
int i, result, choice = 0;
|
2011-11-24 13:24:06 +00:00
|
|
|
DIALOG_LISTITEM *listitems;
|
|
|
|
DIALOG_VARS save_vars;
|
|
|
|
|
|
|
|
dlg_save_vars(&save_vars);
|
|
|
|
|
|
|
|
/* initialize list items */
|
2011-11-24 18:45:23 +00:00
|
|
|
listitems = dlg_calloc(DIALOG_LISTITEM, item_no + 1);
|
2011-11-24 13:24:06 +00:00
|
|
|
assert_ptr(listitems, "xdialog_menu");
|
|
|
|
for (i = 0; i < item_no; i++) {
|
|
|
|
listitems[i].name = ditems[i].prompt;
|
|
|
|
listitems[i].text = ditems[i].title;
|
|
|
|
}
|
|
|
|
|
2011-12-01 09:02:57 +00:00
|
|
|
/* calculate height */
|
|
|
|
if (height < 0)
|
|
|
|
height = xdialog_count_rows(cprompt) + menu_height + 4 + 2;
|
|
|
|
if (height > LINES)
|
|
|
|
height = LINES;
|
|
|
|
|
2011-11-24 13:24:06 +00:00
|
|
|
/* calculate width */
|
|
|
|
if (width < 0) {
|
|
|
|
int tag_x = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < item_no; i++) {
|
|
|
|
int j, l;
|
|
|
|
|
|
|
|
l = strlen(listitems[i].name);
|
|
|
|
for (j = 0; j < item_no; j++) {
|
|
|
|
int k = strlen(listitems[j].text);
|
|
|
|
tag_x = MAX(tag_x, l + k + 2);
|
|
|
|
}
|
|
|
|
}
|
2011-12-01 09:02:57 +00:00
|
|
|
width = MAX(xdialog_count_columns(cprompt), title != NULL ? xdialog_count_columns(title) : 0);
|
2011-11-24 13:24:06 +00:00
|
|
|
width = MAX(width, tag_x + 4) + 4;
|
|
|
|
}
|
|
|
|
width = MAX(width, 24);
|
|
|
|
if (width > COLS)
|
|
|
|
width = COLS;
|
|
|
|
|
|
|
|
again:
|
2011-11-24 18:45:23 +00:00
|
|
|
dialog_vars.default_item = listitems[choice].name;
|
2011-11-24 13:24:06 +00:00
|
|
|
result = dlg_menu(title, cprompt, height, width,
|
|
|
|
menu_height, item_no, listitems, &choice, NULL);
|
|
|
|
switch (result) {
|
|
|
|
case DLG_EXIT_ESC:
|
|
|
|
result = -1;
|
|
|
|
break;
|
|
|
|
case DLG_EXIT_OK:
|
|
|
|
if (ditems[choice].fire != NULL) {
|
|
|
|
int status;
|
|
|
|
|
|
|
|
status = ditems[choice].fire(ditems + choice);
|
|
|
|
if (status & DITEM_RECREATE) {
|
|
|
|
dlg_clear();
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result = 0;
|
|
|
|
break;
|
|
|
|
case DLG_EXIT_CANCEL:
|
|
|
|
default:
|
|
|
|
result = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(listitems);
|
|
|
|
dlg_restore_vars(&save_vars);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-10-21 20:55:04 +00:00
|
|
|
static char path_zonetab[MAXPATHLEN], path_iso3166[MAXPATHLEN],
|
2011-11-24 13:24:06 +00:00
|
|
|
path_zoneinfo[MAXPATHLEN], path_localtime[MAXPATHLEN],
|
2009-10-21 20:55:04 +00:00
|
|
|
path_db[MAXPATHLEN], path_wall_cmos_clock[MAXPATHLEN];
|
|
|
|
|
1996-11-19 18:09:41 +00:00
|
|
|
static int reallydoit = 1;
|
2009-10-20 06:54:31 +00:00
|
|
|
static int reinstall = 0;
|
2009-10-21 20:55:04 +00:00
|
|
|
static int usedialog = 1;
|
|
|
|
static char *chrootenv = NULL;
|
1996-11-19 18:09:41 +00:00
|
|
|
|
2008-06-03 22:34:52 +00:00
|
|
|
static void usage(void);
|
2011-03-30 21:33:23 +00:00
|
|
|
static int confirm_zone(const char *filename);
|
2008-06-03 22:34:52 +00:00
|
|
|
static int continent_country_menu(dialogMenuItem *);
|
2011-03-30 21:33:23 +00:00
|
|
|
static int install_zoneinfo_file(const char *zoneinfo_file);
|
2008-06-03 22:34:52 +00:00
|
|
|
static int set_zone_multi(dialogMenuItem *);
|
|
|
|
static int set_zone_whole_country(dialogMenuItem *);
|
|
|
|
static int set_zone_menu(dialogMenuItem *);
|
2011-03-30 21:33:23 +00:00
|
|
|
static int set_zone_utc(void);
|
1996-11-19 18:09:41 +00:00
|
|
|
|
|
|
|
struct continent {
|
|
|
|
dialogMenuItem *menu;
|
2008-06-03 22:34:52 +00:00
|
|
|
int nitems;
|
1996-11-19 18:09:41 +00:00
|
|
|
};
|
|
|
|
|
2008-06-03 22:34:52 +00:00
|
|
|
static struct continent africa, america, antarctica, arctic, asia, atlantic;
|
2011-03-30 21:33:23 +00:00
|
|
|
static struct continent australia, europe, indian, pacific, utc;
|
1996-11-19 18:09:41 +00:00
|
|
|
|
|
|
|
static struct continent_names {
|
2008-06-03 22:34:52 +00:00
|
|
|
const char *name;
|
1996-11-19 18:09:41 +00:00
|
|
|
struct continent *continent;
|
|
|
|
} continent_names[] = {
|
2008-06-03 22:34:52 +00:00
|
|
|
{ "Africa", &africa },
|
|
|
|
{ "America", &america },
|
|
|
|
{ "Antarctica", &antarctica },
|
|
|
|
{ "Arctic", &arctic },
|
|
|
|
{ "Asia", &asia },
|
|
|
|
{ "Atlantic", &atlantic },
|
|
|
|
{ "Australia", &australia },
|
|
|
|
{ "Europe", &europe },
|
|
|
|
{ "Indian", &indian },
|
2011-03-30 21:33:23 +00:00
|
|
|
{ "Pacific", &pacific },
|
2011-11-24 13:24:06 +00:00
|
|
|
{ "UTC", &utc }
|
1996-11-19 18:09:41 +00:00
|
|
|
};
|
|
|
|
|
2008-06-03 22:34:52 +00:00
|
|
|
static struct continent_items {
|
|
|
|
char prompt[2];
|
|
|
|
char title[30];
|
|
|
|
} continent_items[] = {
|
|
|
|
{ "1", "Africa" },
|
|
|
|
{ "2", "America -- North and South" },
|
|
|
|
{ "3", "Antarctica" },
|
|
|
|
{ "4", "Arctic Ocean" },
|
|
|
|
{ "5", "Asia" },
|
|
|
|
{ "6", "Atlantic Ocean" },
|
|
|
|
{ "7", "Australia" },
|
|
|
|
{ "8", "Europe" },
|
|
|
|
{ "9", "Indian Ocean" },
|
2011-03-30 21:33:23 +00:00
|
|
|
{ "0", "Pacific Ocean" },
|
|
|
|
{ "a", "UTC" }
|
1996-11-19 18:09:41 +00:00
|
|
|
};
|
2008-06-03 22:34:52 +00:00
|
|
|
|
|
|
|
#define NCONTINENTS \
|
|
|
|
(int)((sizeof(continent_items)) / (sizeof(continent_items[0])))
|
|
|
|
static dialogMenuItem continents[NCONTINENTS];
|
|
|
|
|
|
|
|
#define OCEANP(x) ((x) == 3 || (x) == 5 || (x) == 8 || (x) == 9)
|
1996-11-19 18:09:41 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
continent_country_menu(dialogMenuItem *continent)
|
|
|
|
{
|
2008-06-03 22:34:52 +00:00
|
|
|
char title[64], prompt[64];
|
1996-11-19 18:09:41 +00:00
|
|
|
struct continent *contp = continent->data;
|
2008-06-03 22:34:52 +00:00
|
|
|
int isocean = OCEANP(continent - continents);
|
|
|
|
int menulen;
|
|
|
|
int rv;
|
1996-11-19 18:09:41 +00:00
|
|
|
|
2011-03-30 21:33:23 +00:00
|
|
|
if (strcmp(continent->title, "UTC") == 0)
|
2011-11-24 13:24:06 +00:00
|
|
|
return set_zone_utc();
|
2011-03-30 21:33:23 +00:00
|
|
|
|
1996-11-19 18:09:41 +00:00
|
|
|
/* Short cut -- if there's only one country, don't post a menu. */
|
2008-06-02 23:37:33 +00:00
|
|
|
if (contp->nitems == 1)
|
|
|
|
return (contp->menu[0].fire(&contp->menu[0]));
|
1996-11-19 18:09:41 +00:00
|
|
|
|
|
|
|
/* It's amazing how much good grammar really matters... */
|
2008-06-03 22:34:52 +00:00
|
|
|
if (!isocean) {
|
|
|
|
snprintf(title, sizeof(title), "Countries in %s",
|
|
|
|
continent->title);
|
|
|
|
snprintf(prompt, sizeof(prompt), "Select a country or region");
|
|
|
|
} else {
|
|
|
|
snprintf(title, sizeof(title), "Islands and groups in the %s",
|
|
|
|
continent->title);
|
|
|
|
snprintf(prompt, sizeof(prompt), "Select an island or group");
|
|
|
|
}
|
1996-11-19 18:09:41 +00:00
|
|
|
|
|
|
|
menulen = contp->nitems < 16 ? contp->nitems : 16;
|
2011-11-24 13:24:06 +00:00
|
|
|
rv = xdialog_menu(title, prompt, -1, -1, menulen, contp->nitems,
|
|
|
|
contp->menu);
|
1996-11-19 18:09:41 +00:00
|
|
|
if (rv == 0)
|
2008-06-03 22:34:52 +00:00
|
|
|
return (DITEM_LEAVE_MENU);
|
|
|
|
return (DITEM_RECREATE);
|
1996-11-19 18:09:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct continent *
|
|
|
|
find_continent(const char *name)
|
|
|
|
{
|
2008-06-03 22:34:52 +00:00
|
|
|
int i;
|
1996-11-19 18:09:41 +00:00
|
|
|
|
2008-06-03 22:34:52 +00:00
|
|
|
for (i = 0; i < NCONTINENTS; i++)
|
1996-11-19 18:09:41 +00:00
|
|
|
if (strcmp(name, continent_names[i].name) == 0)
|
2008-06-03 22:34:52 +00:00
|
|
|
return (continent_names[i].continent);
|
|
|
|
return (0);
|
1996-11-19 18:09:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct country {
|
2008-06-03 22:34:52 +00:00
|
|
|
char *name;
|
|
|
|
char *tlc;
|
|
|
|
int nzones;
|
|
|
|
char *filename; /* use iff nzones < 0 */
|
|
|
|
struct continent *continent; /* use iff nzones < 0 */
|
|
|
|
TAILQ_HEAD(, zone) zones; /* use iff nzones > 0 */
|
|
|
|
dialogMenuItem *submenu; /* use iff nzones > 0 */
|
1996-11-19 18:09:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct zone {
|
2000-05-26 02:09:24 +00:00
|
|
|
TAILQ_ENTRY(zone) link;
|
2008-06-03 22:34:52 +00:00
|
|
|
char *descr;
|
|
|
|
char *filename;
|
1996-11-19 18:09:41 +00:00
|
|
|
struct continent *continent;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is the easiest organization... we use ISO 3166 country codes,
|
|
|
|
* of the two-letter variety, so we just size this array to suit.
|
|
|
|
* Beats worrying about dynamic allocation.
|
|
|
|
*/
|
2008-06-03 22:34:52 +00:00
|
|
|
#define NCOUNTRIES (26 * 26)
|
1997-02-01 17:03:10 +00:00
|
|
|
static struct country countries[NCOUNTRIES];
|
2008-06-03 22:34:52 +00:00
|
|
|
|
|
|
|
#define CODE2INT(s) ((s[0] - 'A') * 26 + (s[1] - 'A'))
|
1996-11-19 18:09:41 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Read the ISO 3166 country code database in _PATH_ISO3166
|
|
|
|
* (/usr/share/misc/iso3166). On error, exit via err(3).
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
read_iso3166_table(void)
|
|
|
|
{
|
2008-06-03 22:34:52 +00:00
|
|
|
FILE *fp;
|
|
|
|
struct country *cp;
|
|
|
|
size_t len;
|
|
|
|
char *s, *t, *name;
|
|
|
|
int lineno;
|
1996-11-19 18:09:41 +00:00
|
|
|
|
2009-10-21 20:55:04 +00:00
|
|
|
fp = fopen(path_iso3166, "r");
|
1996-11-19 18:09:41 +00:00
|
|
|
if (!fp)
|
2010-06-14 23:51:35 +00:00
|
|
|
err(1, "%s", path_iso3166);
|
1996-11-19 18:09:41 +00:00
|
|
|
lineno = 0;
|
|
|
|
|
|
|
|
while ((s = fgetln(fp, &len)) != 0) {
|
|
|
|
lineno++;
|
|
|
|
if (s[len - 1] != '\n')
|
2009-10-21 20:55:04 +00:00
|
|
|
errx(1, "%s:%d: invalid format", path_iso3166, lineno);
|
1996-11-19 18:09:41 +00:00
|
|
|
s[len - 1] = '\0';
|
1997-11-07 00:07:27 +00:00
|
|
|
if (s[0] == '#' || strspn(s, " \t") == len - 1)
|
1996-11-19 18:09:41 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Isolate the two-letter code. */
|
|
|
|
t = strsep(&s, "\t");
|
|
|
|
if (t == 0 || strlen(t) != 2)
|
2009-10-21 20:55:04 +00:00
|
|
|
errx(1, "%s:%d: invalid format", path_iso3166, lineno);
|
1996-11-19 18:09:41 +00:00
|
|
|
if (t[0] < 'A' || t[0] > 'Z' || t[1] < 'A' || t[1] > 'Z')
|
2009-10-21 20:55:04 +00:00
|
|
|
errx(1, "%s:%d: invalid code `%s'", path_iso3166,
|
2008-06-03 22:34:52 +00:00
|
|
|
lineno, t);
|
1996-11-19 18:09:41 +00:00
|
|
|
|
|
|
|
/* Now skip past the three-letter and numeric codes. */
|
2008-06-03 22:34:52 +00:00
|
|
|
name = strsep(&s, "\t"); /* 3-let */
|
1996-11-19 18:09:41 +00:00
|
|
|
if (name == 0 || strlen(name) != 3)
|
2009-10-21 20:55:04 +00:00
|
|
|
errx(1, "%s:%d: invalid format", path_iso3166, lineno);
|
2008-06-03 22:34:52 +00:00
|
|
|
name = strsep(&s, "\t"); /* numeric */
|
1996-11-19 18:09:41 +00:00
|
|
|
if (name == 0 || strlen(name) != 3)
|
2009-10-21 20:55:04 +00:00
|
|
|
errx(1, "%s:%d: invalid format", path_iso3166, lineno);
|
1996-11-19 18:09:41 +00:00
|
|
|
|
|
|
|
name = s;
|
|
|
|
|
|
|
|
cp = &countries[CODE2INT(t)];
|
|
|
|
if (cp->name)
|
2009-10-21 20:55:04 +00:00
|
|
|
errx(1, "%s:%d: country code `%s' multiply defined: %s",
|
|
|
|
path_iso3166, lineno, t, cp->name);
|
1996-11-19 18:09:41 +00:00
|
|
|
cp->name = strdup(name);
|
2000-01-23 20:25:01 +00:00
|
|
|
if (cp->name == NULL)
|
|
|
|
errx(1, "malloc failed");
|
1996-11-19 18:09:41 +00:00
|
|
|
cp->tlc = strdup(t);
|
2000-01-23 20:25:01 +00:00
|
|
|
if (cp->tlc == NULL)
|
|
|
|
errx(1, "malloc failed");
|
1996-11-19 18:09:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
add_zone_to_country(int lineno, const char *tlc, const char *descr,
|
2008-06-03 22:34:52 +00:00
|
|
|
const char *file, struct continent *cont)
|
1996-11-19 18:09:41 +00:00
|
|
|
{
|
2008-06-03 22:34:52 +00:00
|
|
|
struct zone *zp;
|
|
|
|
struct country *cp;
|
1996-11-19 18:09:41 +00:00
|
|
|
|
|
|
|
if (tlc[0] < 'A' || tlc[0] > 'Z' || tlc[1] < 'A' || tlc[1] > 'Z')
|
2009-10-21 20:55:04 +00:00
|
|
|
errx(1, "%s:%d: country code `%s' invalid", path_zonetab,
|
2008-06-03 22:34:52 +00:00
|
|
|
lineno, tlc);
|
2011-11-24 13:24:06 +00:00
|
|
|
|
1996-11-19 18:09:41 +00:00
|
|
|
cp = &countries[CODE2INT(tlc)];
|
|
|
|
if (cp->name == 0)
|
2009-10-21 20:55:04 +00:00
|
|
|
errx(1, "%s:%d: country code `%s' unknown", path_zonetab,
|
2008-06-03 22:34:52 +00:00
|
|
|
lineno, tlc);
|
1996-11-19 18:09:41 +00:00
|
|
|
|
|
|
|
if (descr) {
|
|
|
|
if (cp->nzones < 0)
|
2009-10-21 20:55:04 +00:00
|
|
|
errx(1, "%s:%d: conflicting zone definition",
|
|
|
|
path_zonetab, lineno);
|
1996-11-19 18:09:41 +00:00
|
|
|
|
2008-06-03 22:34:52 +00:00
|
|
|
zp = malloc(sizeof(*zp));
|
1996-11-19 18:09:41 +00:00
|
|
|
if (zp == 0)
|
2008-06-03 22:34:52 +00:00
|
|
|
errx(1, "malloc(%zu)", sizeof(*zp));
|
2011-11-24 13:24:06 +00:00
|
|
|
|
1996-11-19 18:09:41 +00:00
|
|
|
if (cp->nzones == 0)
|
|
|
|
TAILQ_INIT(&cp->zones);
|
|
|
|
|
|
|
|
zp->descr = strdup(descr);
|
2000-01-23 20:25:01 +00:00
|
|
|
if (zp->descr == NULL)
|
|
|
|
errx(1, "malloc failed");
|
1996-11-19 18:09:41 +00:00
|
|
|
zp->filename = strdup(file);
|
2000-01-23 20:25:01 +00:00
|
|
|
if (zp->filename == NULL)
|
|
|
|
errx(1, "malloc failed");
|
1996-11-19 18:09:41 +00:00
|
|
|
zp->continent = cont;
|
|
|
|
TAILQ_INSERT_TAIL(&cp->zones, zp, link);
|
|
|
|
cp->nzones++;
|
|
|
|
} else {
|
|
|
|
if (cp->nzones > 0)
|
2009-10-21 20:55:04 +00:00
|
|
|
errx(1, "%s:%d: zone must have description",
|
|
|
|
path_zonetab, lineno);
|
1996-11-19 18:09:41 +00:00
|
|
|
if (cp->nzones < 0)
|
2009-10-21 20:55:04 +00:00
|
|
|
errx(1, "%s:%d: zone multiply defined",
|
|
|
|
path_zonetab, lineno);
|
1996-11-19 18:09:41 +00:00
|
|
|
cp->nzones = -1;
|
|
|
|
cp->filename = strdup(file);
|
2000-01-23 20:25:01 +00:00
|
|
|
if (cp->filename == NULL)
|
|
|
|
errx(1, "malloc failed");
|
1996-11-19 18:09:41 +00:00
|
|
|
cp->continent = cont;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This comparison function intentionally sorts all of the null-named
|
|
|
|
* ``countries''---i.e., the codes that don't correspond to a real
|
|
|
|
* country---to the end. Everything else is lexical by country name.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
compare_countries(const void *xa, const void *xb)
|
|
|
|
{
|
|
|
|
const struct country *a = xa, *b = xb;
|
|
|
|
|
|
|
|
if (a->name == 0 && b->name == 0)
|
2008-06-03 22:34:52 +00:00
|
|
|
return (0);
|
1996-11-19 18:09:41 +00:00
|
|
|
if (a->name == 0 && b->name != 0)
|
2008-06-03 22:34:52 +00:00
|
|
|
return (1);
|
1996-11-19 18:09:41 +00:00
|
|
|
if (b->name == 0)
|
2008-06-03 22:34:52 +00:00
|
|
|
return (-1);
|
1996-11-19 18:09:41 +00:00
|
|
|
|
2008-06-03 22:34:52 +00:00
|
|
|
return (strcmp(a->name, b->name));
|
1996-11-19 18:09:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This must be done AFTER all zone descriptions are read, since it breaks
|
|
|
|
* CODE2INT().
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
sort_countries(void)
|
|
|
|
{
|
2008-06-03 22:34:52 +00:00
|
|
|
|
|
|
|
qsort(countries, NCOUNTRIES, sizeof(countries[0]), compare_countries);
|
1996-11-19 18:09:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
read_zones(void)
|
|
|
|
{
|
2008-06-03 22:34:52 +00:00
|
|
|
char contbuf[16];
|
|
|
|
FILE *fp;
|
1996-11-19 18:09:41 +00:00
|
|
|
struct continent *cont;
|
2008-06-03 22:34:52 +00:00
|
|
|
size_t len;
|
|
|
|
char *line, *tlc, *coord, *file, *descr, *p;
|
|
|
|
int lineno;
|
1996-11-19 18:09:41 +00:00
|
|
|
|
2009-10-21 20:55:04 +00:00
|
|
|
fp = fopen(path_zonetab, "r");
|
1996-11-19 18:09:41 +00:00
|
|
|
if (!fp)
|
2010-06-14 23:51:35 +00:00
|
|
|
err(1, "%s", path_zonetab);
|
1996-11-19 18:09:41 +00:00
|
|
|
lineno = 0;
|
|
|
|
|
|
|
|
while ((line = fgetln(fp, &len)) != 0) {
|
|
|
|
lineno++;
|
|
|
|
if (line[len - 1] != '\n')
|
2009-10-21 20:55:04 +00:00
|
|
|
errx(1, "%s:%d: invalid format", path_zonetab, lineno);
|
1996-11-19 18:09:41 +00:00
|
|
|
line[len - 1] = '\0';
|
|
|
|
if (line[0] == '#')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
tlc = strsep(&line, "\t");
|
|
|
|
if (strlen(tlc) != 2)
|
2009-10-21 20:55:04 +00:00
|
|
|
errx(1, "%s:%d: invalid country code `%s'",
|
|
|
|
path_zonetab, lineno, tlc);
|
2010-06-05 12:53:44 +00:00
|
|
|
coord = strsep(&line, "\t"); /* Unused */
|
1996-11-19 18:09:41 +00:00
|
|
|
file = strsep(&line, "\t");
|
|
|
|
p = strchr(file, '/');
|
|
|
|
if (p == 0)
|
2009-10-21 20:55:04 +00:00
|
|
|
errx(1, "%s:%d: invalid zone name `%s'", path_zonetab,
|
2008-06-03 22:34:52 +00:00
|
|
|
lineno, file);
|
1996-11-19 18:09:41 +00:00
|
|
|
contbuf[0] = '\0';
|
|
|
|
strncat(contbuf, file, p - file);
|
|
|
|
cont = find_continent(contbuf);
|
|
|
|
if (!cont)
|
2009-10-21 20:55:04 +00:00
|
|
|
errx(1, "%s:%d: invalid region `%s'", path_zonetab,
|
2008-06-03 22:34:52 +00:00
|
|
|
lineno, contbuf);
|
1996-11-19 18:09:41 +00:00
|
|
|
|
2008-06-03 22:34:52 +00:00
|
|
|
descr = (line != NULL && *line != '\0') ? line : NULL;
|
1996-11-19 18:09:41 +00:00
|
|
|
|
|
|
|
add_zone_to_country(lineno, tlc, descr, file, cont);
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
make_menus(void)
|
|
|
|
{
|
2008-06-03 22:34:52 +00:00
|
|
|
struct country *cp;
|
|
|
|
struct zone *zp, *zp2;
|
1996-11-19 18:09:41 +00:00
|
|
|
struct continent *cont;
|
2008-06-03 22:34:52 +00:00
|
|
|
dialogMenuItem *dmi;
|
|
|
|
int i;
|
1996-11-19 18:09:41 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* First, count up all the countries in each continent/ocean.
|
|
|
|
* Be careful to count those countries which have multiple zones
|
|
|
|
* only once for each. NB: some countries are in multiple
|
|
|
|
* continents/oceans.
|
|
|
|
*/
|
|
|
|
for (cp = countries; cp->name; cp++) {
|
|
|
|
if (cp->nzones == 0)
|
|
|
|
continue;
|
|
|
|
if (cp->nzones < 0) {
|
|
|
|
cp->continent->nitems++;
|
|
|
|
} else {
|
2000-12-29 18:04:54 +00:00
|
|
|
TAILQ_FOREACH(zp, &cp->zones, link) {
|
1996-11-19 18:09:41 +00:00
|
|
|
cont = zp->continent;
|
2000-12-29 18:04:54 +00:00
|
|
|
for (zp2 = TAILQ_FIRST(&cp->zones);
|
2008-06-03 22:34:52 +00:00
|
|
|
zp2->continent != cont;
|
|
|
|
zp2 = TAILQ_NEXT(zp2, link))
|
1996-11-19 18:09:41 +00:00
|
|
|
;
|
|
|
|
if (zp2 == zp)
|
|
|
|
zp->continent->nitems++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2008-06-03 22:34:52 +00:00
|
|
|
* Now allocate memory for the country menus and initialize
|
|
|
|
* continent menus. We set nitems back to zero so that we can
|
|
|
|
* use it for counting again when we actually build the menus.
|
1996-11-19 18:09:41 +00:00
|
|
|
*/
|
2008-06-03 22:34:52 +00:00
|
|
|
memset(continents, 0, sizeof(continents));
|
1996-11-19 18:09:41 +00:00
|
|
|
for (i = 0; i < NCONTINENTS; i++) {
|
|
|
|
continent_names[i].continent->menu =
|
2008-06-03 22:34:52 +00:00
|
|
|
malloc(sizeof(dialogMenuItem) *
|
|
|
|
continent_names[i].continent->nitems);
|
1996-11-19 18:09:41 +00:00
|
|
|
if (continent_names[i].continent->menu == 0)
|
2000-01-23 20:25:01 +00:00
|
|
|
errx(1, "malloc for continent menu");
|
1996-11-19 18:09:41 +00:00
|
|
|
continent_names[i].continent->nitems = 0;
|
2008-06-03 22:34:52 +00:00
|
|
|
continents[i].prompt = continent_items[i].prompt;
|
|
|
|
continents[i].title = continent_items[i].title;
|
|
|
|
continents[i].fire = continent_country_menu;
|
|
|
|
continents[i].data = continent_names[i].continent;
|
1996-11-19 18:09:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now that memory is allocated, create the menu items for
|
|
|
|
* each continent. For multiple-zone countries, also create
|
|
|
|
* the country's zone submenu.
|
|
|
|
*/
|
|
|
|
for (cp = countries; cp->name; cp++) {
|
|
|
|
if (cp->nzones == 0)
|
|
|
|
continue;
|
|
|
|
if (cp->nzones < 0) {
|
|
|
|
dmi = &cp->continent->menu[cp->continent->nitems];
|
2008-06-03 22:34:52 +00:00
|
|
|
memset(dmi, 0, sizeof(*dmi));
|
|
|
|
asprintf(&dmi->prompt, "%d", ++cp->continent->nitems);
|
1996-11-19 18:09:41 +00:00
|
|
|
dmi->title = cp->name;
|
|
|
|
dmi->fire = set_zone_whole_country;
|
|
|
|
dmi->data = cp;
|
|
|
|
} else {
|
2008-06-03 22:34:52 +00:00
|
|
|
cp->submenu = malloc(cp->nzones * sizeof(*dmi));
|
1996-11-19 18:09:41 +00:00
|
|
|
if (cp->submenu == 0)
|
2000-01-23 20:25:01 +00:00
|
|
|
errx(1, "malloc for submenu");
|
1996-11-19 18:09:41 +00:00
|
|
|
cp->nzones = 0;
|
2000-12-29 18:04:54 +00:00
|
|
|
TAILQ_FOREACH(zp, &cp->zones, link) {
|
1996-11-19 18:09:41 +00:00
|
|
|
cont = zp->continent;
|
|
|
|
dmi = &cp->submenu[cp->nzones];
|
2008-06-03 22:34:52 +00:00
|
|
|
memset(dmi, 0, sizeof(*dmi));
|
|
|
|
asprintf(&dmi->prompt, "%d", ++cp->nzones);
|
1996-11-19 18:09:41 +00:00
|
|
|
dmi->title = zp->descr;
|
|
|
|
dmi->fire = set_zone_multi;
|
|
|
|
dmi->data = zp;
|
|
|
|
|
2000-12-29 18:04:54 +00:00
|
|
|
for (zp2 = TAILQ_FIRST(&cp->zones);
|
2008-06-03 22:34:52 +00:00
|
|
|
zp2->continent != cont;
|
|
|
|
zp2 = TAILQ_NEXT(zp2, link))
|
1996-11-19 18:09:41 +00:00
|
|
|
;
|
|
|
|
if (zp2 != zp)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
dmi = &cont->menu[cont->nitems];
|
2008-06-03 22:34:52 +00:00
|
|
|
memset(dmi, 0, sizeof(*dmi));
|
1996-11-19 18:09:41 +00:00
|
|
|
asprintf(&dmi->prompt, "%d", ++cont->nitems);
|
|
|
|
dmi->title = cp->name;
|
|
|
|
dmi->fire = set_zone_menu;
|
|
|
|
dmi->data = cp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
set_zone_menu(dialogMenuItem *dmi)
|
|
|
|
{
|
2008-06-03 22:34:52 +00:00
|
|
|
char title[64], prompt[64];
|
|
|
|
struct country *cp = dmi->data;
|
|
|
|
int menulen;
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
snprintf(title, sizeof(title), "%s Time Zones", cp->name);
|
|
|
|
snprintf(prompt, sizeof(prompt),
|
|
|
|
"Select a zone which observes the same time as your locality.");
|
1996-11-19 18:09:41 +00:00
|
|
|
menulen = cp->nzones < 16 ? cp->nzones : 16;
|
2011-11-24 13:24:06 +00:00
|
|
|
rv = xdialog_menu(title, prompt, -1, -1, menulen, cp->nzones,
|
|
|
|
cp->submenu);
|
1996-11-19 18:09:41 +00:00
|
|
|
if (rv != 0)
|
2008-06-03 22:34:52 +00:00
|
|
|
return (DITEM_RECREATE);
|
|
|
|
return (DITEM_LEAVE_MENU);
|
1996-11-19 18:09:41 +00:00
|
|
|
}
|
|
|
|
|
2011-03-30 21:33:23 +00:00
|
|
|
int
|
|
|
|
set_zone_utc(void)
|
|
|
|
{
|
|
|
|
if (!confirm_zone(NULL))
|
|
|
|
return (DITEM_FAILURE | DITEM_RECREATE);
|
2011-11-24 13:24:06 +00:00
|
|
|
|
2011-03-30 21:33:23 +00:00
|
|
|
return (install_zoneinfo_file(NULL));
|
|
|
|
}
|
|
|
|
|
1996-11-19 18:09:41 +00:00
|
|
|
static int
|
2009-10-21 20:55:04 +00:00
|
|
|
install_zoneinfo_file(const char *zoneinfo_file)
|
1996-11-19 18:09:41 +00:00
|
|
|
{
|
2008-06-03 22:34:52 +00:00
|
|
|
char buf[1024];
|
2012-01-12 05:50:32 +00:00
|
|
|
char title[64], prompt[SILLY_BUFFER_SIZE];
|
2008-06-03 22:34:52 +00:00
|
|
|
struct stat sb;
|
|
|
|
ssize_t len;
|
|
|
|
int fd1, fd2, copymode;
|
|
|
|
|
2009-10-21 20:55:04 +00:00
|
|
|
if (lstat(path_localtime, &sb) < 0) {
|
1996-11-19 18:09:41 +00:00
|
|
|
/* Nothing there yet... */
|
|
|
|
copymode = 1;
|
2008-06-03 22:34:52 +00:00
|
|
|
} else if (S_ISLNK(sb.st_mode))
|
1996-11-19 18:09:41 +00:00
|
|
|
copymode = 0;
|
|
|
|
else
|
|
|
|
copymode = 1;
|
|
|
|
|
1997-01-21 10:53:38 +00:00
|
|
|
#ifdef VERBOSE
|
2012-01-18 04:37:35 +00:00
|
|
|
snprintf(title, sizeof(title), "Info");
|
|
|
|
if (zoneinfo_file == NULL)
|
|
|
|
snprintf(prompt, sizeof(prompt),
|
|
|
|
"Removing %s", path_localtime);
|
|
|
|
else if (copymode)
|
2008-06-03 22:34:52 +00:00
|
|
|
snprintf(prompt, sizeof(prompt),
|
2009-10-21 20:55:04 +00:00
|
|
|
"Copying %s to %s", zoneinfo_file, path_localtime);
|
1996-11-19 18:09:41 +00:00
|
|
|
else
|
2008-06-03 22:34:52 +00:00
|
|
|
snprintf(prompt, sizeof(prompt),
|
2009-10-21 20:55:04 +00:00
|
|
|
"Creating symbolic link %s to %s",
|
2012-01-18 04:37:35 +00:00
|
|
|
path_localtime, zoneinfo_file);
|
2009-10-20 06:54:31 +00:00
|
|
|
if (usedialog)
|
2012-01-18 04:37:35 +00:00
|
|
|
dialog_msgbox(title, prompt, 8, 72, 1);
|
2009-10-20 06:54:31 +00:00
|
|
|
else
|
|
|
|
fprintf(stderr, "%s\n", prompt);
|
1997-01-21 10:53:38 +00:00
|
|
|
#endif
|
1996-11-19 18:09:41 +00:00
|
|
|
|
|
|
|
if (reallydoit) {
|
2011-03-30 21:33:23 +00:00
|
|
|
if (zoneinfo_file == NULL) {
|
|
|
|
if (unlink(path_localtime) < 0 && errno != ENOENT) {
|
|
|
|
snprintf(title, sizeof(title), "Error");
|
|
|
|
snprintf(prompt, sizeof(prompt),
|
|
|
|
"Could not delete %s: %s", path_localtime,
|
|
|
|
strerror(errno));
|
|
|
|
if (usedialog)
|
2011-11-24 13:24:06 +00:00
|
|
|
dialog_msgbox(title, prompt, 8, 72, 1);
|
2011-03-30 21:33:23 +00:00
|
|
|
else
|
|
|
|
fprintf(stderr, "%s\n", prompt);
|
|
|
|
|
|
|
|
return (DITEM_FAILURE | DITEM_RECREATE);
|
|
|
|
}
|
2011-11-02 04:21:20 +00:00
|
|
|
if (unlink(path_db) < 0 && errno != ENOENT) {
|
|
|
|
snprintf(title, sizeof(title), "Error");
|
|
|
|
snprintf(prompt, sizeof(prompt),
|
|
|
|
"Could not delete %s: %s", path_db,
|
|
|
|
strerror(errno));
|
|
|
|
if (usedialog)
|
2011-11-24 13:24:06 +00:00
|
|
|
dialog_msgbox(title, prompt, 8, 72, 1);
|
2011-11-02 04:21:20 +00:00
|
|
|
else
|
|
|
|
fprintf(stderr, "%s\n", prompt);
|
|
|
|
|
|
|
|
return (DITEM_FAILURE | DITEM_RECREATE);
|
|
|
|
}
|
2012-01-18 04:37:35 +00:00
|
|
|
#ifdef VERBOSE
|
2013-03-04 11:34:31 +00:00
|
|
|
snprintf(title, sizeof(title), "Done");
|
2012-01-18 04:37:35 +00:00
|
|
|
snprintf(prompt, sizeof(prompt),
|
|
|
|
"Removed %s", path_localtime);
|
2013-03-04 11:34:31 +00:00
|
|
|
if (usedialog)
|
|
|
|
dialog_msgbox(title, prompt, 8, 72, 1);
|
|
|
|
else
|
|
|
|
fprintf(stderr, "%s\n", prompt);
|
2012-01-18 04:37:35 +00:00
|
|
|
#endif
|
2011-03-30 21:33:23 +00:00
|
|
|
return (DITEM_LEAVE_MENU);
|
|
|
|
}
|
2011-11-24 13:24:06 +00:00
|
|
|
|
1996-11-19 18:09:41 +00:00
|
|
|
if (copymode) {
|
2009-10-21 20:55:04 +00:00
|
|
|
fd1 = open(zoneinfo_file, O_RDONLY, 0);
|
1996-11-19 18:09:41 +00:00
|
|
|
if (fd1 < 0) {
|
2008-06-03 22:34:52 +00:00
|
|
|
snprintf(title, sizeof(title), "Error");
|
|
|
|
snprintf(prompt, sizeof(prompt),
|
2009-10-21 20:55:04 +00:00
|
|
|
"Could not open %s: %s", zoneinfo_file,
|
2008-06-03 22:34:52 +00:00
|
|
|
strerror(errno));
|
2009-10-20 06:54:31 +00:00
|
|
|
if (usedialog)
|
2011-11-24 13:24:06 +00:00
|
|
|
dialog_msgbox(title, prompt, 8, 72, 1);
|
2009-10-20 06:54:31 +00:00
|
|
|
else
|
|
|
|
fprintf(stderr, "%s\n", prompt);
|
2008-06-03 22:34:52 +00:00
|
|
|
return (DITEM_FAILURE | DITEM_RECREATE);
|
1996-11-19 18:09:41 +00:00
|
|
|
}
|
|
|
|
|
2012-02-08 05:03:04 +00:00
|
|
|
if (unlink(path_localtime) < 0 && errno != ENOENT) {
|
2012-01-12 05:50:32 +00:00
|
|
|
snprintf(prompt, sizeof(prompt),
|
|
|
|
"Could not unlink %s: %s",
|
|
|
|
path_localtime, strerror(errno));
|
|
|
|
if (usedialog) {
|
|
|
|
snprintf(title, sizeof(title), "Error");
|
|
|
|
dialog_msgbox(title, prompt, 8, 72, 1);
|
|
|
|
} else
|
|
|
|
fprintf(stderr, "%s\n", prompt);
|
|
|
|
return (DITEM_FAILURE | DITEM_RECREATE);
|
|
|
|
}
|
|
|
|
|
2009-10-21 20:55:04 +00:00
|
|
|
fd2 = open(path_localtime, O_CREAT | O_EXCL | O_WRONLY,
|
2008-06-03 22:34:52 +00:00
|
|
|
S_IRUSR | S_IRGRP | S_IROTH);
|
1996-11-19 18:09:41 +00:00
|
|
|
if (fd2 < 0) {
|
2008-06-03 22:34:52 +00:00
|
|
|
snprintf(title, sizeof(title), "Error");
|
|
|
|
snprintf(prompt, sizeof(prompt),
|
2009-10-21 20:55:04 +00:00
|
|
|
"Could not open %s: %s",
|
|
|
|
path_localtime, strerror(errno));
|
2009-10-20 06:54:31 +00:00
|
|
|
if (usedialog)
|
2011-11-24 13:24:06 +00:00
|
|
|
dialog_msgbox(title, prompt, 8, 72, 1);
|
2009-10-20 06:54:31 +00:00
|
|
|
else
|
|
|
|
fprintf(stderr, "%s\n", prompt);
|
2008-06-03 22:34:52 +00:00
|
|
|
return (DITEM_FAILURE | DITEM_RECREATE);
|
1996-11-19 18:09:41 +00:00
|
|
|
}
|
|
|
|
|
2008-06-03 22:34:52 +00:00
|
|
|
while ((len = read(fd1, buf, sizeof(buf))) > 0)
|
2010-06-05 12:49:39 +00:00
|
|
|
if ((len = write(fd2, buf, len)) < 0)
|
|
|
|
break;
|
1996-11-19 18:09:41 +00:00
|
|
|
|
|
|
|
if (len == -1) {
|
2008-06-03 22:34:52 +00:00
|
|
|
snprintf(title, sizeof(title), "Error");
|
|
|
|
snprintf(prompt, sizeof(prompt),
|
2009-10-21 20:55:04 +00:00
|
|
|
"Error copying %s to %s %s", zoneinfo_file,
|
|
|
|
path_localtime, strerror(errno));
|
2009-10-20 06:54:31 +00:00
|
|
|
if (usedialog)
|
2011-11-24 13:24:06 +00:00
|
|
|
dialog_msgbox(title, prompt, 8, 72, 1);
|
2009-10-20 06:54:31 +00:00
|
|
|
else
|
|
|
|
fprintf(stderr, "%s\n", prompt);
|
1996-11-19 18:09:41 +00:00
|
|
|
/* Better to leave none than a corrupt one. */
|
2009-10-21 20:55:04 +00:00
|
|
|
unlink(path_localtime);
|
2008-06-03 22:34:52 +00:00
|
|
|
return (DITEM_FAILURE | DITEM_RECREATE);
|
1996-11-19 18:09:41 +00:00
|
|
|
}
|
|
|
|
close(fd1);
|
|
|
|
close(fd2);
|
|
|
|
} else {
|
2009-10-21 20:55:04 +00:00
|
|
|
if (access(zoneinfo_file, R_OK) != 0) {
|
2008-06-03 22:34:52 +00:00
|
|
|
snprintf(title, sizeof(title), "Error");
|
|
|
|
snprintf(prompt, sizeof(prompt),
|
2009-10-21 20:55:04 +00:00
|
|
|
"Cannot access %s: %s", zoneinfo_file,
|
2008-06-03 22:34:52 +00:00
|
|
|
strerror(errno));
|
2009-10-20 06:54:31 +00:00
|
|
|
if (usedialog)
|
2011-11-24 13:24:06 +00:00
|
|
|
dialog_msgbox(title, prompt, 8, 72, 1);
|
2009-10-20 06:54:31 +00:00
|
|
|
else
|
|
|
|
fprintf(stderr, "%s\n", prompt);
|
2008-06-03 22:34:52 +00:00
|
|
|
return (DITEM_FAILURE | DITEM_RECREATE);
|
1996-11-19 18:09:41 +00:00
|
|
|
}
|
2012-02-08 05:03:04 +00:00
|
|
|
if (unlink(path_localtime) < 0 && errno != ENOENT) {
|
2012-01-12 05:50:32 +00:00
|
|
|
snprintf(prompt, sizeof(prompt),
|
|
|
|
"Could not unlink %s: %s",
|
|
|
|
path_localtime, strerror(errno));
|
|
|
|
if (usedialog) {
|
|
|
|
snprintf(title, sizeof(title), "Error");
|
|
|
|
dialog_msgbox(title, prompt, 8, 72, 1);
|
|
|
|
} else
|
|
|
|
fprintf(stderr, "%s\n", prompt);
|
|
|
|
return (DITEM_FAILURE | DITEM_RECREATE);
|
|
|
|
}
|
2009-10-21 20:55:04 +00:00
|
|
|
if (symlink(zoneinfo_file, path_localtime) < 0) {
|
2008-06-03 22:34:52 +00:00
|
|
|
snprintf(title, sizeof(title), "Error");
|
|
|
|
snprintf(prompt, sizeof(prompt),
|
2009-10-21 20:55:04 +00:00
|
|
|
"Cannot create symbolic link %s to %s: %s",
|
|
|
|
path_localtime, zoneinfo_file,
|
2008-06-03 22:34:52 +00:00
|
|
|
strerror(errno));
|
2009-10-20 06:54:31 +00:00
|
|
|
if (usedialog)
|
2011-11-24 13:24:06 +00:00
|
|
|
dialog_msgbox(title, prompt, 8, 72, 1);
|
2009-10-20 06:54:31 +00:00
|
|
|
else
|
|
|
|
fprintf(stderr, "%s\n", prompt);
|
2008-06-03 22:34:52 +00:00
|
|
|
return (DITEM_FAILURE | DITEM_RECREATE);
|
1996-11-19 18:09:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-01-21 10:53:38 +00:00
|
|
|
#ifdef VERBOSE
|
2012-01-18 04:37:35 +00:00
|
|
|
snprintf(title, sizeof(title), "Done");
|
|
|
|
if (copymode)
|
|
|
|
snprintf(prompt, sizeof(prompt),
|
|
|
|
"Copied timezone file from %s to %s",
|
|
|
|
zoneinfo_file, path_localtime);
|
|
|
|
else
|
|
|
|
snprintf(prompt, sizeof(prompt),
|
|
|
|
"Created symbolic link from %s to %s",
|
|
|
|
zoneinfo_file, path_localtime);
|
|
|
|
if (usedialog)
|
|
|
|
dialog_msgbox(title, prompt, 8, 72, 1);
|
|
|
|
else
|
|
|
|
fprintf(stderr, "%s\n", prompt);
|
1997-01-21 10:53:38 +00:00
|
|
|
#endif
|
2012-01-18 04:37:35 +00:00
|
|
|
} /* reallydoit */
|
2009-10-20 06:54:31 +00:00
|
|
|
|
2009-10-21 20:55:04 +00:00
|
|
|
return (DITEM_LEAVE_MENU);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
install_zoneinfo(const char *zoneinfo)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
FILE *f;
|
|
|
|
char path_zoneinfo_file[MAXPATHLEN];
|
|
|
|
|
|
|
|
sprintf(path_zoneinfo_file, "%s/%s", path_zoneinfo, zoneinfo);
|
|
|
|
rv = install_zoneinfo_file(path_zoneinfo_file);
|
|
|
|
|
2009-10-20 06:54:31 +00:00
|
|
|
/* Save knowledge for later */
|
2012-01-18 04:12:32 +00:00
|
|
|
if (reallydoit && (rv & DITEM_FAILURE) == 0) {
|
|
|
|
if ((f = fopen(path_db, "w")) != NULL) {
|
|
|
|
fprintf(f, "%s\n", zoneinfo);
|
|
|
|
fclose(f);
|
|
|
|
}
|
2009-10-20 06:54:31 +00:00
|
|
|
}
|
|
|
|
|
2009-10-21 20:55:04 +00:00
|
|
|
return (rv);
|
1996-11-19 18:09:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
confirm_zone(const char *filename)
|
|
|
|
{
|
2008-06-03 22:34:52 +00:00
|
|
|
char title[64], prompt[64];
|
|
|
|
time_t t = time(0);
|
|
|
|
struct tm *tm;
|
|
|
|
int rv;
|
2011-11-24 13:24:06 +00:00
|
|
|
|
2011-03-30 21:33:23 +00:00
|
|
|
setenv("TZ", filename == NULL ? "" : filename, 1);
|
1996-11-19 18:09:41 +00:00
|
|
|
tzset();
|
|
|
|
tm = localtime(&t);
|
|
|
|
|
2008-06-03 22:34:52 +00:00
|
|
|
snprintf(title, sizeof(title), "Confirmation");
|
|
|
|
snprintf(prompt, sizeof(prompt),
|
|
|
|
"Does the abbreviation `%s' look reasonable?", tm->tm_zone);
|
|
|
|
rv = !dialog_yesno(title, prompt, 5, 72);
|
|
|
|
return (rv);
|
1996-11-19 18:09:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
set_zone_multi(dialogMenuItem *dmi)
|
|
|
|
{
|
2008-06-03 22:34:52 +00:00
|
|
|
struct zone *zp = dmi->data;
|
|
|
|
int rv;
|
1996-11-19 18:09:41 +00:00
|
|
|
|
|
|
|
if (!confirm_zone(zp->filename))
|
2008-06-03 22:34:52 +00:00
|
|
|
return (DITEM_FAILURE | DITEM_RECREATE);
|
1996-11-19 18:09:41 +00:00
|
|
|
|
2009-10-21 20:55:04 +00:00
|
|
|
rv = install_zoneinfo(zp->filename);
|
2008-06-03 22:34:52 +00:00
|
|
|
return (rv);
|
1996-11-19 18:09:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
set_zone_whole_country(dialogMenuItem *dmi)
|
|
|
|
{
|
2008-06-03 22:34:52 +00:00
|
|
|
struct country *cp = dmi->data;
|
|
|
|
int rv;
|
1996-11-19 18:09:41 +00:00
|
|
|
|
|
|
|
if (!confirm_zone(cp->filename))
|
2008-06-03 22:34:52 +00:00
|
|
|
return (DITEM_FAILURE | DITEM_RECREATE);
|
1996-11-19 18:09:41 +00:00
|
|
|
|
2009-10-21 20:55:04 +00:00
|
|
|
rv = install_zoneinfo(cp->filename);
|
2008-06-03 22:34:52 +00:00
|
|
|
return (rv);
|
1996-11-19 18:09:41 +00:00
|
|
|
}
|
|
|
|
|
1997-10-27 07:49:47 +00:00
|
|
|
static void
|
2008-06-03 22:34:52 +00:00
|
|
|
usage(void)
|
1997-10-27 07:49:47 +00:00
|
|
|
{
|
2008-06-03 22:34:52 +00:00
|
|
|
|
2011-05-20 17:35:10 +00:00
|
|
|
fprintf(stderr, "usage: tzsetup [-nrs] [-C chroot_directory]"
|
|
|
|
" [zoneinfo_file | zoneinfo_name]\n");
|
1997-10-27 07:49:47 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
1996-11-19 18:09:41 +00:00
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2008-06-03 22:34:52 +00:00
|
|
|
char title[64], prompt[128];
|
2009-10-20 06:54:31 +00:00
|
|
|
int c, fd, rv, skiputc;
|
2014-11-11 19:45:14 +00:00
|
|
|
char vm_guest[16] = "";
|
|
|
|
size_t len = sizeof(vm_guest);
|
1996-11-19 18:09:41 +00:00
|
|
|
|
2009-07-04 11:11:21 +00:00
|
|
|
skiputc = 0;
|
2014-11-11 19:37:17 +00:00
|
|
|
|
|
|
|
/* Default skiputc to 1 for VM guests */
|
2014-11-11 19:45:14 +00:00
|
|
|
if (sysctlbyname("kern.vm_guest", vm_guest, &len, NULL, 0) == 0 &&
|
2014-11-11 19:37:17 +00:00
|
|
|
strcmp(vm_guest, "none") != 0)
|
2014-11-11 19:45:14 +00:00
|
|
|
skiputc = 1;
|
2014-11-11 19:37:17 +00:00
|
|
|
|
2009-10-21 20:55:04 +00:00
|
|
|
while ((c = getopt(argc, argv, "C:nrs")) != -1) {
|
1996-11-19 18:09:41 +00:00
|
|
|
switch(c) {
|
2009-10-21 20:55:04 +00:00
|
|
|
case 'C':
|
|
|
|
chrootenv = optarg;
|
|
|
|
break;
|
1996-11-19 18:09:41 +00:00
|
|
|
case 'n':
|
|
|
|
reallydoit = 0;
|
|
|
|
break;
|
2009-10-20 06:54:31 +00:00
|
|
|
case 'r':
|
|
|
|
reinstall = 1;
|
2009-10-21 20:55:04 +00:00
|
|
|
usedialog = 0;
|
2009-10-20 06:54:31 +00:00
|
|
|
break;
|
2009-07-04 11:11:21 +00:00
|
|
|
case 's':
|
|
|
|
skiputc = 1;
|
|
|
|
break;
|
1996-11-19 18:09:41 +00:00
|
|
|
default:
|
1997-10-27 07:49:47 +00:00
|
|
|
usage();
|
1996-11-19 18:09:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-02-02 20:26:31 +00:00
|
|
|
if (argc - optind > 1)
|
1997-10-27 07:49:47 +00:00
|
|
|
usage();
|
1996-11-19 18:09:41 +00:00
|
|
|
|
2009-10-21 20:55:04 +00:00
|
|
|
if (chrootenv == NULL) {
|
|
|
|
strcpy(path_zonetab, _PATH_ZONETAB);
|
|
|
|
strcpy(path_iso3166, _PATH_ISO3166);
|
|
|
|
strcpy(path_zoneinfo, _PATH_ZONEINFO);
|
|
|
|
strcpy(path_localtime, _PATH_LOCALTIME);
|
|
|
|
strcpy(path_db, _PATH_DB);
|
|
|
|
strcpy(path_wall_cmos_clock, _PATH_WALL_CMOS_CLOCK);
|
|
|
|
} else {
|
|
|
|
sprintf(path_zonetab, "%s/%s", chrootenv, _PATH_ZONETAB);
|
|
|
|
sprintf(path_iso3166, "%s/%s", chrootenv, _PATH_ISO3166);
|
|
|
|
sprintf(path_zoneinfo, "%s/%s", chrootenv, _PATH_ZONEINFO);
|
|
|
|
sprintf(path_localtime, "%s/%s", chrootenv, _PATH_LOCALTIME);
|
|
|
|
sprintf(path_db, "%s/%s", chrootenv, _PATH_DB);
|
|
|
|
sprintf(path_wall_cmos_clock, "%s/%s", chrootenv,
|
|
|
|
_PATH_WALL_CMOS_CLOCK);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-08-05 11:48:47 +00:00
|
|
|
/* Override the user-supplied umask. */
|
2008-06-03 22:34:52 +00:00
|
|
|
(void)umask(S_IWGRP | S_IWOTH);
|
1999-08-05 11:48:47 +00:00
|
|
|
|
2009-10-20 06:54:31 +00:00
|
|
|
if (reinstall == 1) {
|
|
|
|
FILE *f;
|
2012-01-25 02:15:40 +00:00
|
|
|
char zoneinfo[MAXPATHLEN];
|
2009-10-20 06:54:31 +00:00
|
|
|
|
2009-10-21 20:55:04 +00:00
|
|
|
if ((f = fopen(path_db, "r")) != NULL) {
|
2012-01-25 02:15:40 +00:00
|
|
|
if (fgets(zoneinfo, sizeof(zoneinfo), f) != NULL) {
|
|
|
|
zoneinfo[sizeof(zoneinfo) - 1] = 0;
|
|
|
|
if (strlen(zoneinfo) > 0) {
|
|
|
|
zoneinfo[strlen(zoneinfo) - 1] = 0;
|
|
|
|
rv = install_zoneinfo(zoneinfo);
|
2009-10-20 06:54:31 +00:00
|
|
|
exit(rv & ~DITEM_LEAVE_MENU);
|
|
|
|
}
|
2009-10-21 20:55:04 +00:00
|
|
|
errx(1, "Error reading %s.\n", path_db);
|
2009-10-20 06:54:31 +00:00
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
errx(1,
|
|
|
|
"Unable to determine earlier installed zoneinfo "
|
2012-01-25 02:15:40 +00:00
|
|
|
"name. Check %s", path_db);
|
2009-10-21 20:55:04 +00:00
|
|
|
}
|
|
|
|
errx(1, "Cannot open %s for reading. Does it exist?", path_db);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the arguments on the command-line do not specify a file,
|
|
|
|
* then interpret it as a zoneinfo name
|
|
|
|
*/
|
|
|
|
if (optind == argc - 1) {
|
|
|
|
struct stat sb;
|
|
|
|
|
|
|
|
if (stat(argv[optind], &sb) != 0) {
|
|
|
|
usedialog = 0;
|
|
|
|
rv = install_zoneinfo(argv[optind]);
|
|
|
|
exit(rv & ~DITEM_LEAVE_MENU);
|
2009-10-20 06:54:31 +00:00
|
|
|
}
|
2009-10-21 20:55:04 +00:00
|
|
|
/* FALLTHROUGH */
|
2009-10-20 06:54:31 +00:00
|
|
|
}
|
|
|
|
|
2012-01-25 02:15:40 +00:00
|
|
|
read_iso3166_table();
|
|
|
|
read_zones();
|
|
|
|
sort_countries();
|
|
|
|
make_menus();
|
|
|
|
|
2011-11-24 13:24:06 +00:00
|
|
|
init_dialog(stdin, stdout);
|
2009-07-04 11:11:21 +00:00
|
|
|
if (skiputc == 0) {
|
2011-11-24 13:24:06 +00:00
|
|
|
DIALOG_VARS save_vars;
|
|
|
|
int yesno;
|
|
|
|
|
2009-07-04 11:11:21 +00:00
|
|
|
snprintf(title, sizeof(title),
|
|
|
|
"Select local or UTC (Greenwich Mean Time) clock");
|
|
|
|
snprintf(prompt, sizeof(prompt),
|
|
|
|
"Is this machine's CMOS clock set to UTC? "
|
|
|
|
"If it is set to local time,\n"
|
|
|
|
"or you don't know, please choose NO here!");
|
2011-11-24 13:24:06 +00:00
|
|
|
dlg_save_vars(&save_vars);
|
|
|
|
#if !defined(__sparc64__)
|
|
|
|
dialog_vars.defaultno = TRUE;
|
|
|
|
#endif
|
|
|
|
yesno = dialog_yesno(title, prompt, 7, 73);
|
|
|
|
dlg_restore_vars(&save_vars);
|
|
|
|
if (!yesno) {
|
2009-07-04 11:11:21 +00:00
|
|
|
if (reallydoit)
|
2010-07-19 16:38:45 +00:00
|
|
|
unlink(path_wall_cmos_clock);
|
2009-07-04 11:11:21 +00:00
|
|
|
} else {
|
|
|
|
if (reallydoit) {
|
2010-07-19 16:38:45 +00:00
|
|
|
fd = open(path_wall_cmos_clock,
|
2009-07-04 11:11:21 +00:00
|
|
|
O_WRONLY | O_CREAT | O_TRUNC,
|
|
|
|
S_IRUSR | S_IRGRP | S_IROTH);
|
2009-10-19 21:24:19 +00:00
|
|
|
if (fd < 0) {
|
|
|
|
end_dialog();
|
2009-07-04 11:11:21 +00:00
|
|
|
err(1, "create %s",
|
2010-07-19 16:38:45 +00:00
|
|
|
path_wall_cmos_clock);
|
2009-10-19 21:24:19 +00:00
|
|
|
}
|
2009-07-04 11:11:21 +00:00
|
|
|
close(fd);
|
|
|
|
}
|
1998-12-16 05:34:49 +00:00
|
|
|
}
|
2011-11-24 13:24:06 +00:00
|
|
|
dlg_clear();
|
1998-01-10 15:55:11 +00:00
|
|
|
}
|
1999-02-02 20:26:31 +00:00
|
|
|
if (optind == argc - 1) {
|
2008-06-03 22:34:52 +00:00
|
|
|
snprintf(title, sizeof(title), "Default timezone provided");
|
|
|
|
snprintf(prompt, sizeof(prompt),
|
|
|
|
"\nUse the default `%s' zone?", argv[optind]);
|
|
|
|
if (!dialog_yesno(title, prompt, 7, 72)) {
|
2009-10-21 20:55:04 +00:00
|
|
|
rv = install_zoneinfo_file(argv[optind]);
|
2011-11-24 13:24:06 +00:00
|
|
|
dlg_clear();
|
1999-02-02 20:26:31 +00:00
|
|
|
end_dialog();
|
2009-10-20 06:54:31 +00:00
|
|
|
exit(rv & ~DITEM_LEAVE_MENU);
|
1999-02-02 20:26:31 +00:00
|
|
|
}
|
2011-11-24 13:24:06 +00:00
|
|
|
dlg_clear();
|
1999-02-02 20:26:31 +00:00
|
|
|
}
|
2008-06-03 22:34:52 +00:00
|
|
|
snprintf(title, sizeof(title), "Time Zone Selector");
|
|
|
|
snprintf(prompt, sizeof(prompt), "Select a region");
|
2011-11-24 13:24:06 +00:00
|
|
|
xdialog_menu(title, prompt, -1, -1, NCONTINENTS, NCONTINENTS,
|
|
|
|
continents);
|
1999-02-02 20:26:31 +00:00
|
|
|
|
2011-11-24 13:24:06 +00:00
|
|
|
dlg_clear();
|
1996-11-19 18:09:41 +00:00
|
|
|
end_dialog();
|
2008-06-03 22:34:52 +00:00
|
|
|
return (0);
|
1996-11-19 18:09:41 +00:00
|
|
|
}
|