svn commit: r198267 - head/usr.sbin/tzsetup

Edwin Groothuis edwin at FreeBSD.org
Tue Oct 20 06:54:32 UTC 2009


Author: edwin
Date: Tue Oct 20 06:54:31 2009
New Revision: 198267
URL: http://svn.freebsd.org/changeset/base/198267

Log:
  Instead of having to know which timezone was picked last time, you
  now can run "tzsetup -r" which will reinstall the last choice. This
  data is recorded in /var/db/zoneinfo.
  
  MFC after:	 1 week

Modified:
  head/usr.sbin/tzsetup/tzsetup.8
  head/usr.sbin/tzsetup/tzsetup.c

Modified: head/usr.sbin/tzsetup/tzsetup.8
==============================================================================
--- head/usr.sbin/tzsetup/tzsetup.8	Tue Oct 20 05:40:51 2009	(r198266)
+++ head/usr.sbin/tzsetup/tzsetup.8	Tue Oct 20 06:54:31 2009	(r198267)
@@ -31,7 +31,7 @@
 .Nd set local timezone
 .Sh SYNOPSIS
 .Nm
-.Op Fl ns
+.Op Fl nrs
 .Op Ar zoneinfo file
 .Sh DESCRIPTION
 The
@@ -51,6 +51,9 @@ The following option is available:
 .Bl -tag -offset indent -width Fl
 .It Fl n
 Do not create or copy files.
+.It Fl r
+Reinstall the zoneinfo file installed last time. The name is obtained from
+.Pa /var/db/zoneinfo .
 .It Fl s 
 Skip the initial question about adjusting the clock if not set to
 .Tn UTC .
@@ -106,6 +109,8 @@ mapping of
 directory for zoneinfo files
 .It Pa /usr/share/zoneinfo/zone.tab
 mapping of timezone file to country and location
+.It Pa /var/db/zoneinfo
+saved name of the timezone file installed last.
 .El
 .Sh SEE ALSO
 .Xr date 1 ,

Modified: head/usr.sbin/tzsetup/tzsetup.c
==============================================================================
--- head/usr.sbin/tzsetup/tzsetup.c	Tue Oct 20 05:40:51 2009	(r198266)
+++ head/usr.sbin/tzsetup/tzsetup.c	Tue Oct 20 06:54:31 2009	(r198267)
@@ -45,6 +45,7 @@ __FBSDID("$FreeBSD$");
 #include <unistd.h>
 
 #include <sys/fcntl.h>
+#include <sys/param.h>
 #include <sys/queue.h>
 #include <sys/stat.h>
 
@@ -52,9 +53,11 @@ __FBSDID("$FreeBSD$");
 #define	_PATH_ISO3166		"/usr/share/misc/iso3166"
 #define	_PATH_ZONEINFO		"/usr/share/zoneinfo"
 #define	_PATH_LOCALTIME		"/etc/localtime"
+#define	_PATH_DB		"/var/db/zoneinfo"
 #define	_PATH_WALL_CMOS_CLOCK	"/etc/wall_cmos_clock"
 
 static int reallydoit = 1;
+static int reinstall = 0;
 
 static void	usage(void);
 static int	continent_country_menu(dialogMenuItem *);
@@ -495,13 +498,14 @@ set_zone_menu(dialogMenuItem *dmi)
 }
 
 static int
-install_zone_file(const char *filename)
+install_zone_file(const char *filename, int usedialog)
 {
 	char		buf[1024];
 	char		title[64], prompt[64];
 	struct stat	sb;
 	ssize_t		len;
 	int		fd1, fd2, copymode;
+	FILE		*f;
 
 	if (lstat(_PATH_LOCALTIME, &sb) < 0) {
 		/* Nothing there yet... */
@@ -519,7 +523,10 @@ install_zone_file(const char *filename)
 		snprintf(prompt, sizeof(prompt),
 		    "Creating symbolic link " _PATH_LOCALTIME " to %s",
 		    filename);
-	dialog_notify(prompt);
+	if (usedialog)
+		dialog_notify(prompt);
+	else
+		fprintf(stderr, "%s\n", prompt);
 #endif
 
 	if (reallydoit) {
@@ -530,7 +537,10 @@ install_zone_file(const char *filename)
 				snprintf(prompt, sizeof(prompt),
 				    "Could not open %s: %s", filename,
 				    strerror(errno));
-				dialog_mesgbox(title, prompt, 8, 72);
+				if (usedialog)
+					dialog_mesgbox(title, prompt, 8, 72);
+				else
+					fprintf(stderr, "%s\n", prompt);
 				return (DITEM_FAILURE | DITEM_RECREATE);
 			}
 
@@ -542,7 +552,10 @@ install_zone_file(const char *filename)
 				snprintf(prompt, sizeof(prompt),
 				    "Could not open " _PATH_LOCALTIME ": %s",
 				    strerror(errno));
-				dialog_mesgbox(title, prompt, 8, 72);
+				if (usedialog)
+					dialog_mesgbox(title, prompt, 8, 72);
+				else
+					fprintf(stderr, "%s\n", prompt);
 				return (DITEM_FAILURE | DITEM_RECREATE);
 			}
 
@@ -554,7 +567,10 @@ install_zone_file(const char *filename)
 				snprintf(prompt, sizeof(prompt),
 				    "Error copying %s to " _PATH_LOCALTIME
 				    ": %s", filename, strerror(errno));
-				dialog_mesgbox(title, prompt, 8, 72);
+				if (usedialog)
+					dialog_mesgbox(title, prompt, 8, 72);
+				else
+					fprintf(stderr, "%s\n", prompt);
 				/* Better to leave none than a corrupt one. */
 				unlink(_PATH_LOCALTIME);
 				return (DITEM_FAILURE | DITEM_RECREATE);
@@ -567,7 +583,10 @@ install_zone_file(const char *filename)
 				snprintf(prompt, sizeof(prompt),
 				    "Cannot access %s: %s", filename,
 				    strerror(errno));
-				dialog_mesgbox(title, prompt, 8, 72);
+				if (usedialog)
+					dialog_mesgbox(title, prompt, 8, 72);
+				else
+					fprintf(stderr, "%s\n", prompt);
 				return (DITEM_FAILURE | DITEM_RECREATE);
 			}
 			unlink(_PATH_LOCALTIME);
