Skip to content
Permalink
Browse files

Port zdump.c back to C89

Do not assume snprintf, since it was not standardized until C99.
* NEWS, Makefile: Mention this
* asctime.c: Resurrect comment about snprintf.
* zdump.c (HAVE_SNPRINTF): New symbol.
(snprintf) [!HAVE_SNPRINTF]: New function.
  • Loading branch information...
eggert committed Jun 13, 2017
1 parent 4f92a69 commit 399180580f65bcad63cf996997068366cadaf530
Showing with 42 additions and 0 deletions.
  1. +1 −0 Makefile
  2. +2 −0 NEWS
  3. +3 −0 asctime.c
  4. +36 −0 zdump.c
@@ -147,6 +147,7 @@ LDLIBS=
# localtime_rz can make zdump significantly faster, but is nonstandard.
# -DHAVE_POSIX_DECLS=0 if your system's include files do not declare
# functions like 'link' or variables like 'tzname' required by POSIX
# -DHAVE_SNPRINTF=0 if your system lacks the snprintf function
# -DHAVE_STDBOOL_H if you have a non-C99 compiler with <stdbool.h>
# -DHAVE_STDINT_H if you have a non-C99 compiler with <stdint.h>
# -DHAVE_STRFTIME_L if <time.h> declares locale_t and strftime_l
2 NEWS
@@ -86,6 +86,8 @@ Unreleased, experimental changes
localtime.c and difftime.c no longer require stdio.h, and .c files
other than zic.c no longer require sys/wait.h.

zdump.c no longer assumes snprintf. (

Several minor changes have been made to the code to make it a
bit easier to port to MS-Windows and Solaris. (Thanks to Kees
Dekker for reporting the problems.)
@@ -100,6 +100,9 @@ asctime_r(register const struct tm *timeptr, char *buf)
** (e.g., timeptr->tm_mday) when processing "%Y".
*/
strftime(year, sizeof year, "%Y", timeptr);
/*
** We avoid using snprintf since it's not available on all systems.
*/
sprintf(result,
((strlen(year) <= 4) ? ASCTIME_FMT : ASCTIME_FMT_B),
wn, mn,
36 zdump.c
@@ -22,6 +22,10 @@
#include "private.h"
#include <stdio.h>

#ifndef HAVE_SNPRINTF
# define HAVE_SNPRINTF (199901 <= __STDC_VERSION__)
#endif

#ifndef HAVE_LOCALTIME_R
# define HAVE_LOCALTIME_R 1
#endif
@@ -791,6 +795,38 @@ show(timezone_t tz, char *zone, time_t t, bool v)
abbrok(abbr(tmp), zone);
}

#if !HAVE_SNPRINTF
# include <stdarg.h>

/* A substitute for snprintf that is good enough for zdump. */
static int
snprintf(char *s, size_t size, char const *format, ...)
{
int n;
va_list args;
char const *arg;
size_t arglen, slen;
char buf[1024];
va_start(args, format);
if (strcmp(format, "%s") == 0) {
arg = va_arg(args, char const *);
arglen = strlen(arg);
} else {
n = vsprintf(buf, format, args);
if (n < 0)
return n;
arg = buf;
arglen = n;
}
slen = arglen < size ? arglen : size - 1;
memcpy(s, arg, slen);
s[slen] = '\0';
n = arglen <= INT_MAX ? arglen : -1;
va_end(args);
return n;
}
#endif

/* Store into BUF, of size SIZE, a formatted local time taken from *TM.
Use ISO 8601 format +HH:MM:SS. Omit :SS if SS is zero, and omit
:MM too if MM is also zero.

0 comments on commit 3991805

Please sign in to comment.
You can’t perform that action at this time.