[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH] posixtm: avoid compiler warning in a better way
From: |
Paul Eggert |
Subject: |
[PATCH] posixtm: avoid compiler warning in a better way |
Date: |
Tue, 9 Dec 2014 18:12:51 -0800 |
* lib/posixtm.c (IF_LINT): Remove.
(year, posix_time_parse):
Return true (not 0) if successful. All callers changed.
(posix_time_parse): Simplify to pacify GCC without need for IF_LINT.
---
ChangeLog | 8 ++++++
lib/posixtm.c | 80 ++++++++++++++++++++++++-----------------------------------
2 files changed, 40 insertions(+), 48 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index f6f65c8..5fac19d 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2014-12-09 Paul Eggert <address@hidden>
+
+ posixtm: avoid compiler warning in a better way
+ * lib/posixtm.c (IF_LINT): Remove.
+ (year, posix_time_parse):
+ Return true (not 0) if successful. All callers changed.
+ (posix_time_parse): Simplify to pacify GCC without need for IF_LINT.
+
2014-12-08 KO Myung-Hun <address@hidden>
* lib/relocatable.c (relocate): Prepend $UNIXROOT to pathname if it is
diff --git a/lib/posixtm.c b/lib/posixtm.c
index 3023932..3e40f22 100644
--- a/lib/posixtm.c
+++ b/lib/posixtm.c
@@ -27,13 +27,6 @@
#include <sys/types.h>
#include <string.h>
-/* Use this to suppress gcc's "...may be used uninitialized" warnings. */
-#ifdef lint
-# define IF_LINT(Code) Code
-#else
-# define IF_LINT(Code) /* empty */
-#endif
-
#if USE_UNLOCKED_IO
# include "unlocked-io.h"
#endif
@@ -64,7 +57,7 @@
*/
-static int
+static bool
year (struct tm *tm, const int *digit_pair, size_t n, unsigned int syntax_bits)
{
switch (n)
@@ -77,14 +70,14 @@ year (struct tm *tm, const int *digit_pair, size_t n,
unsigned int syntax_bits)
if (digit_pair[0] <= 68)
{
if (syntax_bits & PDS_PRE_2000)
- return 1;
+ return false;
tm->tm_year += 100;
}
break;
case 2:
if (! (syntax_bits & PDS_CENTURY))
- return 1;
+ return false;
tm->tm_year = digit_pair[0] * 100 + digit_pair[1] - 1900;
break;
@@ -97,7 +90,7 @@ year (struct tm *tm, const int *digit_pair, size_t n,
unsigned int syntax_bits)
time (&now);
tmp = localtime (&now);
if (! tmp)
- return 1;
+ return false;
tm->tm_year = tmp->tm_year;
}
break;
@@ -106,10 +99,10 @@ year (struct tm *tm, const int *digit_pair, size_t n,
unsigned int syntax_bits)
abort ();
}
- return 0;
+ return true;
}
-static int
+static bool
posix_time_parse (struct tm *tm, const char *s, unsigned int syntax_bits)
{
const char *dot = NULL;
@@ -118,27 +111,25 @@ posix_time_parse (struct tm *tm, const char *s, unsigned
int syntax_bits)
size_t i;
size_t s_len = strlen (s);
- size_t len = (((syntax_bits & PDS_SECONDS) && (dot = strchr (s, '.')))
- ? (size_t) (dot - s)
- : s_len);
-
- if (len != 8 && len != 10 && len != 12)
- return 1;
+ size_t len = s_len;
- IF_LINT(if (len < 8) return 1;)
-
- if (dot)
+ if (syntax_bits & PDS_SECONDS)
{
- if (!(syntax_bits & PDS_SECONDS))
- return 1;
-
- if (s_len - len != 3)
- return 1;
+ dot = strchr (s, '.');
+ if (dot)
+ {
+ len = dot - s;
+ if (s_len - len != 3)
+ return false;
+ }
}
+ if (! (8 <= len && len <= 12 && len % 2 == 0))
+ return false;
+
for (i = 0; i < len; i++)
if (!ISDIGIT (s[i]))
- return 1;
+ return false;
len /= 2;
for (i = 0; i < len; i++)
@@ -147,8 +138,8 @@ posix_time_parse (struct tm *tm, const char *s, unsigned
int syntax_bits)
p = pair;
if (syntax_bits & PDS_LEADING_YEAR)
{
- if (year (tm, p, len - 4, syntax_bits))
- return 1;
+ if (! year (tm, p, len - 4, syntax_bits))
+ return false;
p += len - 4;
len = 4;
}
@@ -163,28 +154,19 @@ posix_time_parse (struct tm *tm, const char *s, unsigned
int syntax_bits)
/* Handle any trailing year. */
if (syntax_bits & PDS_TRAILING_YEAR)
{
- if (year (tm, p, len, syntax_bits))
- return 1;
+ if (! year (tm, p, len, syntax_bits))
+ return false;
}
/* Handle seconds. */
if (!dot)
- {
- tm->tm_sec = 0;
- }
+ tm->tm_sec = 0;
+ else if (ISDIGIT (dot[1]) && ISDIGIT (dot[2]))
+ tm->tm_sec = 10 * (dot[1] - '0') + dot[2] - '0';
else
- {
- int seconds;
-
- ++dot;
- if (!ISDIGIT (dot[0]) || !ISDIGIT (dot[1]))
- return 1;
- seconds = 10 * (dot[0] - '0') + dot[1] - '0';
-
- tm->tm_sec = seconds;
- }
+ return false;
- return 0;
+ return true;
}
/* Parse a POSIX-style date, returning true if successful. */
@@ -197,7 +179,7 @@ posixtime (time_t *p, const char *s, unsigned int
syntax_bits)
struct tm const *tm;
time_t t;
- if (posix_time_parse (&tm0, s, syntax_bits))
+ if (! posix_time_parse (&tm0, s, syntax_bits))
return false;
tm1 = tm0;
@@ -216,7 +198,9 @@ posixtime (time_t *p, const char *s, unsigned int
syntax_bits)
}
/* Reject dates like "September 31" and times like "25:61".
- Do not reject times that specify "60" as the number of seconds. */
+ However, allow a seconds count of 60 even in time zones that do
+ not support leap seconds, treating it as the following second;
+ POSIX requires this. */
if ((tm0.tm_year ^ tm->tm_year)
| (tm0.tm_mon ^ tm->tm_mon)
| (tm0.tm_mday ^ tm->tm_mday)
--
1.9.3
- [PATCH] posixtm: avoid compiler warning in a better way,
Paul Eggert <=