@@ -577,7 +596,10 @@ install_zone_file(const char *filename)
 				    "Cannot create symbolic link "
 				    _PATH_LOCALTIME " to %s: %s", filename,
 				    strerror(errno));
-				dialog_mesgbox(title, prompt, 8, 72);
+				if (usedialog)
+					dialog_mesgbox(title, prompt, 8, 72);
+				else
+					fprintf(stderr, "%s\n", prompt);
 				return (DITEM_FAILURE | DITEM_RECREATE);
 			}
 		}
@@ -592,8 +614,18 @@ install_zone_file(const char *filename)
 	else
 		snprintf(prompt, sizeof(prompt), "Created symbolic link from "
 		    _PATH_LOCALTIME " to %s", filename);
-	dialog_mesgbox(title, prompt, 8, 72);
+	if (usedialog)
+		dialog_mesgbox(title, prompt, 8, 72);
+	else
+		fprintf(stderr, "%s\n", prompt);
 #endif
+
+	/* Save knowledge for later */
+	if ((f = fopen(_PATH_DB, "w")) != NULL) {
+		fprintf(f, "%s\n", filename + strlen(_PATH_ZONEINFO) + 1);
+		fclose(f);
+	}
+
 	return (DITEM_LEAVE_MENU);
 }
 
@@ -627,7 +659,7 @@ set_zone_multi(dialogMenuItem *dmi)
 		return (DITEM_FAILURE | DITEM_RECREATE);
 
 	asprintf(&fn, "%s/%s", _PATH_ZONEINFO, zp->filename);
-	rv = install_zone_file(fn);
+	rv = install_zone_file(fn, 1);
 	free(fn);
 	return (rv);
 }
@@ -643,7 +675,7 @@ set_zone_whole_country(dialogMenuItem *d
 		return (DITEM_FAILURE | DITEM_RECREATE);
 
 	asprintf(&fn, "%s/%s", _PATH_ZONEINFO, cp->filename);
-	rv = install_zone_file(fn);
+	rv = install_zone_file(fn, 1);
 	free(fn);
 	return (rv);
 }
@@ -652,7 +684,7 @@ static void
 usage(void)
 {
 
-	fprintf(stderr, "usage: tzsetup [-ns]\n");
+	fprintf(stderr, "usage: tzsetup [-nrs] [zoneinfo file]\n");
 	exit(1);
 }
 
@@ -666,14 +698,17 @@ int
 main(int argc, char **argv)
 {
 	char		title[64], prompt[128];
-	int		c, fd, skiputc;
+	int		c, fd, rv, skiputc;
 
 	skiputc = 0;
-	while ((c = getopt(argc, argv, "ns")) != -1) {
+	while ((c = getopt(argc, argv, "nrs")) != -1) {
 		switch(c) {
 		case 'n':
 			reallydoit = 0;
 			break;
+		case 'r':
+			reinstall = 1;
+			break;
 		case 's':
 			skiputc = 1;
 			break;
@@ -693,6 +728,30 @@ main(int argc, char **argv)
 	sort_countries();
 	make_menus();
 
+	if (reinstall == 1) {
+		FILE *f;
+		char zonefile[MAXPATHLEN];
+
+		sprintf(zonefile, "%s/", _PATH_ZONEINFO);
+		if ((f = fopen(_PATH_DB, "r")) != NULL) {
+			if (fgets(zonefile + strlen(zonefile),
+			    sizeof(zonefile) - strlen(zonefile), f) != NULL) {
+				zonefile[sizeof(zonefile) - 1] = 0;
+				if (strlen(zonefile) > 0) {
+					zonefile[strlen(zonefile) - 1] = 0;
+					rv = install_zone_file(zonefile, 0);
+					exit(rv & ~DITEM_LEAVE_MENU);
+				}
+				errx(1, "Error reading %s.\n", _PATH_DB);
+			}
+			fclose(f);
+			errx(1,
+			    "Unable to determine earlier installed zoneinfo "
+			    "file. Check %s", _PATH_DB);
+		}
+		errx(1, "Cannot open %s for reading. Does it exist?", _PATH_DB);
+	}
+
 	init_dialog();
 	if (skiputc == 0) {
 		snprintf(title, sizeof(title),
@@ -724,10 +783,10 @@ main(int argc, char **argv)
 		snprintf(prompt, sizeof(prompt),
 		    "\nUse the default `%s' zone?", argv[optind]);
 		if (!dialog_yesno(title, prompt, 7, 72)) {
-			install_zone_file(argv[optind]);
+			rv = install_zone_file(argv[optind], 1);
 			dialog_clear();
 			end_dialog();
-			return (0);
+			exit(rv & ~DITEM_LEAVE_MENU);
 		}
 		dialog_clear_norefresh();
 	}


More information about the svn-src-all mailing list