aboutsummaryrefslogtreecommitdiff
path: root/string
diff options
context:
space:
mode:
authorUlrich Drepper <drepper@redhat.com>2001-08-21 01:40:45 +0000
committerUlrich Drepper <drepper@redhat.com>2001-08-21 01:40:45 +0000
commit9b7783a79bf6a08f1abf5ed4b70853940843ced5 (patch)
tree5ff4290ad53aa03318d044852808a1fbdbaeeb9b /string
parent16195d2026aac75c590699400b602581e977d0f6 (diff)
downloadglibc-9b7783a79bf6a08f1abf5ed4b70853940843ced5.zip
glibc-9b7783a79bf6a08f1abf5ed4b70853940843ced5.tar.gz
glibc-9b7783a79bf6a08f1abf5ed4b70853940843ced5.tar.bz2
Fix the implementation. We cannot use memchr.
Diffstat (limited to 'string')
-rw-r--r--string/strnlen.c25
1 files changed, 19 insertions, 6 deletions
diff --git a/string/strnlen.c b/string/strnlen.c
index 83a6f44..a777b35 100644
--- a/string/strnlen.c
+++ b/string/strnlen.c
@@ -1,5 +1,5 @@
/* Find the length of STRING, but scan at most MAXLEN characters.
- Copyright (C) 1996, 1997, 1998, 2000 Free Software Foundation, Inc.
+ Copyright (C) 1996, 1997, 1998, 2000, 2001 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
@@ -19,13 +19,26 @@
#include <string.h>
-/* Find the length of STRING, but scan at most MAXLEN characters.
- If no '\0' terminator is found in that many characters, return MAXLEN. */
+/* Find the length of S, but scan at most MAXLEN characters. If no
+ '\0' terminator is found in that many characters, return MAXLEN. */
size_t
-__strnlen (const char *string, size_t maxlen)
+__strnlen (const char *s, size_t maxlen)
{
- const char *end = memchr (string, '\0', maxlen);
- return end ? (size_t) (end - string) : maxlen;
+ size_t len = 0;
+
+ while (s[len] != '\0' && maxlen > 0)
+ {
+ if (s[++len] == '\0' || --maxlen == 0)
+ return len;
+ if (s[++len] == '\0' || --maxlen == 0)
+ return len;
+ if (s[++len] == '\0' || --maxlen == 0)
+ return len;
+ ++len;
+ --maxlen;
+ }
+
+ return len;
}
weak_alias (__strnlen, strnlen)