As an addendum to my previous patch, I made an attempt to properly add
strnlen() to libiberty, with the code copied from gnulib. Unfortunately it seems I've messed it up somewhere since defining HAVE_STRNLEN to 0 doesn't seem to build strnlen.o for me. Any ideas?


Thanks,
Dimitris
=== modified file 'libiberty/Makefile.in'
--- libiberty/Makefile.in       2012-04-27 14:14:14 +0000
+++ libiberty/Makefile.in       2012-08-07 03:52:53 +0000
@@ -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 \
@@ -218,6 +218,7 @@ CONFIGURED_OFILES = ./asprintf.$(objext)
         ./strncmp.$(objext) ./strndup.$(objext) ./strrchr.$(objext)    \
         ./strstr.$(objext) ./strtod.$(objext) ./strtol.$(objext)       \
         ./strtoul.$(objext) ./strverscmp.$(objext)                     \
+       ./strnlen.$(objext)                                             \
        ./tmpnam.$(objext)                                              \
        ./vasprintf.$(objext) ./vfork.$(objext) ./vfprintf.$(objext)    \
         ./vprintf.$(objext) ./vsnprintf.$(objext) ./vsprintf.$(objext) \
@@ -622,8 +623,7 @@ $(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.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
+       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/$@; \

=== modified file 'libiberty/config.in'
--- libiberty/config.in 2011-07-22 08:33:37 +0000
+++ libiberty/config.in 2012-08-07 03:29:18 +0000
@@ -262,6 +262,9 @@
 /* Define to 1 if you have the `strndup' function. */
 #undef HAVE_STRNDUP
 
+/* Define to 1 if you have the `strnlen' function. */
+#undef HAVE_STRNLEN
+
 /* Define to 1 if you have the `strrchr' function. */
 #undef HAVE_STRRCHR
 

=== modified file 'libiberty/configure'
--- libiberty/configure 2012-01-23 06:25:28 +0000
+++ libiberty/configure 2012-08-07 03:10:54 +0000
@@ -5340,6 +5340,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"
@@ -5380,8 +5381,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

=== modified file 'libiberty/configure.ac'
--- libiberty/configure.ac      2011-08-22 16:54:02 +0000
+++ libiberty/configure.ac      2012-08-07 03:10:42 +0000
@@ -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)

=== modified file 'libiberty/functions.texi'
--- libiberty/functions.texi    2011-02-28 18:23:25 +0000
+++ libiberty/functions.texi    2012-08-07 03:46:29 +0000
@@ -1574,6 +1574,14 @@ memory was available.  The result is alw
 
 @end deftypefn
 
+@c strnlen.c:20
+@deftypefn Extension size_t strnlen (const char *@var{string}, size_t 
@var{maxlen})
+
+Find the length of @var{string}, but scan at most @var{maxlen} characters.  If
+no '\0' terminator is found in that many characters, return @var{maxlen}.
+
+@end deftypefn
+
 @c strrchr.c:6
 @deftypefn Supplemental char* strrchr (const char *@var{s}, int @var{c})
 

=== added file 'libiberty/strnlen.c'
--- libiberty/strnlen.c 1970-01-01 00:00:00 +0000
+++ libiberty/strnlen.c 2012-08-07 03:54:45 +0000
@@ -0,0 +1,37 @@
+/* Find the length of STRING, but scan at most MAXLEN characters.
+   Copyright (C) 2005-2007, 2009-2012 Free Software Foundation, Inc.
+   Written by Simon Josefsson.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, see <http://www.gnu.org/licenses/>.  */
+
+/*
+
+@deftypefn Extension size_t strnlen (const char *@var{string}, size_t 
@var{maxlen})
+
+Find the length of @var{string}, but scan at most @var{maxlen} characters.  If
+no '\0' terminator is found in that many characters, return @var{maxlen}.
+
+@end deftypefn
+
+*/
+
+#include <string.h>
+#include <stddef.h>
+
+size_t
+strnlen (const char *string, size_t maxlen)
+{
+  const char *end = (const char *) memchr (string, '\0', maxlen);
+  return end ? (size_t) (end - string) : maxlen;
+}

