0002-Port-zdump-to-C90-snprintf.patch 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. From e231da4fb2beb17c60b4b1a5c276366d6a6e433f Mon Sep 17 00:00:00 2001
  2. From: Paul Eggert <eggert@cs.ucla.edu>
  3. Date: Mon, 23 Oct 2017 17:58:36 -0700
  4. Subject: [PATCH] Port zdump to C90 + snprintf
  5. MIME-Version: 1.0
  6. Content-Type: text/plain; charset=UTF-8
  7. Content-Transfer-Encoding: 8bit
  8. Problem reported by Jon Skeet in:
  9. https://mm.icann.org/pipermail/tz/2017-October/025362.html
  10. * NEWS: Mention this.
  11. * zdump.c (my_snprintf): New macro or function. If a macro, it is
  12. just snprintf. If a function, it is the same as the old snprintf
  13. static function, with an ATTRIBUTE_FORMAT to pacify modern GCC.
  14. All uses of snprintf changed to use my_snprintf. This way,
  15. installers don’t need to specify -DHAVE_SNPRINTF if they are using
  16. a pre-C99 compiler with a library that has snprintf.
  17. Upstream-Status: Backport
  18. Signed-off-by: Armin Kuster <akuster@mvista.com>
  19. ---
  20. NEWS | 4 ++++
  21. zdump.c | 29 ++++++++++++++++-------------
  22. 2 files changed, 20 insertions(+), 13 deletions(-)
  23. diff --git a/NEWS b/NEWS
  24. index 75ab095..dea08b8 100644
  25. --- a/NEWS
  26. +++ b/NEWS
  27. @@ -7,6 +7,10 @@ Unreleased, experimental changes
  28. The Makefile now quotes values like BACKWARD more carefully when
  29. passing them to the shell. (Problem reported by Zefram.)
  30. + Builders no longer need to specify -DHAVE_SNPRINTF on platforms
  31. + that have snprintf and use pre-C99 compilers. (Problem reported
  32. + by Jon Skeet.)
  33. +
  34. Release 2017c - 2017-10-20 14:49:34 -0700
  35. diff --git a/zdump.c b/zdump.c
  36. index 8e3bf3e..d4e6084 100644
  37. --- a/zdump.c
  38. +++ b/zdump.c
  39. @@ -795,12 +795,14 @@ show(timezone_t tz, char *zone, time_t t, bool v)
  40. abbrok(abbr(tmp), zone);
  41. }
  42. -#if !HAVE_SNPRINTF
  43. +#if HAVE_SNPRINTF
  44. +# define my_snprintf snprintf
  45. +#else
  46. # include <stdarg.h>
  47. /* A substitute for snprintf that is good enough for zdump. */
  48. -static int
  49. -snprintf(char *s, size_t size, char const *format, ...)
  50. +static int ATTRIBUTE_FORMAT((printf, 3, 4))
  51. +my_snprintf(char *s, size_t size, char const *format, ...)
  52. {
  53. int n;
  54. va_list args;
  55. @@ -839,10 +841,10 @@ format_local_time(char *buf, size_t size, struct tm const *tm)
  56. {
  57. int ss = tm->tm_sec, mm = tm->tm_min, hh = tm->tm_hour;
  58. return (ss
  59. - ? snprintf(buf, size, "%02d:%02d:%02d", hh, mm, ss)
  60. + ? my_snprintf(buf, size, "%02d:%02d:%02d", hh, mm, ss)
  61. : mm
  62. - ? snprintf(buf, size, "%02d:%02d", hh, mm)
  63. - : snprintf(buf, size, "%02d", hh));
  64. + ? my_snprintf(buf, size, "%02d:%02d", hh, mm)
  65. + : my_snprintf(buf, size, "%02d", hh));
  66. }
  67. /* Store into BUF, of size SIZE, a formatted UTC offset for the
  68. @@ -877,10 +879,10 @@ format_utc_offset(char *buf, size_t size, struct tm const *tm, time_t t)
  69. mm = off / 60 % 60;
  70. hh = off / 60 / 60;
  71. return (ss || 100 <= hh
  72. - ? snprintf(buf, size, "%c%02ld%02d%02d", sign, hh, mm, ss)
  73. + ? my_snprintf(buf, size, "%c%02ld%02d%02d", sign, hh, mm, ss)
  74. : mm
  75. - ? snprintf(buf, size, "%c%02ld%02d", sign, hh, mm)
  76. - : snprintf(buf, size, "%c%02ld", sign, hh));
  77. + ? my_snprintf(buf, size, "%c%02ld%02d", sign, hh, mm)
  78. + : my_snprintf(buf, size, "%c%02ld", sign, hh));
  79. }
  80. /* Store into BUF (of size SIZE) a quoted string representation of P.
  81. @@ -983,15 +985,16 @@ istrftime(char *buf, size_t size, char const *time_fmt,
  82. for (abp = ab; is_alpha(*abp); abp++)
  83. continue;
  84. len = (!*abp && *ab
  85. - ? snprintf(b, s, "%s", ab)
  86. + ? my_snprintf(b, s, "%s", ab)
  87. : format_quoted_string(b, s, ab));
  88. if (s <= len)
  89. return false;
  90. b += len, s -= len;
  91. }
  92. - formatted_len = (tm->tm_isdst
  93. - ? snprintf(b, s, &"\t\t%d"[show_abbr], tm->tm_isdst)
  94. - : 0);
  95. + formatted_len
  96. + = (tm->tm_isdst
  97. + ? my_snprintf(b, s, &"\t\t%d"[show_abbr], tm->tm_isdst)
  98. + : 0);
  99. }
  100. break;
  101. }
  102. --
  103. 2.7.4