summaryrefslogtreecommitdiff
path: root/libiberty/strnlen.c
diff options
context:
space:
mode:
authorIan Lance Taylor <iant@google.com>2012-09-18 16:03:01 +0000
committerIan Lance Taylor <ian@gcc.gnu.org>2012-09-18 16:03:01 +0000
commit9a9baa5254a06287326469f1b2f9ba1f7db6fd71 (patch)
treee4924912fd85dad90b6a534928ada8aac3840841 /libiberty/strnlen.c
parentfb5e0707d18e9fd0bc6a0e92b6571512aafd0e43 (diff)
downloadgcc-9a9baa5254a06287326469f1b2f9ba1f7db6fd71.tar.gz
strnlen.c: New file.
* strnlen.c: New file. * configure.ac: Check for strnlen, add it to AC_LIBOBJ if it's not present. * Makefile.in: Rebuild dependencies. (CFILES): Add strnlen.c. (CONFIGURED_OFILES): Add ./strnlen.$(objext). * configure, config.in, functions.texi: Rebuild. * maint-tool: Accept .def files in the include directory. From-SVN: r191432
Diffstat (limited to 'libiberty/strnlen.c')
-rw-r--r--libiberty/strnlen.c30
1 files changed, 30 insertions, 0 deletions
diff --git a/libiberty/strnlen.c b/libiberty/strnlen.c
new file mode 100644
index 00000000000..4934973adca
--- /dev/null
+++ b/libiberty/strnlen.c
@@ -0,0 +1,30 @@
+/* Portable version of strnlen.
+ This function is in the public domain. */
+
+/*
+
+@deftypefn Supplemental size_t strnlen (const char *@var{s}, size_t @var{maxlen})
+
+Returns the length of @var{s}, as with @code{strlen}, but never looks
+past the first @var{maxlen} characters in the string. If there is no
+'\0' character in the first @var{maxlen} characters, returns
+@var{maxlen}.
+
+@end deftypefn
+
+*/
+
+#include "config.h"
+
+#include <stddef.h>
+
+size_t
+strnlen (const char *s, size_t maxlen)
+{
+ size_t i;
+
+ for (i = 0; i < maxlen; ++i)
+ if (s[i] == '\0')
+ break;
+ return i;
+}