Patchwork libiberty patch committed: Add strnlen

login
register
mail settings
Submitter Ian Taylor
Date Sept. 18, 2012, 4:03 p.m.
Message ID <mcra9wnpbd4.fsf@google.com>
Download mbox | patch
Permalink /patch/184756/
State New
Headers show

Comments

Ian Taylor - Sept. 18, 2012, 4:03 p.m.
This patch to libiberty adds support for strnlen if it is not already
present.

I rebuilt the Makefile dependencies.  This revealed that maint-tool
wasn't recognizing that files that include dwarf2.h also depend on
dwarf2.def, so I fixed maint-tool.

I also rebuilt functions.texi, which had not been rebuilt for a while.

Bootstrapped on x86_64-unknown-linux-gnu, where, as expected, the
function was not compiled.

Ian


2012-09-18  Ian Lance Taylor  <iant@google.com>

	* 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.
Jakub Jelinek - Sept. 18, 2012, 4:25 p.m.
On Tue, Sep 18, 2012 at 09:03:03AM -0700, Ian Lance Taylor wrote:
> --- strnlen.c	(revision 0)
> +++ strnlen.c	(revision 0)
> @@ -0,0 +1,28 @@
> +/* 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"

Shouldn't this #include <stddef.h> for size_t, or is config.h providing
size_t?  Or #include <sys/types.h> ?  From what I can see, config.h doesn't
always define size_t, only if sys/types.h doesn't define it.
> +
> +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;
> +}

	Jakub
Ian Taylor - Sept. 18, 2012, 4:40 p.m.
On Tue, Sep 18, 2012 at 9:25 AM, Jakub Jelinek <jakub@redhat.com> wrote:
> On Tue, Sep 18, 2012 at 09:03:03AM -0700, Ian Lance Taylor wrote:
>> +*/
>> +
>> +#include "config.h"
>
> Shouldn't this #include <stddef.h> for size_t, or is config.h providing
> size_t?  Or #include <sys/types.h> ?  From what I can see, config.h doesn't
> always define size_t, only if sys/types.h doesn't define it.

Yes, the patch as committed does #include <stddef.h>.  There are other
files in libiberty that unconditionally #include <stddef.h> so I
figured that was portable enough.  I guess I forgot to update the
patch attachment after making that change.  Thanks for pointing it
out.

Ian

Patch

Index: configure.ac
===================================================================
--- configure.ac	(revision 191430)
+++ configure.ac	(working copy)
@@ -322,6 +322,7 @@  funcs="$funcs strchr"
 funcs="$funcs strdup"
 funcs="$funcs strncasecmp"
 funcs="$funcs strndup"
+funcs="$funcs strnlen"
 funcs="$funcs strrchr"
 funcs="$funcs strstr"
 funcs="$funcs strtod"
@@ -362,8 +363,8 @@  if test "x" = "y"; then
     random realpath rename rindex \
     sbrk setenv setproctitle setrlimit sigsetmask snprintf spawnve spawnvpe \
      stpcpy stpncpy strcasecmp strchr strdup \
-     strerror strncasecmp strndup strrchr strsignal strstr strtod strtol \
-     strtoul strverscmp sysconf sysctl sysmp \
+     strerror strncasecmp strndup strnlen strrchr strsignal strstr strtod \
+     strtol strtoul strverscmp sysconf sysctl sysmp \
     table times tmpnam \
     vasprintf vfprintf vprintf vsprintf \
     wait3 wait4 waitpid)
@@ -442,13 +443,14 @@  if test -n "${with_target_subdir}"; then
     AC_LIBOBJ([stpcpy])
     AC_LIBOBJ([stpncpy])
     AC_LIBOBJ([strndup])
+    AC_LIBOBJ([strnlen])
     AC_LIBOBJ([strverscmp])
     AC_LIBOBJ([vasprintf])
     AC_LIBOBJ([waitpid])
 
     for f in $funcs; do
       case "$f" in
-	asprintf | basename | bcmp | bcopy | bzero | clock | ffs | getpagesize | index | insque | mempcpy | mkstemps | random | rindex | sigsetmask | stpcpy | stpncpy | strdup | strndup | strverscmp | vasprintf | waitpid)
+	asprintf | basename | bcmp | bcopy | bzero | clock | ffs | getpagesize | index | insque | mempcpy | mkstemps | random | rindex | sigsetmask | stpcpy | stpncpy | strdup | strndup | strnlen | strverscmp | vasprintf | waitpid)
 	  ;;
 	*)
 	  n=HAVE_`echo $f | tr 'abcdefghijklmnopqrstuvwxyz' 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'`
