]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
maint: prefer STREQ_LEN and STRPREFIX over strncmp in all cases
authorBernhard Voelker <mail@bernhard-voelker.de>
Tue, 17 Feb 2015 07:46:56 +0000 (08:46 +0100)
committerBernhard Voelker <mail@bernhard-voelker.de>
Tue, 17 Feb 2015 07:50:34 +0000 (08:50 +0100)
* cfg.mk (sc_prohibit_strncmp): Improve the search pattern: use
_sc_search_regexp to find all invocations of strncmp except when
used on a macro definition line; just match the function name with
an opening parenthesis.  Before, the expression missed places where
the comparison against 0 was in a subsequent line.
* src/system.h (STRNCMP_LIT): Shorten 'literal' to 'lit' to move
the whole definition of the macro into one line - thus making
sc_prohibit_strncmp pass.
(STRPREFIX): Add space before parenthesis.
* src/du.c (main): Prefer STREQ_LEN over strncmp.
* src/pinky.c (scan_entries): Likewise.
* src/tac.c (tac_seekable): Likewise.
* src/who.c (scan_entries): Likewise.

cfg.mk
src/du.c
src/pinky.c
src/system.h
src/tac.c
src/who.c

diff --git a/cfg.mk b/cfg.mk
index 7dc1a20c80e9d76fb07841c175434e74bd4fbb6b..ab0b8a4b6294290e9cd1c5379563bc442cf7b925 100644 (file)
--- a/cfg.mk
+++ b/cfg.mk
@@ -581,11 +581,9 @@ sc_space_before_open_paren:
 # Similar to the gnulib maint.mk rule for sc_prohibit_strcmp
 # Use STREQ_LEN or STRPREFIX rather than comparing strncmp == 0, or != 0.
 sc_prohibit_strncmp:
-       @grep -nE '! *str''ncmp *\(|\<str''ncmp *\(.+\) *[!=]='         \
-           $$($(VC_LIST_EXCEPT))                                       \
-         | grep -vE ':# *define STR(N?EQ_LEN|PREFIX)\(' &&             \
-         { echo '$(ME): use STREQ_LEN or STRPREFIX instead of str''ncmp' \
-               1>&2; exit 1; } || :
+       @prohibit='^[^#].*str''ncmp *\('                                \
+       halt='use STREQ_LEN or STRPREFIX instead of str''ncmp'          \
+         $(_sc_search_regexp)
 
 # Enforce recommended preprocessor indentation style.
 sc_preprocessor_indentation:
index 65fc0742f541605bd16d14540515dd4699fe9e0b..86827f808ebe6abebea3c14fcf2530f22764f740 100644 (file)
--- a/src/du.c
+++ b/src/du.c
@@ -968,9 +968,9 @@ main (int argc, char **argv)
             {
               /* Ignore "posix-" prefix, for compatibility with ls.  */
               static char const posix_prefix[] = "posix-";
-              while (strncmp (time_style, posix_prefix, sizeof posix_prefix - 1)
-                     == 0)
-                time_style += sizeof posix_prefix - 1;
+              static const size_t prefix_len = sizeof posix_prefix - 1;
+              while (STREQ_LEN (time_style, posix_prefix, prefix_len))
+                time_style += prefix_len;
             }
         }
 
index f1bf3407ce49a558286dc265029ccdfdb5283c2b..71650bfae7d8cb5bb0547de2df4fd8d0548e17e4 100644 (file)
@@ -445,8 +445,7 @@ scan_entries (size_t n, const STRUCT_UTMP *utmp_buf,
               int i;
 
               for (i = 0; i < argc_names; i++)
-                if (strncmp (UT_USER (utmp_buf), argv_names[i], UT_USER_SIZE)
-                    == 0)
+                if (STREQ_LEN (UT_USER (utmp_buf), argv_names[i], UT_USER_SIZE))
                   {
                     print_entry (utmp_buf);
                     break;
index b6c971d30a7e8fe7b8925fe5a9f9e54f67f5f8f0..46edd07d54c0d4adc6ca43870a2702f6a1fcd0c8 100644 (file)
@@ -193,12 +193,11 @@ select_plural (uintmax_t n)
 
 #define STREQ(a, b) (strcmp (a, b) == 0)
 #define STREQ_LEN(a, b, n) (strncmp (a, b, n) == 0)
-#define STRPREFIX(a, b) (strncmp(a, b, strlen (b)) == 0)
+#define STRPREFIX(a, b) (strncmp (a, b, strlen (b)) == 0)
 
 /* Just like strncmp, but the second argument must be a literal string
    and you don't specify the length;  that comes from the literal.  */
-#define STRNCMP_LIT(s, literal) \
-  strncmp (s, "" literal "", sizeof (literal) - 1)
+#define STRNCMP_LIT(s, lit) strncmp (s, "" lit "", sizeof (lit) - 1)
 
 #if !HAVE_DECL_GETLOGIN
 char *getlogin ();
index 9fce47208c00d1a11469bb6d0442d884a6c2b506..b69953f356a822b509724141c3af214a9984d81d 100644 (file)
--- a/src/tac.c
+++ b/src/tac.c
@@ -296,8 +296,8 @@ tac_seekable (int input_fd, const char *file, off_t file_pos)
         {
           /* 'match_length' is constant for non-regexp boundaries. */
           while (*--match_start != first_char
-                 || (match_length1 && strncmp (match_start + 1, separator1,
-                                               match_length1)))
+                 || (match_length1 && !STREQ_LEN (match_start + 1, separator1,
+                                                  match_length1)))
             /* Do nothing. */ ;
         }
 
index cc42d5f5bccce80dd8214faac940d9d8e2620a31..0a09411f4abec3d63cbb49a5b1b30f2a453a0e90 100644 (file)
--- a/src/who.c
+++ b/src/who.c
@@ -582,8 +582,8 @@ scan_entries (size_t n, const STRUCT_UTMP *utmp_buf)
   while (n--)
     {
       if (!my_line_only
-          || strncmp (ttyname_b, utmp_buf->ut_line,
-                      sizeof (utmp_buf->ut_line)) == 0)
+          || STREQ_LEN (ttyname_b, utmp_buf->ut_line,
+                        sizeof (utmp_buf->ut_line)))
         {
           if (need_users && IS_USER_PROCESS (utmp_buf))
             print_user (utmp_buf, boottime);