]> Sergey Matveev's repositories - nnn.git/commitdiff
Use xmemrchr() instead of strrchr()
authorArun Prakash Jana <engineerarun@gmail.com>
Wed, 5 Jul 2017 04:47:42 +0000 (10:17 +0530)
committerArun Prakash Jana <engineerarun@gmail.com>
Wed, 5 Jul 2017 04:47:42 +0000 (10:17 +0530)
nnn.c

diff --git a/nnn.c b/nnn.c
index 7a82904316cb6172fc8dd0325150f23167e7767c..a5f666ed65249e93b9a53921869d34bf322c932c 100644 (file)
--- a/nnn.c
+++ b/nnn.c
@@ -347,23 +347,21 @@ xstrcmp(const char *s1, const char *s2)
 /*
  * The poor man's implementation of memrchr(3).
  * We are only looking for '/' in this program.
+ * Ideally 0 < n <= strlen(s).
  */
 static void *
-xmemrchr(const void *s, uchar ch, size_t n)
+xmemrchr(uchar *s, uchar ch, size_t n)
 {
        if (!s || !n)
                return NULL;
 
-       static uchar *p;
-
-       p = (uchar *)s + n - 1;
+       s = s + n - 1;
 
        while (n) {
-               if (*p == ch)
-                       return p;
+               if (*s == ch)
+                       return s;
 
-               --p;
-               --n;
+               --n, --s;
        }
 
        return NULL;
@@ -384,7 +382,7 @@ xdirname(const char *path)
        xstrlcpy(buf, path, PATH_MAX);
 
        /* Find last '/'. */
-       last_slash = strrchr(buf, '/');
+       last_slash = xmemrchr((uchar *)buf, '/', strlen(buf));
 
        if (last_slash != NULL && last_slash != buf && last_slash[1] == '\0') {
                /* Determine whether all remaining characters are slashes. */
@@ -396,7 +394,7 @@ xdirname(const char *path)
 
                /* The '/' is the last character, we have to look further. */
                if (runp != buf)
-                       last_slash = xmemrchr(buf, '/', runp - buf);
+                       last_slash = xmemrchr((uchar *)buf, '/', runp - buf);
        }
 
        if (last_slash != NULL) {