Index: maint-tool
===================================================================
--- maint-tool	(revision 191430)
+++ maint-tool	(working copy)
@@ -222,7 +222,7 @@  sub deps {
 
     opendir(INC, $incdir);
     while ($f = readdir INC) {
-	next unless $f =~ /\.h$/;
+	next unless $f =~ /\.h$/ || $f =~ /\.def$/;
 	$mine{$f} = "\$(INCDIR)/$f";
 	$deps{$f} = join(' ', &deps_for("$incdir/$f"));
     }
Index: strnlen.c
===================================================================
--- strnlen.c	(revision 0)
+++ strnlen.c	(revision 0)
@@ -0,0 +1,28 @@ 
+/* 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"
+
+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;
+}
Index: Makefile.in
===================================================================
--- Makefile.in	(revision 191430)
+++ Makefile.in	(working copy)
@@ -151,7 +151,7 @@  CFILES = alloca.c argv.c asprintf.c atex
 	 spaces.c splay-tree.c stack-limit.c stpcpy.c stpncpy.c		\
 	 strcasecmp.c strchr.c strdup.c strerror.c strncasecmp.c	\
 	 strncmp.c strrchr.c strsignal.c strstr.c strtod.c strtol.c	\
-	 strtoul.c strndup.c strverscmp.c				\
+	 strtoul.c strndup.c strnlen.c strverscmp.c			\
 	timeval-utils.c tmpnam.c					\
 	unlink-if-ordinary.c						\
 	vasprintf.c vfork.c vfprintf.c vprintf.c vsnprintf.c vsprintf.c	\
@@ -215,9 +215,9 @@  CONFIGURED_OFILES = ./asprintf.$(objext)
 	 ./sigsetmask.$(objext) ./snprintf.$(objext)			\
 	 ./stpcpy.$(objext) ./stpncpy.$(objext) ./strcasecmp.$(objext)	\
 	 ./strchr.$(objext) ./strdup.$(objext) ./strncasecmp.$(objext)	\
-	 ./strncmp.$(objext) ./strndup.$(objext) ./strrchr.$(objext)	\
-	 ./strstr.$(objext) ./strtod.$(objext) ./strtol.$(objext)	\
-	 ./strtoul.$(objext) ./strverscmp.$(objext)			\
+	 ./strncmp.$(objext) ./strndup.$(objext) ./strnlen.$(objext)	\
+	 ./strrchr.$(objext) ./strstr.$(objext) ./strtod.$(objext)	\
+	 ./strtol.$(objext) ./strtoul.$(objext) ./strverscmp.$(objext)	\
 	./tmpnam.$(objext)						\
 	./vasprintf.$(objext) ./vfork.$(objext) ./vfprintf.$(objext)	\
 	 ./vprintf.$(objext) ./vsnprintf.$(objext) ./vsprintf.$(objext)	\
@@ -622,8 +622,8 @@  $(CONFIGURED_OFILES): stamp-picdir
 	else true; fi
 	$(COMPILE.c) $(srcdir)/crc32.c $(OUTPUT_OPTION)
 
-./dwarfnames.$(objext): $(srcdir)/dwarfnames.c $(INCDIR)/dwarf2.h \
-	$(INCDIR)/dwarf2.def
+./dwarfnames.$(objext): $(srcdir)/dwarfnames.c $(INCDIR)/dwarf2.def \
+	$(INCDIR)/dwarf2.h
 	if [ x"$(PICFLAG)" != x ]; then \
 	  $(COMPILE.c) $(PICFLAG) $(srcdir)/dwarfnames.c -o pic/$@; \
 	else true; fi
@@ -656,7 +656,8 @@  $(CONFIGURED_OFILES): stamp-picdir
 	else true; fi
 	$(COMPILE.c) $(srcdir)/fibheap.c $(OUTPUT_OPTION)
 
-./filename_cmp.$(objext): $(srcdir)/filename_cmp.c config.h $(INCDIR)/filenames.h \
+./filename_cmp.$(objext): $(srcdir)/filename_cmp.c config.h $(INCDIR)/ansidecl.h \
+	$(INCDIR)/filenames.h $(INCDIR)/hashtab.h \
 	$(INCDIR)/safe-ctype.h
 	if [ x"$(PICFLAG)" != x ]; then \
 	  $(COMPILE.c) $(PICFLAG) $(srcdir)/filename_cmp.c -o pic/$@; \
@@ -757,7 +758,7 @@  $(CONFIGURED_OFILES): stamp-picdir
 	$(COMPILE.c) $(srcdir)/insque.c $(OUTPUT_OPTION)
 
 ./lbasename.$(objext): $(srcdir)/lbasename.c config.h $(INCDIR)/ansidecl.h \
-	$(INCDIR)/filenames.h $(INCDIR)/libiberty.h \
+	$(INCDIR)/filenames.h $(INCDIR)/hashtab.h $(INCDIR)/libiberty.h \
 	$(INCDIR)/safe-ctype.h
 	if [ x"$(PICFLAG)" != x ]; then \
 	  $(COMPILE.c) $(PICFLAG) $(srcdir)/lbasename.c -o pic/$@; \
@@ -1043,7 +1044,7 @@  $(CONFIGURED_OFILES): stamp-picdir
 	else true; fi
 	$(COMPILE.c) $(srcdir)/splay-tree.c $(OUTPUT_OPTION)
 
-./stack-limit.$(objext): $(srcdir)/stack-limit.c config.h
+./stack-limit.$(objext): $(srcdir)/stack-limit.c config.h $(INCDIR)/ansidecl.h
 	if [ x"$(PICFLAG)" != x ]; then \
 	  $(COMPILE.c) $(PICFLAG) $(srcdir)/stack-limit.c -o pic/$@; \
 	else true; fi
@@ -1104,6 +1105,12 @@  $(CONFIGURED_OFILES): stamp-picdir
 	else true; fi
 	$(COMPILE.c) $(srcdir)/strndup.c $(OUTPUT_OPTION)
 
+./strnlen.$(objext): $(srcdir)/strnlen.c config.h
+	if [ x"$(PICFLAG)" != x ]; then \
+	  $(COMPILE.c) $(PICFLAG) $(srcdir)/strnlen.c -o pic/$@; \
+	else true; fi
+	$(COMPILE.c) $(srcdir)/strnlen.c $(OUTPUT_OPTION)
+
 ./strrchr.$(objext): $(srcdir)/strrchr.c $(INCDIR)/ansidecl.h
 	if [ x"$(PICFLAG)" != x ]; then \
 	  $(COMPILE.c) $(PICFLAG) $(srcdir)/strrchr.c -o pic/$@; \