=== modified file 'gcc/config.in'
--- gcc/config.in       2012-05-25 09:24:08 +0000
+++ gcc/config.in       2012-08-07 01:15:45 +0000
@@ -828,6 +828,13 @@
 #endif
 
 
+/* Define to 1 if we found a declaration for 'strnlen', otherwise define to 0.
+   */
+#ifndef USED_FOR_TARGET
+#undef HAVE_DECL_STRNLEN
+#endif
+
+
 /* Define to 1 if we found a declaration for 'strsignal', otherwise define to
    0. */
 #ifndef USED_FOR_TARGET

=== modified file 'gcc/configure'
--- gcc/configure       2012-07-24 09:49:56 +0000
+++ gcc/configure       2012-08-07 01:02:28 +0000
@@ -10462,7 +10462,7 @@ CFLAGS="$CFLAGS -I${srcdir} -I${srcdir}/
 saved_CXXFLAGS="$CXXFLAGS"
 CXXFLAGS="$CXXFLAGS -I${srcdir} -I${srcdir}/../include"
 for ac_func in getenv atol asprintf sbrk abort atof getcwd getwd \
-       strsignal strstr stpcpy strverscmp \
+       strsignal strstr stpcpy strverscmp strnlen \
        errno snprintf vsnprintf vasprintf malloc realloc calloc \
        free basename getopt clock getpagesize clearerr_unlocked feof_unlocked  
 ferror_unlocked fflush_unlocked fgetc_unlocked fgets_unlocked   
fileno_unlocked fprintf_unlocked fputc_unlocked fputs_unlocked   fread_unlocked 
fwrite_unlocked getchar_unlocked getc_unlocked   putchar_unlocked putc_unlocked
 do
@@ -17973,7 +17973,7 @@ else
   lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
   lt_status=$lt_dlunknown
   cat > conftest.$ac_ext <<_LT_EOF
-#line 17975 "configure"
+#line 17976 "configure"
 #include "confdefs.h"
 
 #if HAVE_DLFCN_H
@@ -18079,7 +18079,7 @@ else
   lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
   lt_status=$lt_dlunknown
   cat > conftest.$ac_ext <<_LT_EOF
-#line 18081 "configure"
+#line 18082 "configure"
 #include "confdefs.h"
 
 #if HAVE_DLFCN_H

=== modified file 'gcc/configure.ac'
--- gcc/configure.ac    2012-07-24 09:49:56 +0000
+++ gcc/configure.ac    2012-08-07 01:38:59 +0000
@@ -1087,7 +1087,7 @@ CFLAGS="$CFLAGS -I${srcdir} -I${srcdir}/
 saved_CXXFLAGS="$CXXFLAGS"
 CXXFLAGS="$CXXFLAGS -I${srcdir} -I${srcdir}/../include"
 gcc_AC_CHECK_DECLS(getenv atol asprintf sbrk abort atof getcwd getwd \
-       strsignal strstr stpcpy strverscmp \
+       strsignal strstr stpcpy strverscmp strnlen \
        errno snprintf vsnprintf vasprintf malloc realloc calloc \
        free basename getopt clock getpagesize gcc_UNLOCKED_FUNCS, , ,[
 #include "ansidecl.h"

=== modified file 'gcc/system.h'
--- gcc/system.h        2012-07-24 22:25:18 +0000
+++ gcc/system.h        2012-08-07 01:54:49 +0000
@@ -451,6 +451,10 @@ extern char *strstr (const char *, const
 extern char *stpcpy (char *, const char *);
 #endif
 
+#if defined (HAVE_DECL_STRNLEN) && !HAVE_DECL_STRNLEN
+extern size_t strnlen(const char *, size_t);
+#endif
+
 #ifdef __cplusplus
 }
 #endif

Reply via email to