diff options
author | wollman <wollman@FreeBSD.org> | 1996-11-19 18:09:41 +0000 |
---|---|---|
committer | wollman <wollman@FreeBSD.org> | 1996-11-19 18:09:41 +0000 |
commit | 8587682a08d10e4f98954d015e225341302797ab (patch) | |
tree | 93c74b4ed74d4c41b44522421907724ff1195c38 | |
parent | 9b54175344028e4b01b450a1467b60ee7e047d47 (diff) | |
download | FreeBSD-src-8587682a08d10e4f98954d015e225341302797ab.zip FreeBSD-src-8587682a08d10e4f98954d015e225341302797ab.tar.gz |
Completely re-vamped `tzsetup':
- It no longer attempts to fiddle wall-vs-UTC-in-RTC. The results
were just confusing most of the time.
- The program no longer contains a pre-compiled list of timezones
(compiled by groveling through the tzdata source files for comments
starting with `ZONE-DESCR'). Now it uses the new `zone.tab' file
supplied with current versions of the timezone data files, to determine
the list at run time. (It also requires the ISO 3166 table I
committed some months ago.)
AS A RESULT, this program will NOT work until the new timezone data files
are committed (should happen sometime soon).
-rw-r--r-- | usr.sbin/tzsetup/Makefile | 13 | ||||
-rw-r--r-- | usr.sbin/tzsetup/grok.pl | 76 | ||||
-rw-r--r-- | usr.sbin/tzsetup/main.c | 264 | ||||
-rw-r--r-- | usr.sbin/tzsetup/pathnames.h | 31 | ||||
-rw-r--r-- | usr.sbin/tzsetup/paths.h | 4 | ||||
-rw-r--r-- | usr.sbin/tzsetup/tzmenu.c | 186 | ||||
-rw-r--r-- | usr.sbin/tzsetup/tzsetup.c | 651 | ||||
-rw-r--r-- | usr.sbin/tzsetup/tzsetup.h | 55 |
8 files changed, 656 insertions, 624 deletions
diff --git a/usr.sbin/tzsetup/Makefile b/usr.sbin/tzsetup/Makefile index 001c257..25ca3ff 100644 --- a/usr.sbin/tzsetup/Makefile +++ b/usr.sbin/tzsetup/Makefile @@ -1,20 +1,9 @@ -# $Id: Makefile,v 1.3 1996/01/24 00:11:57 wosch Exp $ +# $Id: Makefile,v 1.4 1996/09/04 22:25:35 bde Exp $ PROG= tzsetup MAN8= tzsetup.8 -SRCS= main.c tzmenu.c menus.c CFLAGS+= -I${.CURDIR} -CLEANFILES+= menus.c LDADD+= -ldialog -lncurses -lmytinfo DPADD+= ${LIBDIALOG} ${LIBNCURSES} ${LIBMYTINFO} -Z= ${.CURDIR}/../../share/zoneinfo -.PATH: ${Z} - -ZF= africa antarctica asia australasia europe northamerica southamerica - -menus.c: grok.pl ${ZF} - perl ${.CURDIR}/grok.pl `for a in ${ZF}; do echo ${Z}/$$a; done` \ - > ${.TARGET} - .include <bsd.prog.mk> diff --git a/usr.sbin/tzsetup/grok.pl b/usr.sbin/tzsetup/grok.pl deleted file mode 100644 index 434554f..0000000 --- a/usr.sbin/tzsetup/grok.pl +++ /dev/null @@ -1,76 +0,0 @@ -# -*- Perl -*- - -%reg_ctry = (); -%ctry_files = (); -%file_descrs = (); - -while(<>) { - next if(!/^\# ZONE-DESCR/); - chop; - split; - - shift(@_); shift(@_); # get rid of # ZONE-DESCR - - # Now $_[0] is region, $_[1] is filename, $_[2] is country, - # and @_[3 .. $#_] is the description - $reg = $_[0]; - $file = $_[1]; - $ctry = $_[2]; - $descr = join(' ', @_[3 .. $#_]); - - if($reg_ctry{$reg} =~ /$ctry/) { - # do nothing - } else { - $reg_ctry{$reg} = $ctry . "," . $reg_ctry{$reg}; - } - - $ctry_files{$ctry} .= ",$reg/$file"; - $file_descrs{"$reg/$file"} = $descr; -} - -print "/* This file automatically generated. */\n"; -print "#include \"tzsetup.h\"\n"; - -foreach $ctry (sort keys %ctry_files) { - print "const char *files_$ctry\[\] = {\n"; - $ctry_files{$ctry} =~ s/^,//; - foreach $file (sort {$file_descrs{$a} cmp $file_descrs{$b}} - split(/,/, $ctry_files{$ctry})) { - print "\t\"$file\",\n"; - } - print "\t0 };\n"; - print "const char *menu_$ctry\[\] = {\n"; - $i = 0; - - foreach $file (sort {$file_descrs{$a} cmp $file_descrs{$b}} - split(/,/, $ctry_files{$ctry})) { - $i++; - print "\t\"$i\", \"$file_descrs{$file}\",\n"; - } - print "\t0, 0 };\n"; - - print "struct country $ctry = { files_$ctry, menu_$ctry, $i };\n"; -} - -foreach $reg (sort keys %reg_ctry) { - print "\nstruct country *menu_$reg\[\] = {\n"; - $reg_ctry{$reg} =~ s/,$//; - foreach $ctry (sort split(/,/, $reg_ctry{$reg})) { - print "\t&$ctry,\n"; - } - - print "\t0 };\n"; - print "const char *name_$reg\[\] = {\n"; - $i = 0; - foreach $ctry (sort split(/,/, $reg_ctry{$reg})) { - $i++; - $ctry =~ s/_/ /g; - print "\t\"$i\", \"$ctry\",\n"; - } - print "\t0, 0 };\n"; - - print "struct region $reg = { menu_$reg, name_$reg, $i };\n"; -} - -exit 0; - diff --git a/usr.sbin/tzsetup/main.c b/usr.sbin/tzsetup/main.c deleted file mode 100644 index 6a46538..0000000 --- a/usr.sbin/tzsetup/main.c +++ /dev/null @@ -1,264 +0,0 @@ -/* - * Copyright 1995 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. - * - * 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. - */ - -static const char rcsid[] = - "$Id: main.c,v 1.8 1996/07/03 01:17:34 jkh Exp $"; - -#include <sys/types.h> -#include <sys/stat.h> -#include <stdio.h> -#include <ncurses.h> -#include <dialog.h> -#include <limits.h> -#include <time.h> -#include <stdlib.h> -#include <errno.h> -#include <fcntl.h> -#include <unistd.h> - -#include "tzsetup.h" -#include "pathnames.h" - -static int set_time(); - -enum cmos { CMOS_UTC, CMOS_LOCAL, CMOS_LEAVE } cmos_state = CMOS_LEAVE; -static int time_adjust = 0; -static enum cmos cmos(enum cmos); -static void fiddle_cmos(void); - -int -main(void) -{ - const char *tz; - - init_dialog(); - if (set_time() != 0) { - end_dialog(); - exit(1); - } - - tz = tzmenu(); - - fiddle_cmos(); - - dialog_notify("Daemons will only notice\n" - "a timezone change.\n" - "after rebooting.\n"); - end_dialog(); - - return tz ? 0 : 1; -} - -static int -set_time(void) -{ - unsigned char result[_POSIX2_LINE_MAX]; - unsigned char buf2[_POSIX2_LINE_MAX]; - static struct tm usertm, systm; - time_t usertime, systime; - long diff; - int rv; - - /* - * If /etc/wall_cmos_clock exists, or if there is already a timezone - * file installed, then just leave it alone and don't ask the user - * what time it is (because the system clock is already in POSIX - * time so we don't need to adjust anything later on). - */ - time(&systime); - systm = *localtime(&systime); - if (systm.tm_zone[0] - || access(PATH_WALL_CMOS_CLOCK, 0) == 0) { - cmos_state = CMOS_LEAVE; - return 0; - } - - usertm = systm; - usertm.tm_isdst = -1; - - result[0] = '\0'; - - while(1) { - rv = dialog_inputbox("Checking current time", - "Please enter the current local time" - " using 24-hour style,\n" - "in the form HH:MM", - 8, 78, result); - if (rv != 0) - return 1; - - if (result[0]) { - if (sscanf(result, "%d:%d:%d", &usertm.tm_hour, - &usertm.tm_min, - &usertm.tm_sec) < 2) { - snprintf(buf2, sizeof buf2, - "Invalid time format: %s", result); - dialog_notify(buf2); - continue; - } - usertime = mktime(&usertm); - if (usertime == (time_t)-1) { - snprintf(buf2, sizeof buf2, - "Unreasonable time: %s", result); - dialog_notify(buf2); - continue; - } - - diff = usertime - systime; - if (labs(diff) > 15*60) { - cmos_state = cmos(CMOS_LOCAL); - if (diff > 0) { - time_adjust = ((diff + 15*60)/(30*60) - * 30*60); - } else { - time_adjust = ((diff - 15*60)/(30*60) - * 30*60); - } - } else { - cmos_state = cmos(CMOS_UTC); - time_adjust = 0; - } - break; - } - } - return 0; -} - -static unsigned char *cmos_list[] = { - "1", "CMOS clock is set to Universal time (UTC)", - "2", "CMOS clock is set to local time", - "3", "I'm not sure, leave it alone" -}; - -static enum cmos -cmos(enum cmos state) -{ - int rv, sel = 0; - unsigned char buf[_POSIX2_LINE_MAX]; - unsigned char result[_POSIX2_LINE_MAX]; - - snprintf(buf, sizeof buf, "%s seems most likely", - state == CMOS_UTC ? "UTC" : "local time"); - - rv = dialog_menu("CMOS clock in local time or UTC", - buf, 12, 78, 3, 3, cmos_list, result, &sel, 0); - if (rv == 0) { - return sel; - } else { - return state; - } -} - -static void -fiddle_cmos(void) -{ - int fd; - - switch(cmos_state) { - case CMOS_LEAVE: - break; - case CMOS_UTC: - if (unlink(PATH_WALL_CMOS_CLOCK) == -1 && - errno != ENOENT) - dialog_notify("Error removing " PATH_WALL_CMOS_CLOCK); - break; - case CMOS_LOCAL: - if ((fd = open(PATH_WALL_CMOS_CLOCK, O_RDONLY|O_CREAT, 0644)) - == -1) - dialog_notify("Error creating " PATH_WALL_CMOS_CLOCK); - else - close(fd); - break; - } -} - - -int -setzone(const char *zone) -{ - time_t systime; - struct tm *tm; - char msg[_POSIX2_LINE_MAX]; - int rv; - struct stat sb; - - /* - * Make sure the definition file does exist before clobbering - * an existing PATH_LOCALTIME. We do this before evaluating - * locatlime below, since a missing zoneinfo file would result - * in nothing but garbage in the displayed time information. - */ - snprintf(msg, sizeof msg, PATH_ZONEINFO "/%s", zone); - if (stat(msg, &sb) == -1) { - snprintf(msg, sizeof msg, - "Could not find definition file\n" - PATH_ZONEINFO "/%s", zone); - dialog_notify(msg); - return 1; - } - - snprintf(msg, sizeof msg, "TZ=%s", zone); - putenv(msg); - tzset(); - time(&systime); - systime += time_adjust; - tm = localtime(&systime); - - snprintf(msg, sizeof msg, "Does %s look reasonable?", tm->tm_zone); - - rv = dialog_yesno("Verifying timezone selection", - msg, -1, -1); - if (rv) - return 1; - - if (lstat(PATH_LOCALTIME, &sb) == 0 && S_ISLNK(sb.st_mode)) { - /* The destination is already a symlink, symlink it. */ - (void)unlink(PATH_LOCALTIME); - snprintf(msg, sizeof msg, PATH_ZONEINFO "/%s", zone); - if (symlink(msg, PATH_LOCALTIME) == -1) { - dialog_notify("Could not create a symbolic link for " - PATH_LOCALTIME); - return 1; - } - } else { - /* Copy it. */ - snprintf(msg, sizeof msg, - "install -C -o bin -g bin -m 0444 " - PATH_ZONEINFO "/%s " PATH_LOCALTIME, zone); - if (system(msg) != 0) { - dialog_notify("Could not copy zone information into " - PATH_LOCALTIME); - return 1; - } - } - - snprintf(msg, sizeof msg, "Installed timezone file %s", zone); - dialog_notify(msg); - return 0; -} diff --git a/usr.sbin/tzsetup/pathnames.h b/usr.sbin/tzsetup/pathnames.h deleted file mode 100644 index 006b2d7..0000000 --- a/usr.sbin/tzsetup/pathnames.h +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright (C) 1996 - * FreeBSD Inc. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY FreeBSD Inc. AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL [your name] OR CONTRIBUTORS 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. - * - * $Id$ - */ - -#define PATH_LOCALTIME "/etc/localtime" -#define PATH_WALL_CMOS_CLOCK "/etc/wall_cmos_clock" -#define PATH_ZONEINFO "/usr/share/zoneinfo" diff --git a/usr.sbin/tzsetup/paths.h b/usr.sbin/tzsetup/paths.h new file mode 100644 index 0000000..10a6812 --- /dev/null +++ b/usr.sbin/tzsetup/paths.h @@ -0,0 +1,4 @@ +#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" diff --git a/usr.sbin/tzsetup/tzmenu.c b/usr.sbin/tzsetup/tzmenu.c deleted file mode 100644 index dbcba71..0000000 --- a/usr.sbin/tzsetup/tzmenu.c +++ /dev/null @@ -1,186 +0,0 @@ -/* - * Copyright 1995 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. - * - * 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. - */ - -static const char rcsid[] = - "$Id: tzmenu.c,v 1.4 1996/03/31 09:55:02 joerg Exp $"; - -#include <stdio.h> -#include <ncurses.h> -#include <dialog.h> -#include <limits.h> -#include <unistd.h> -#include <stdlib.h> -#include <sys/ioctl.h> - -#include "tzsetup.h" - -static const char *regmenu[] = { - "1", "Africa", - "2", "Asia", - "3", "Atlantic Ocean islands", - "4", "Australia", - "5", "Europe (including Russia)", - "6", "Indian Ocean islands", - "7", "North and South America", - "8", "Pacific Ocean islands" -}; - -static struct region *regions[] = { - &Africa, - &Asia, - &Atlantic, - &Australia, - &Europe, - &Indian, - &America, - &Pacific -}; - -#define NREGIONS 8 -#define DEFAULT_NROWS 24 /* default height of tty */ - -static const char *country_menu(const struct region *, const char *); - -const char * -tzmenu(void) -{ - unsigned char rbuf[_POSIX2_LINE_MAX]; - int rv; - int item = 0; - int sc = 0; - const char *res; - - while(1) { - dialog_clear(); - rv = dialog_menu("Timezone Selector", - "Select a region", - NREGIONS + 6, - 78, - NREGIONS, - NREGIONS, - (unsigned char **)regmenu, - rbuf, - &item, - &sc); - - - if (rv != 0) { - return 0; - } - - res = country_menu(regions[item], - regmenu[2 * item + 1]); - - if (res) - return res; - } -} - -static const char *location_menu(const struct country *, const char *); - -static const char * -country_menu(const struct region *reg, const char *name) -{ - unsigned char rbuf[_POSIX2_LINE_MAX]; - unsigned char title[_POSIX2_LINE_MAX]; - int rv; - int item = 0; - int sc = 0; - const char *res; - - snprintf(title, sizeof title, "Timezone Selector - %s", name); - - while(1) { - dialog_clear(); - rv = dialog_menu(title, "Select a country", - reg->r_count > LINES - 6 ? - LINES : reg->r_count + 6, - 78, - reg->r_count > LINES - 6 ? - LINES - 6 : reg->r_count, - reg->r_count, - (unsigned char **)reg->r_menu, - rbuf, - &item, - &sc); - - - if (rv != 0) { - return 0; - } - - sscanf(rbuf, "%d", &rv); - - res = location_menu(reg->r_ctrylist[rv - 1], - reg->r_menu[2 * (rv - 1) + 1]); - - if (res) - return res; - } -} - -static const char * -location_menu(const struct country *ctry, const char *name) -{ - unsigned char rbuf[_POSIX2_LINE_MAX]; - unsigned char title[_POSIX2_LINE_MAX]; - int rv; - int item = 0; - int sc = 0; - - snprintf(title, sizeof title, "Timezone Selector - %s", name); - - while(1) { - dialog_clear(); - rv = dialog_menu(title, "Select a location", - ctry->c_count > LINES - 6? - LINES : ctry->c_count + 6, - 78, - ctry->c_count > LINES - 6? - LINES - 6 : ctry->c_count, - ctry->c_count, - (unsigned char **)ctry->c_menu, - rbuf, - &item, - &sc); - - - if (rv != 0) { - return 0; - } - - sscanf(rbuf, "%d", &rv); - - rv = setzone(ctry->c_filelist[rv - 1]); - - if (rv == 0) - return ctry->c_filelist[rv - 1]; - } -} - diff --git a/usr.sbin/tzsetup/tzsetup.c b/usr.sbin/tzsetup/tzsetup.c new file mode 100644 index 0000000..f0a60cc --- /dev/null +++ b/usr.sbin/tzsetup/tzsetup.c @@ -0,0 +1,651 @@ +/* + * 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. + * + * 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. + * + * $Id$ + */ + +/* + * Second attempt at a `tzmenu' program, using the separate description + * files provided in newer tzdata releases. + */ + +#include <sys/types.h> +#include <dialog.h> +#include <err.h> +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include <sys/fcntl.h> +#include <sys/queue.h> +#include <sys/stat.h> + +#include "paths.h" + +static int reallydoit = 1; + +static int continent_country_menu(dialogMenuItem *); +static int set_zone_multi(dialogMenuItem *); +static int set_zone_whole_country(dialogMenuItem *); +static int set_zone_menu(dialogMenuItem *); + +struct continent { + dialogMenuItem *menu; + int nitems; + int ch; + int sc; +}; + +static struct continent africa, america, antarctica, arctic, asia, atlantic; +static struct continent australia, europe, indian, pacific; + +static struct continent_names { + char *name; + struct continent *continent; +} continent_names[] = { + { "Africa", &africa }, { "America", &america }, + { "Antarctica", &antarctica }, { "Arctic", &arctic }, + { "Asia", &asia }, + { "Atlantic", &atlantic }, { "Australia", &australia }, + { "Europe", &europe }, { "Indian", &indian }, { "Pacific", &pacific } +}; + +static dialogMenuItem continents[] = { + { "1", "Africa", 0, continent_country_menu, 0, &africa }, + { "2", "America -- North and South", 0, continent_country_menu, 0, + &america }, + { "3", "Antarctica", 0, continent_country_menu, 0, &antarctica }, + { "4", "Arctic Ocean", 0, continent_country_menu, 0, &arctic }, + { "5", "Asia", 0, continent_country_menu, 0, &asia }, + { "6", "Atlantic Ocean", 0, continent_country_menu, 0, &atlantic }, + { "7", "Australia", 0, continent_country_menu, 0, &australia }, + { "8", "Europe", 0, continent_country_menu, 0, &europe }, + { "9", "Indian Ocean", 0, continent_country_menu, 0, &indian }, + { "0", "Pacific Ocean", 0, continent_country_menu, 0, &pacific } +}; +#define NCONTINENTS ((sizeof continents)/(sizeof continents[0])) +#define OCEANP(x) ((x) == 3 || (x) == 5 || (x) == 8 || (x) == 9) + +static int +continent_country_menu(dialogMenuItem *continent) +{ + int rv; + struct continent *contp = continent->data; + char title[256]; + int isocean = OCEANP(continent - continents); + int menulen; + + /* Short cut -- if there's only one country, don't post a menu. */ + if (contp->nitems == 1) { + return set_zone_menu(&contp->menu[0]); + } + + /* It's amazing how much good grammar really matters... */ + if (!isocean) + snprintf(title, sizeof title, "Countries in %s", + continent->title); + else + snprintf(title, sizeof title, "Islands and groups in the %s", + continent->title); + + menulen = contp->nitems < 16 ? contp->nitems : 16; + rv = dialog_menu(title, (isocean ? "Select an island or group" + : "Select a country"), -1, -1, menulen, + -contp->nitems, contp->menu, 0, &contp->ch, + &contp->sc); + if (rv == 0) + return DITEM_LEAVE_MENU; + return DITEM_RECREATE; +} + +static struct continent * +find_continent(const char *name) +{ + int i; + + for (i = 0; i < NCONTINENTS; i++) { + if (strcmp(name, continent_names[i].name) == 0) + return continent_names[i].continent; + } + return 0; +} + +struct country { + 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 */ +}; + +struct zone { + TAILQ_ENTRY(zone) link; + char *descr; + char *filename; + 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. + */ +static struct country countries[26*26]; +#define CODE2INT(s) ((s[0] - 'A') * 26 + (s[1] - 'A')) + +/* + * 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) +{ + FILE *fp; + char *s, *t, *name; + size_t len; + int lineno; + struct country *cp; + + fp = fopen(_PATH_ISO3166, "r"); + if (!fp) + err(1, _PATH_ISO3166); + lineno = 0; + + while ((s = fgetln(fp, &len)) != 0) { + lineno++; + if (s[len - 1] != '\n') + errx(1, _PATH_ISO3166 ":%d: invalid format", lineno); + s[len - 1] = '\0'; + if (s[0] == '#') + continue; + + /* Isolate the two-letter code. */ + t = strsep(&s, "\t"); + if (t == 0 || strlen(t) != 2) + errx(1, _PATH_ISO3166 ":%d: invalid format", lineno); + if (t[0] < 'A' || t[0] > 'Z' || t[1] < 'A' || t[1] > 'Z') + errx(1, _PATH_ISO3166 ":%d: invalid code `%s'", + lineno, t); + + /* Now skip past the three-letter and numeric codes. */ + name = strsep(&s, "\t"); /* 3-let */ + if (name == 0 || strlen(name) != 3) + errx(1, _PATH_ISO3166 ":%d: invalid format", lineno); + name = strsep(&s, "\t"); /* numeric */ + if (name == 0 || strlen(name) != 3) + errx(1, _PATH_ISO3166 ":%d: invalid format", lineno); + + name = s; + + cp = &countries[CODE2INT(t)]; + if (cp->name) + errx(1, _PATH_ISO3166 + ":%d: country code `%s' multiply defined: %s", + lineno, t, cp->name); + cp->name = strdup(name); + cp->tlc = strdup(t); + } + + fclose(fp); +} + +static void +add_zone_to_country(int lineno, const char *tlc, const char *descr, + const char *file, struct continent *cont) +{ + struct zone *zp; + struct country *cp; + + if (tlc[0] < 'A' || tlc[0] > 'Z' || tlc[1] < 'A' || tlc[1] > 'Z') + errx(1, _PATH_ZONETAB ":%d: country code `%s' invalid", + lineno, tlc); + + cp = &countries[CODE2INT(tlc)]; + if (cp->name == 0) + errx(1, _PATH_ZONETAB ":%d: country code `%s' unknown", + lineno, tlc); + + if (descr) { + if (cp->nzones < 0) + errx(1, _PATH_ZONETAB + ":%d: conflicting zone definition", lineno); + + zp = malloc(sizeof *zp); + if (zp == 0) + err(1, "malloc(%lu)", (unsigned long)sizeof *zp); + + if (cp->nzones == 0) + TAILQ_INIT(&cp->zones); + + zp->descr = strdup(descr); + zp->filename = strdup(file); + zp->continent = cont; + TAILQ_INSERT_TAIL(&cp->zones, zp, link); + cp->nzones++; + } else { + if (cp->nzones > 0) + errx(1, _PATH_ZONETAB + ":%d: zone must have description", lineno); + if (cp->nzones < 0) + errx(1, _PATH_ZONETAB + ":%d: zone multiply defined", lineno); + cp->nzones = -1; + cp->filename = strdup(file); + 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) + return 0; + if (a->name == 0 && b->name != 0) + return 1; + if (b->name == 0) + return -1; + + return strcmp(a->name, b->name); +} + +/* + * This must be done AFTER all zone descriptions are read, since it breaks + * CODE2INT(). + */ +static void +sort_countries(void) +{ + qsort(countries, 576, sizeof countries[0], compare_countries); +} + +static void +read_zones(void) +{ + FILE *fp; + char *line; + size_t len; + int lineno; + char *tlc, *coord, *file, *descr, *p; + char contbuf[16]; + struct continent *cont; + + fp = fopen(_PATH_ZONETAB, "r"); + if (!fp) + err(1, _PATH_ZONETAB); + lineno = 0; + + while ((line = fgetln(fp, &len)) != 0) { + lineno++; + if (line[len - 1] != '\n') + errx(1, _PATH_ZONETAB ":%d: invalid format", lineno); + line[len - 1] = '\0'; + if (line[0] == '#') + continue; + + tlc = strsep(&line, "\t"); + if (strlen(tlc) != 2) + errx(1, _PATH_ZONETAB ":%d: invalid country code `%s'", + lineno, tlc); + coord = strsep(&line, "\t"); + file = strsep(&line, "\t"); + p = strchr(file, '/'); + if (p == 0) + errx(1, _PATH_ZONETAB ":%d: invalid zone name `%s'", + lineno, file); + contbuf[0] = '\0'; + strncat(contbuf, file, p - file); + cont = find_continent(contbuf); + if (!cont) + errx(1, _PATH_ZONETAB ":%d: invalid region `%s'", + lineno, contbuf); + + descr = (line && *line) ? line : 0; + + add_zone_to_country(lineno, tlc, descr, file, cont); + } + fclose(fp); +} + +static void +make_menus(void) +{ + struct country *cp; + struct zone *zp, *zp2; + struct continent *cont; + dialogMenuItem *dmi; + int i; + + /* + * 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 { + for (zp = cp->zones.tqh_first; zp; + zp = zp->link.tqe_next) { + cont = zp->continent; + for (zp2 = cp->zones.tqh_first; + zp2->continent != cont; + zp2 = zp2->link.tqe_next) + ; + if (zp2 == zp) + zp->continent->nitems++; + } + } + } + + /* + * Now allocate memory for the country menus. We set + * nitems back to zero so that we can use it for counting + * again when we actually build the menus. + */ + for (i = 0; i < NCONTINENTS; i++) { + continent_names[i].continent->menu = + malloc(sizeof(dialogMenuItem) * + continent_names[i].continent->nitems); + if (continent_names[i].continent->menu == 0) + err(1, "malloc for continent menu"); + continent_names[i].continent->nitems = 0; + } + + /* + * 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]; + memset(dmi, 0, sizeof *dmi); + asprintf(&dmi->prompt, "%d", + ++cp->continent->nitems); + dmi->title = cp->name; + dmi->checked = 0; + dmi->fire = set_zone_whole_country; + dmi->selected = 0; + dmi->data = cp; + } else { + cp->submenu = malloc(cp->nzones * sizeof *dmi); + if (cp->submenu == 0) + err(1, "malloc for submenu"); + cp->nzones = 0; + for (zp = cp->zones.tqh_first; zp; + zp = zp->link.tqe_next) { + cont = zp->continent; + dmi = &cp->submenu[cp->nzones]; + memset(dmi, 0, sizeof *dmi); + asprintf(&dmi->prompt, "%d", + ++cp->nzones); + dmi->title = zp->descr; + dmi->checked = 0; + dmi->fire = set_zone_multi; + dmi->selected = 0; + dmi->data = zp; + + for (zp2 = cp->zones.tqh_first; + zp2->continent != cont; + zp2 = zp2->link.tqe_next) + ; + if (zp2 != zp) + continue; + + dmi = &cont->menu[cont->nitems]; + memset(dmi, 0, sizeof *dmi); + asprintf(&dmi->prompt, "%d", ++cont->nitems); + dmi->title = cp->name; + dmi->checked = 0; + dmi->fire = set_zone_menu; + dmi->selected = 0; + dmi->data = cp; + } + } + } +} + +static int +set_zone_menu(dialogMenuItem *dmi) +{ + int rv; + char buf[256]; + struct country *cp = dmi->data; + int menulen; + + snprintf(buf, sizeof buf, "%s Time Zones", cp->name); + menulen = cp->nzones < 16 ? cp->nzones : 16; + rv = dialog_menu(buf, "Select a zone which observes the same time as " + "your locality.", -1, -1, menulen, -cp->nzones, + cp->submenu, 0, 0, 0); + if (rv != 0) + return DITEM_RECREATE; + return DITEM_LEAVE_MENU; +} + +static int +install_zone_file(const char *filename) +{ + struct stat sb; + int fd1, fd2; + int copymode; + char *msg; + ssize_t len; + char buf[1024]; + + if (lstat(_PATH_LOCALTIME, &sb) < 0) + /* Nothing there yet... */ + copymode = 1; + else if(S_ISLNK(sb.st_mode)) + copymode = 0; + else + copymode = 1; + + if (copymode) + asprintf(&msg, "Copying %s to " _PATH_LOCALTIME, filename); + else + asprintf(&msg, "Creating symbolic link " _PATH_LOCALTIME + " to %s", filename); + + dialog_notify(msg); + free(msg); + + if (reallydoit) { + if (copymode) { + fd1 = open(filename, O_RDONLY, 0); + if (fd1 < 0) { + asprintf(&msg, "Could not open %s: %s", + filename, strerror(errno)); + dialog_mesgbox("Error", msg, 8, 72); + free(msg); + return DITEM_FAILURE | DITEM_RECREATE; + } + + unlink(_PATH_LOCALTIME); + fd2 = open(_PATH_LOCALTIME, + O_CREAT | O_EXCL | O_WRONLY, + 0444); + if (fd2 < 0) { + asprintf(&msg, "Could not open " + _PATH_LOCALTIME ": %s", + strerror(errno)); + dialog_mesgbox("Error", msg, 8, 72); + free(msg); + return DITEM_FAILURE | DITEM_RECREATE; + } + + while ((len = read(fd1, buf, sizeof buf)) > 0) + len = write(fd2, buf, len); + + if (len == -1) { + asprintf(&msg, "Error copying %s to " + _PATH_LOCALTIME ": %s", + strerror(errno)); + dialog_mesgbox("Error", msg, 8, 72); + free(msg); + /* Better to leave none than a corrupt one. */ + unlink(_PATH_LOCALTIME); + return DITEM_FAILURE | DITEM_RECREATE; + } + close(fd1); + close(fd2); + } else { + if (access(filename, R_OK) != 0) { + asprintf(&msg, "Cannot access %s: %s", + filename, strerror(errno)); + dialog_mesgbox("Error", msg, 8, 72); + free(msg); + return DITEM_FAILURE | DITEM_RECREATE; + } + unlink(_PATH_LOCALTIME); + if (symlink(filename, _PATH_LOCALTIME) < 0) { + asprintf(&msg, "Cannot create symbolic link " + _PATH_LOCALTIME " to %s: %s", + filename, strerror(errno)); + dialog_mesgbox("Error", msg, 8, 72); + free(msg); + return DITEM_FAILURE | DITEM_RECREATE; + } + } + } + + if (copymode) + asprintf(&msg, "Copied timezone file from %s to " + _PATH_LOCALTIME, filename); + else + asprintf(&msg, "Created symbolic link from " _PATH_LOCALTIME + " to %s", filename); + + dialog_mesgbox("Done", msg, 8, 72); + free(msg); + return DITEM_LEAVE_MENU; +} + +static int +confirm_zone(const char *filename) +{ + char *msg; + struct tm *tm; + time_t t = time(0); + int rv; + + setenv("TZ", filename, 1); + tzset(); + tm = localtime(&t); + + asprintf(&msg, "Does the abbreviation `%s' look reasonable?", + tm->tm_zone); + rv = !dialog_yesno("Confirmation", msg, 4, 72); + free(msg); + return rv; +} + +static int +set_zone_multi(dialogMenuItem *dmi) +{ + char *fn; + struct zone *zp = dmi->data; + int rv; + + if (!confirm_zone(zp->filename)) + return DITEM_FAILURE | DITEM_RECREATE; + + asprintf(&fn, "%s/%s", _PATH_ZONEINFO, zp->filename); + rv = install_zone_file(fn); + free(fn); + return rv; +} + +static int +set_zone_whole_country(dialogMenuItem *dmi) +{ + char *fn; + struct country *cp = dmi->data; + int rv; + + if (!confirm_zone(cp->filename)) + return DITEM_FAILURE | DITEM_RECREATE; + + asprintf(&fn, "%s/%s", _PATH_ZONEINFO, cp->filename); + rv = install_zone_file(fn); + free(fn); + return rv; +} + +int +main(int argc, char **argv) +{ + int c; + int ch, sc; + + while ((c = getopt(argc, argv, "n")) != -1) { + switch(c) { + case 'n': + reallydoit = 0; + break; + + default: + fprintf(stderr, "%s: usage:\n\t%s [-n]\n", argv[0], + argv[0]); + exit(1); + } + } + + if (optind != argc) { + fprintf(stderr, "%s: usage:\n\t%s [-n]\n", argv[0], argv[0]); + exit(1); + } + + read_iso3166_table(); + read_zones(); + sort_countries(); + make_menus(); + + init_dialog(); + dialog_menu("Time Zone Selector", "Select a region", -1, -1, + NCONTINENTS, -NCONTINENTS, continents, 0, &ch, &sc); + end_dialog(); + return 0; +} + diff --git a/usr.sbin/tzsetup/tzsetup.h b/usr.sbin/tzsetup/tzsetup.h deleted file mode 100644 index bceff7e..0000000 --- a/usr.sbin/tzsetup/tzsetup.h +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright 1995 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. - * - * 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. - * - * $Id: tzsetup.h,v 1.1 1995/04/24 21:04:35 wollman Exp $ - */ - -struct country { - const char **c_filelist; - const char **c_menu; - int c_count; -}; - -struct region { - struct country **r_ctrylist; - const char **r_menu; - int r_count; -}; - -extern struct region Africa; /* all of Africa */ -extern struct region America; /* North and South America */ -extern struct region Asia; /* all of Asia */ -extern struct region Atlantic; /* Atlantic Ocean islands */ -extern struct region Australia; /* Australia */ -extern struct region Europe; /* all of Europe including Russia */ -extern struct region Indian; /* Indian Ocean islands */ -extern struct region Pacific; /* Pacific Ocean islands */ - -extern int setzone(const char *); -extern const char *tzmenu(void); - |