[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: RFC: don't use strncmp
From: |
Akim Demaille |
Subject: |
Re: RFC: don't use strncmp |
Date: |
Thu, 23 Feb 2012 20:22:41 +0100 |
Le 23 févr. 2012 à 20:21, Akim Demaille a écrit :
> This commit changes the behavior of Bison on when used
> with -o foo_tab.c. Before, it would create foo.output,
> recognizing that _tab is the same as .tab, and therefore
> should be omitted, now it would create foo_tab.output.
> In other words, I don't think that, today, our Unix
> platforms should care about foo.tab.c not being valid on
> M$ DOS.
>
> But I'll wait for a go before installing.
I hit the wrong button :)
From 53645905ea763faa4ec9d28260894c476a19b38e Mon Sep 17 00:00:00 2001
From: Akim Demaille <address@hidden>
Date: Thu, 23 Feb 2012 20:14:42 +0100
Subject: [PATCH] avoid direct strncmp calls.
Before this change, bison would accept either .tab and _tab equivalently,
whatever the current platform. Besides, it was not obeying everywhere
to the possible definition of TAB_EXT to something else than .tab.
For consistency, handle only TAB_EXT (".tab" on non DJGPP platforms).
Support for "_tab" is neither documented, nor tested.
* src/system.h (STRNCMP_LIT): New.
From Jim Meyering.
(STRPREFIX_LIT): New.
* src/files.c, src/getargs.c: Use it.
---
src/files.c | 5 ++---
src/getargs.c | 2 +-
src/system.h | 9 +++++++++
3 files changed, 12 insertions(+), 4 deletions(-)
diff --git a/src/files.c b/src/files.c
index ceb0489..550e42e 100644
--- a/src/files.c
+++ b/src/files.c
@@ -215,10 +215,9 @@ file_name_split (const char *file_name,
if (*ext)
{
size_t baselen = *ext - *base;
- size_t dottablen = 4;
+ size_t dottablen = sizeof (TAB_EXT) - 1;
if (dottablen < baselen
- && (strncmp (*ext - dottablen, ".tab", dottablen) == 0
- || strncmp (*ext - dottablen, "_tab", dottablen) == 0))
+ && STRPREFIX_LIT (TAB_EXT, *ext - dottablen))
*tab = *ext - dottablen;
}
}
diff --git a/src/getargs.c b/src/getargs.c
index 5e04ab0..06e6e97 100644
--- a/src/getargs.c
+++ b/src/getargs.c
@@ -102,7 +102,7 @@ flags_argmatch (const char *option,
args = strtok (args, ",");
while (args)
{
- int no = strncmp (args, "no-", 3) == 0 ? 3 : 0;
+ int no = STRPREFIX_LIT ("no-", args) ? 3 : 0;
int value = XARGMATCH (option, args + no, keys, values);
if (value == 0)
{
diff --git a/src/system.h b/src/system.h
index 7aa2c9f..f0a76ce 100644
--- a/src/system.h
+++ b/src/system.h
@@ -42,6 +42,15 @@
#define STREQ(L, R) (strcmp(L, R) == 0)
#define STRNEQ(L, R) (!STREQ(L, R))
+/* Just like strncmp, but the second argument must be a literal string
+ and you don't specify the length. */
+#define STRNCMP_LIT(S, Literal) \
+ strncmp (S, "" Literal "", sizeof (Literal) - 1)
+
+/* Whether Literal is a prefix of S. */
+#define STRPREFIX_LIT(Literal, S) \
+ (STRNCMP_LIT (S, Literal) == 0)
+
#if HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif
--
1.7.9