[Toybox] [PATCH] macOS: replace local strnstr with strcasestr.

enh enh at google.com
Wed Nov 28 12:30:21 PST 2018


bionic, glibc, macOS, and musl all have strcasestr
(see http://man7.org/linux/man-pages/man3/strstr.3.html).

macOS (via BSD) has a strnstr that does what strnstr sounds like it
should do by analogy with strnlen and strncpy.

So we at least need to rename strnstr, but it probably makes more sense
just to switch to strcasestr instead.
---
 lib/lib.c         | 11 -----------
 lib/lib.h         |  1 -
 lib/portability.h |  3 +++
 toys/lsb/passwd.c |  4 ++--
 toys/posix/grep.c |  2 +-
 5 files changed, 6 insertions(+), 15 deletions(-)

diff --git a/lib/lib.c b/lib/lib.c
index 03f0a24..11acba0 100644
--- a/lib/lib.c
+++ b/lib/lib.c
@@ -1191,17 +1191,6 @@ char *next_printf(char *s, char **start)
   return 0;
 }

-// Posix inexplicably hasn't got this, so find str in line.
-char *strnstr(char *line, char *str)
-{
-  long len = strlen(str);
-  char *s;
-
-  for (s = line; *s; s++) if (!strncasecmp(s, str, len)) break;
-
-  return *s ? s : 0;
-}
-
 int dev_minor(int dev)
 {
   return ((dev&0xfff00000)>>12)|(dev&0xff);
diff --git a/lib/lib.h b/lib/lib.h
index e630bbc..f51aa68 100644
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -237,7 +237,6 @@ int qstrcmp(const void *a, const void *b);
 void create_uuid(char *uuid);
 char *show_uuid(char *uuid);
 char *next_printf(char *s, char **start);
-char *strnstr(char *line, char *str);
 int dev_minor(int dev);
 int dev_major(int dev);
 int dev_makedev(int major, int minor);
diff --git a/lib/portability.h b/lib/portability.h
index c2b29b6..dbb066a 100644
--- a/lib/portability.h
+++ b/lib/portability.h
@@ -90,6 +90,9 @@ char *dirname(char *path);
 char *__xpg_basename(char *path);
 static inline char *basename(char *path) { return __xpg_basename(path); }

+#include <string.h>
+char *strcasestr(const char *haystack, const char *needle);
+
 // When building under obsolete glibc (Ubuntu 8.04-ish), hold its hand a bit.
 #if __GLIBC__ == 2 && __GLIBC_MINOR__ < 10
 #define fstatat fstatat64
diff --git a/toys/lsb/passwd.c b/toys/lsb/passwd.c
index 0f51c0c..7302483 100644
--- a/toys/lsb/passwd.c
+++ b/toys/lsb/passwd.c
@@ -46,8 +46,8 @@ static void weak_check(char *new, char *old, char *user)

   if (strlen(new) < 6) msg = "too short";
   if (*new) {
-    if (strnstr(new, user) || strnstr(user, new)) msg = "user";
-    if (*old && (strnstr(new, old) || strnstr(old, new))) msg = "old";
+    if (strcasestr(new, user) || strcasestr(user, new)) msg = "user";
+    if (*old && (strcasestr(new, old) || strcasestr(old, new))) msg = "old";
   }
   if (msg) xprintf("BAD PASSWORD: %s\n",msg);
 }
diff --git a/toys/posix/grep.c b/toys/posix/grep.c
index f0332ce..14cebf9 100644
--- a/toys/posix/grep.c
+++ b/toys/posix/grep.c
@@ -152,7 +152,7 @@ static void do_grep(int fd, char *name)
             fseek.arg = s = line;
             break;
           }
-          if (toys.optflags & FLAG_i) s = strnstr(line, seek->arg);
+          if (toys.optflags & FLAG_i) s = strcasestr(line, seek->arg);
           else s = strstr(line, seek->arg);
           if (s) break;
         }
-- 
2.20.0.rc0.387.gc7a69e6b6c-goog
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-macOS-replace-local-strnstr-with-strcasestr.patch
Type: text/x-patch
Size: 3271 bytes
Desc: not available
URL: <http://lists.landley.net/pipermail/toybox-landley.net/attachments/20181128/6a6e69a9/attachment-0002.bin>


More information about the Toybox mailing list