aboutsummaryrefslogtreecommitdiff
path: root/contrib/tzcode/date.c
diff options
context:
space:
mode:
authorDag-Erling Smørgrav <des@FreeBSD.org>2023-01-10 15:14:27 +0000
committerDag-Erling Smørgrav <des@FreeBSD.org>2023-01-31 09:44:19 +0000
commit93cc70bf9ca755bdfc43cf34cd762549fd9cba51 (patch)
treef4932a179e68b97990ab600895d182aac844ca32 /contrib/tzcode/date.c
parentd3ed105942bbd8457e0c307ddc9d8ac95b365c2b (diff)
Bring our tzcode up to date.
* Replay 2010[acflm] which had been merged but not recorded. * Merge 2010n. * Reorganize (unsplit) the code to match the upstream layout. * Merge 2022[cdefg]. MFC after: 1 week Sponsored by: Klara, Inc. (cherry picked from commit bc42155199b5b0b479311e05b07aee7f6f9c5172) tzcode: Address some compiler warnings. MFC after: 1 week Sponsored by: Klara, Inc. (cherry picked from commit d5c85ac6526dcd0ac25ebdb71969c8c788d5e13b) tzcode: Move configuration into separate header. MFC after: 1 week Sponsored by: Klara, Inc. (cherry picked from commit 394cf6719a3c665eabe24d96669409e0c5a78076) tzcode: Remove access() again, cf. 02ba1d993fa9. Reported by: Coverity (CID 1250126) MFC after: 1 week Sponsored by: Klara, Inc. Differential Revision: https://reviews.freebsd.org/D38035 (cherry picked from commit 3e2e5eebfaa0aebb2790bbde7f35d4c6c52e2596) tzcode: Avoid memory leak if pthread_setspecific() fails. Reported by: Coverity (CID 1018472, 1018474) MFC after: 1 week Sponsored by: Klara, Inc. Differential Revision: https://reviews.freebsd.org/D38036 (cherry picked from commit 96e68c393f65046c0cf69d29f89efcfee949e5de) tzcode: Fully initialize structs before use. Reported by: Coverity (CID 1502542, 1502548) MFC after: 1 week Sponsored by: Klara, Inc. Differential Revision: https://reviews.freebsd.org/D38037 (cherry picked from commit 55572cff58c4208a9e532356a245676b8eed6a3e) tzcode: Silence warnings. * Ignore failure to remove our temp file. * Avoid a minor memory leak in the -D case. Reported by: Coverity (CID 1502534, 1502535, 1502538) MFC after: 1 week Sponsored by: Klara, Inc. Differential Revision: https://reviews.freebsd.org/D38038 (cherry picked from commit 2aad7570f4e11ac94bb06c44cdc83ad1962fb97e) depend-cleanup.sh: handle zic moving one level up. MFC after: 1 week Sponsored by: Klara, Inc. Differential Revision: https://reviews.freebsd.org/D38044 (cherry picked from commit 4a158fc030b5a31efdbad8b30c9a859df553ae3c) libc: Update mktime(3) / timegm(3) tests. Sponsored by: Klara, Inc. Reviewed by: ngie Differential Revision: https://reviews.freebsd.org/D38177 (cherry picked from commit d8c9f80891872a54d0b21933136644d915096fe3) zoneinfo: On amd64, include 32-bit data. While there, drop the unnecessary posixrules option. Sponsored by: Klara, Inc. Reviewed by: imp, allanjude Differential Revision: https://reviews.freebsd.org/D38142 (cherry picked from commit 783c318fd1181d46554c31a5039db10e7f5eef49) tzcode: Document zic's -D option. Sponsored by: Klara, Inc. Reviewed by: allanjude Differential Revision: https://reviews.freebsd.org/D38141 (cherry picked from commit a7b1b73935f21a3dcd6fc8d86a37b92f0c9ba93f) zoneinfo: Always produce fat zoneinfo files. These aren't just needed for compatibility with i386 binaries (which need the 32-bit section), but potentially also for compatibility with older binaries on all platforms. Sponsored by: Klara, Inc. Reviewed by: emaste Differential Revision: https://reviews.freebsd.org/D38194 (cherry picked from commit f1021d27f73efb821213a973e1ab9366278c78f1) Fix cross-build from STABLE or older CURRENT. Previously, zic and tzsetup were both listed as install tools and basic bootstrap tools. Actually, tzsetup is an install tool while zic is a non-basic bootstrap tool. Fixes: 783c318fd118 Sponsored by: Klara, Inc. Reviewed by: jrtc27, emaste Differential Revision: https://reviews.freebsd.org/D38195 (cherry picked from commit 7a4a520064cb7fbff79560113682d6fe1a1ae9ee) Makefile.inc1: Restore adding tzsetup to _basic_bootstrap_tools ITOOLS is only the list of programs to make a copy of during install, it doesn't cause anything to be bootstrapped. Thus, by removing tzsetup from _basic_bootstrap_tools, we end up without it on non-FreeBSD, and so we error out trying to copy it to INSTALLTMP. Note that _basic_bootstrap_tools is only used for BOOTSTRAP_ALL_TOOLS (which is why zic was moved away from it). Should tzsetup evolve such that the host version is insufficient on older FreeBSD it can be moved to be more like zic, but that seems unnecessary for what is likely to remain a simple tool. This partially reverts commit 7a4a520064cb7fbff79560113682d6fe1a1ae9ee. Fixes: 7a4a520064cb ("Fix cross-build from STABLE or older CURRENT.") (cherry picked from commit d4fdfd3f9077216e52f38f7364974959c9e43168)
Diffstat (limited to 'contrib/tzcode/date.c')
-rw-r--r--contrib/tzcode/date.c216
1 files changed, 216 insertions, 0 deletions
diff --git a/contrib/tzcode/date.c b/contrib/tzcode/date.c
new file mode 100644
index 000000000000..11c5e5fe8d49
--- /dev/null
+++ b/contrib/tzcode/date.c
@@ -0,0 +1,216 @@
+/* Display or set the current time and date. */
+
+/* Copyright 1985, 1987, 1988 The Regents of the University of California.
+ 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.
+ 3. Neither the name of the University nor the names of its contributors
+ may be used to endorse or promote products derived from this software
+ without specific prior written permission.
+
+ THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 THE REGENTS 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. */
+
+#include "private.h"
+#include <locale.h>
+#include <stdio.h>
+
+#if !HAVE_POSIX_DECLS
+extern char * optarg;
+extern int optind;
+#endif
+
+static int retval = EXIT_SUCCESS;
+
+static void display(const char *, time_t);
+static void dogmt(void);
+static void errensure(void);
+static void timeout(FILE *, const char *, const struct tm *);
+static ATTRIBUTE_NORETURN void usage(void);
+
+int
+main(const int argc, char *argv[])
+{
+ register const char * format = "+%+";
+ register int ch;
+ register bool rflag = false;
+ time_t t;
+ intmax_t secs;
+ char * endarg;
+
+#ifdef LC_ALL
+ setlocale(LC_ALL, "");
+#endif /* defined(LC_ALL) */
+#if HAVE_GETTEXT
+# ifdef TZ_DOMAINDIR
+ bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR);
+# endif /* defined(TEXTDOMAINDIR) */
+ textdomain(TZ_DOMAIN);
+#endif /* HAVE_GETTEXT */
+ t = time(NULL);
+ while ((ch = getopt(argc, argv, "ucr:")) != EOF && ch != -1) {
+ switch (ch) {
+ default:
+ usage();
+ case 'u': /* do it in UT */
+ case 'c':
+ dogmt();
+ break;
+ case 'r': /* seconds since 1970 */
+ if (rflag) {
+ fprintf(stderr,
+ _("date: error: multiple -r's used"));
+ usage();
+ }
+ rflag = true;
+ errno = 0;
+ secs = strtoimax(optarg, &endarg, 0);
+ if (*endarg || optarg == endarg)
+ errno = EINVAL;
+ else if (! (TIME_T_MIN <= secs && secs <= TIME_T_MAX))
+ errno = ERANGE;
+ if (errno) {
+ perror(optarg);
+ errensure();
+ exit(retval);
+ }
+ t = secs;
+ break;
+ }
+ }
+ if (optind < argc) {
+ if (argc - optind != 1) {
+ fprintf(stderr,
+ _("date: error: multiple operands in command line\n"));
+ usage();
+ }
+ format = argv[optind];
+ if (*format != '+') {
+ fprintf(stderr, _("date: unknown operand: %s\n"), format);
+ usage();
+ }
+ }
+
+ display(format, t);
+ return retval;
+}
+
+static void
+dogmt(void)
+{
+ static char ** fakeenv;
+
+ if (fakeenv == NULL) {
+ static char tzeutc0[] = "TZ=UTC0";
+ ptrdiff_t from, to, n;
+
+ for (n = 0; environ[n] != NULL; ++n)
+ continue;
+#if defined ckd_add && defined ckd_mul
+ if (!ckd_add(&n, n, 2) && !ckd_mul(&n, n, sizeof *fakeenv)
+ && n <= SIZE_MAX)
+ fakeenv = malloc(n);
+#else
+ if (n <= min(PTRDIFF_MAX, SIZE_MAX) / sizeof *fakeenv - 2)
+ fakeenv = malloc((n + 2) * sizeof *fakeenv);
+#endif
+ if (fakeenv == NULL) {
+ fprintf(stderr, _("date: Memory exhausted\n"));
+ errensure();
+ exit(retval);
+ }
+ to = 0;
+ fakeenv[to++] = tzeutc0;
+ for (from = 1; environ[from] != NULL; ++from)
+ if (strncmp(environ[from], "TZ=", 3) != 0)
+ fakeenv[to++] = environ[from];
+ fakeenv[to] = NULL;
+ environ = fakeenv;
+ }
+}
+
+static void
+errensure(void)
+{
+ if (retval == EXIT_SUCCESS)
+ retval = EXIT_FAILURE;
+}
+
+static void
+usage(void)
+{
+ fprintf(stderr,
+ _("date: usage: date [-u] [-c] [-r seconds]"
+ " [+format]\n"));
+ errensure();
+ exit(retval);
+}
+
+static void
+display(char const *format, time_t now)
+{
+ struct tm *tmp;
+
+ tmp = localtime(&now);
+ if (!tmp) {
+ fprintf(stderr,
+ _("date: error: time out of range\n"));
+ errensure();
+ return;
+ }
+ timeout(stdout, format, tmp);
+ putchar('\n');
+ fflush(stdout);
+ fflush(stderr);
+ if (ferror(stdout) || ferror(stderr)) {
+ fprintf(stderr,
+ _("date: error: couldn't write results\n"));
+ errensure();
+ }
+}
+
+static void
+timeout(FILE *fp, char const *format, struct tm const *tmp)
+{
+ char *cp = NULL;
+ ptrdiff_t result;
+ ptrdiff_t size = 1024 / 2;
+
+ for ( ; ; ) {
+#ifdef ckd_mul
+ bool bigger = !ckd_mul(&size, size, 2) && size <= SIZE_MAX;
+#else
+ bool bigger = (size <= min(PTRDIFF_MAX, SIZE_MAX) / 2
+ && (size *= 2, true));
+#endif
+ char *newcp = bigger ? realloc(cp, size) : NULL;
+ if (!newcp) {
+ fprintf(stderr,
+ _("date: error: can't get memory\n"));
+ errensure();
+ exit(retval);
+ }
+ cp = newcp;
+ result = strftime(cp, size, format, tmp);
+ if (result != 0)
+ break;
+ }
+ fwrite(cp + 1, 1, result - 1, fp);
+ free(cp);
+}