Use xmemrchr() instead of strrchr()

This commit is contained in:
Arun Prakash Jana 2017-07-05 10:17:42 +05:30
parent 7b6e3c261d
commit 7bb1e4e4bf
No known key found for this signature in database
GPG key ID: A75979F35C080412

18
nnn.c
View file

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