From 4e1afcd6625dff04063733c6a8dfc3a8036daf27 Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Tue, 28 Dec 2021 19:19:32 +0100 Subject: [PATCH 1/5] Assume STDC_HEADERS will always be defined We're in 2021. C89 is everywhere; in fact, there are many other assumptions in the code that wouldn't probably hold on pre-standard C environments. Let's simplify and assume that C89 is available. The specific assumptions are that: - , and are available - strchr(3), strrchr(3), and strtok(3) are available - isalpha(3), isspace(3), isdigit(3), and isupper(3) are available I think we can safely assume we have all of those. Signed-off-by: Alejandro Colomar --- configure.ac | 1 - lib/defines.h | 12 ++---------- libmisc/getdate.y | 10 ++-------- 3 files changed, 4 insertions(+), 19 deletions(-) diff --git a/configure.ac b/configure.ac index c9aa37ab..13cbc8c4 100644 --- a/configure.ac +++ b/configure.ac @@ -38,7 +38,6 @@ dnl Checks for libraries. dnl Checks for header files. AC_HEADER_DIRENT -AC_HEADER_STDC AC_HEADER_SYS_WAIT AC_HEADER_STDBOOL diff --git a/lib/defines.h b/lib/defines.h index fc1521cb..08483940 100644 --- a/lib/defines.h +++ b/lib/defines.h @@ -61,16 +61,8 @@ extern char * textdomain (const char * domainname); #endif #endif -#if STDC_HEADERS -# include -# include -#else /* not STDC_HEADERS */ -# ifndef HAVE_STRCHR -# define strchr index -# define strrchr rindex -# endif -char *strchr (), *strrchr (), *strtok (); -#endif /* not STDC_HEADERS */ +#include +#include #if HAVE_ERRNO_H # include diff --git a/libmisc/getdate.y b/libmisc/getdate.y index 7afa995b..9b9f48c2 100644 --- a/libmisc/getdate.y +++ b/libmisc/getdate.y @@ -31,11 +31,7 @@ #include #include -#if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII)) -# define IN_CTYPE_DOMAIN(c) 1 -#else -# define IN_CTYPE_DOMAIN(c) isascii(c) -#endif +#define IN_CTYPE_DOMAIN(c) 1 #define ISSPACE(c) (IN_CTYPE_DOMAIN (c) && isspace (c)) #define ISALPHA(c) (IN_CTYPE_DOMAIN (c) && isalpha (c)) @@ -54,9 +50,7 @@ #include "getdate.h" -#if defined (STDC_HEADERS) -# include -#endif +#include /* Some old versions of bison generate parsers that use bcopy. That loses on systems that don't provide the function, so we have From 45d2e6dff0c03960bd8c9a4b352aa950aa17330d Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Tue, 28 Dec 2021 19:25:18 +0100 Subject: [PATCH 2/5] Remove IN_CTYPE_DOMAIN, which was always true The recent removal of STDC_HEADERS made IN_CTYPE_DOMAIN be defined to 1 unconditionally. Remove the now unnecessary definition, and propagate its truthness to expressions where it was used. Signed-off-by: Alejandro Colomar --- lib/defines.h | 2 +- libmisc/getdate.y | 10 ++++------ 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/lib/defines.h b/lib/defines.h index 08483940..6b1bb3cb 100644 --- a/lib/defines.h +++ b/lib/defines.h @@ -22,7 +22,7 @@ typedef unsigned char _Bool; # define __bool_true_false_are_defined 1 #endif -#define ISDIGIT_LOCALE(c) (IN_CTYPE_DOMAIN (c) && isdigit (c)) +#define ISDIGIT_LOCALE(c) isdigit (c) /* Take care of NLS matters. */ #ifdef S_SPLINT_S diff --git a/libmisc/getdate.y b/libmisc/getdate.y index 9b9f48c2..ce2235fd 100644 --- a/libmisc/getdate.y +++ b/libmisc/getdate.y @@ -31,12 +31,10 @@ #include #include -#define IN_CTYPE_DOMAIN(c) 1 - -#define ISSPACE(c) (IN_CTYPE_DOMAIN (c) && isspace (c)) -#define ISALPHA(c) (IN_CTYPE_DOMAIN (c) && isalpha (c)) -#define ISUPPER(c) (IN_CTYPE_DOMAIN (c) && isupper (c)) -#define ISDIGIT_LOCALE(c) (IN_CTYPE_DOMAIN (c) && isdigit (c)) +#define ISSPACE(c) isspace (c) +#define ISALPHA(c) isalpha (c) +#define ISUPPER(c) isupper (c) +#define ISDIGIT_LOCALE(c) isdigit (c) /* ISDIGIT differs from ISDIGIT_LOCALE, as follows: - Its arg may be any int or unsigned int; it need not be an unsigned char. From 2a41a72b8cfd05b9d6b050194ea6e1c877b9fd3b Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Tue, 28 Dec 2021 19:33:23 +0100 Subject: [PATCH 3/5] Use standard isspace(3), isalpha(3), and isupper(3) Due to the recent removal of IN_CTYPE_DOMAIN(), the uppercase macros that wrapped these standard calls are now defined to be equivalent. Therefore, there's no need for the wrappers, and it is much more readable to use the standard calls directly. However, hold on with ISDIGIT*(), since it's not so obvious what to do with it. Signed-off-by: Alejandro Colomar --- libmisc/getdate.y | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/libmisc/getdate.y b/libmisc/getdate.y index ce2235fd..4d282edc 100644 --- a/libmisc/getdate.y +++ b/libmisc/getdate.y @@ -31,9 +31,6 @@ #include #include -#define ISSPACE(c) isspace (c) -#define ISALPHA(c) isalpha (c) -#define ISUPPER(c) isupper (c) #define ISDIGIT_LOCALE(c) isdigit (c) /* ISDIGIT differs from ISDIGIT_LOCALE, as follows: @@ -643,7 +640,7 @@ static int LookupWord (char *buff) /* Make it lowercase. */ for (p = buff; '\0' != *p; p++) - if (ISUPPER (*p)) + if (isupper (*p)) *p = tolower (*p); if (strcmp (buff, "am") == 0 || strcmp (buff, "a.m.") == 0) @@ -724,7 +721,7 @@ static int LookupWord (char *buff) } /* Military timezones. */ - if (buff[1] == '\0' && ISALPHA (*buff)) + if (buff[1] == '\0' && isalpha (*buff)) { for (tp = MilitaryTable; tp->name; tp++) if (strcmp (buff, tp->name) == 0) @@ -763,7 +760,7 @@ yylex (void) for (;;) { - while (ISSPACE (*yyInput)) + while (isspace (*yyInput)) yyInput++; if (ISDIGIT (c = *yyInput) || c == '-' || c == '+') @@ -784,9 +781,9 @@ yylex (void) yylval.Number = -yylval.Number; return (0 != sign) ? tSNUMBER : tUNUMBER; } - if (ISALPHA (c)) + if (isalpha (c)) { - for (p = buff; (c = *yyInput++, ISALPHA (c)) || c == '.';) + for (p = buff; (c = *yyInput++, isalpha (c)) || c == '.';) if (p < &buff[sizeof buff - 1]) *p++ = c; *p = '\0'; From 44126d85ee9a33d10e24da1a8b901f8145918d95 Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Tue, 28 Dec 2021 19:38:58 +0100 Subject: [PATCH 4/5] Remove definition of ISDIGIT_LOCALE(c) It wasn't being used at all. Let's remove it. Use isdigit(3) directly in comments that referenced it. Also, in those comments, remove an outdated reference to the fact that ISDIGIT_LOCALE(c) might evaluate its argument more than once, which could be true a few commits ago, until IN_CTYPE_DEFINITION(c) was removed. Previously, the definition for ISDIGIT_LOCALE(c) was: #if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII)) # define IN_CTYPE_DOMAIN(c) 1 #else # define IN_CTYPE_DOMAIN(c) isascii(c) #endif #define ISDIGIT_LOCALE(c) (IN_CTYPE_DOMAIN (c) && isdigit (c)) Which could evaluate 'c' twice on pre-C89 systems (which I hope don't exist nowadays). Signed-off-by: Alejandro Colomar --- lib/defines.h | 2 -- libmisc/getdate.y | 7 ++----- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/lib/defines.h b/lib/defines.h index 6b1bb3cb..316cce5a 100644 --- a/lib/defines.h +++ b/lib/defines.h @@ -22,8 +22,6 @@ typedef unsigned char _Bool; # define __bool_true_false_are_defined 1 #endif -#define ISDIGIT_LOCALE(c) isdigit (c) - /* Take care of NLS matters. */ #ifdef S_SPLINT_S extern char *setlocale(int categories, const char *locale); diff --git a/libmisc/getdate.y b/libmisc/getdate.y index 4d282edc..82f1369c 100644 --- a/libmisc/getdate.y +++ b/libmisc/getdate.y @@ -31,14 +31,11 @@ #include #include -#define ISDIGIT_LOCALE(c) isdigit (c) - -/* ISDIGIT differs from ISDIGIT_LOCALE, as follows: +/* ISDIGIT differs from isdigit(3), as follows: - Its arg may be any int or unsigned int; it need not be an unsigned char. - - It's guaranteed to evaluate its argument exactly once. - It's typically faster. Posix 1003.2-1992 section 2.5.2.1 page 50 lines 1556-1558 says that - only '0' through '9' are digits. Prefer ISDIGIT to ISDIGIT_LOCALE unless + only '0' through '9' are digits. Prefer ISDIGIT to isdigit(3) unless it's important to use the locale's definition of `digit' even when the host does not conform to Posix. */ #define ISDIGIT(c) ((unsigned) (c) - '0' <= 9) From 8f134c0beab0cb6cc2fb2e662516e54e8177fa87 Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Tue, 28 Dec 2021 19:55:09 +0100 Subject: [PATCH 5/5] Use isdigit(3) instead of a reimplementation of it C89 defined isdigit as a function that tests for any decimal-digit character, defining the decimal digits as 0 1 2 3 4 5 6 7 8 9. I don't own a copy of C89 to check, but check in C17: 7.4.1.5 5.2.1 More specifically: > In both the source and execution basic character sets, the value > of each character after 0 in the above list of decimal digits > shall be one greater than the value of the previous. And since in ascii(7), the character after '9' is ':', it's highly unlikely that any implementation will ever accept any _decimal digit_ other than 0..9. POSIX simply defers to the ISO C standard. This is exactly what we wanted from ISDIGIT(c), so just use it. Non-standard implementations might have been slower or considered other characters as digits in the past, but let's assume implementations available today conform to ISO C89. Signed-off-by: Alejandro Colomar --- libmisc/getdate.y | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/libmisc/getdate.y b/libmisc/getdate.y index 82f1369c..0c07f746 100644 --- a/libmisc/getdate.y +++ b/libmisc/getdate.y @@ -31,15 +31,6 @@ #include #include -/* ISDIGIT differs from isdigit(3), as follows: - - Its arg may be any int or unsigned int; it need not be an unsigned char. - - It's typically faster. - Posix 1003.2-1992 section 2.5.2.1 page 50 lines 1556-1558 says that - only '0' through '9' are digits. Prefer ISDIGIT to isdigit(3) unless - it's important to use the locale's definition of `digit' even when the - host does not conform to Posix. */ -#define ISDIGIT(c) ((unsigned) (c) - '0' <= 9) - #include "getdate.h" #include @@ -760,18 +751,18 @@ yylex (void) while (isspace (*yyInput)) yyInput++; - if (ISDIGIT (c = *yyInput) || c == '-' || c == '+') + if (isdigit (c = *yyInput) || c == '-' || c == '+') { if (c == '-' || c == '+') { sign = c == '-' ? -1 : 1; - if (!ISDIGIT (*++yyInput)) + if (!isdigit (*++yyInput)) /* skip the '-' sign */ continue; } else sign = 0; - for (yylval.Number = 0; ISDIGIT (c = *yyInput++);) + for (yylval.Number = 0; isdigit (c = *yyInput++);) yylval.Number = 10 * yylval.Number + c - '0'; yyInput--; if (sign < 0)