Found this change in an old CVS workspace: rewrite savestring() to the
authorPaul Smith <psmith@gnu.org>
Sun, 24 May 2009 18:31:18 +0000 (18:31 +0000)
committerPaul Smith <psmith@gnu.org>
Sun, 24 May 2009 18:31:18 +0000 (18:31 +0000)
more standard xstrndup().

ChangeLog
commands.c
configure.in
function.c
make.h
misc.c
read.c
variable.c

index cbdb12c..d55c716 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
        * function.c (func_shell): Don't close pipedes[1] if it is -1.
        Fixes Savannah bug #20495.
 
+2008-10-26  Paul Smith  <psmith@gnu.org>
+
+       * configure.in: Check for strndup().
+       * misc.c (xstrndup): Rename savestring to xstrndup.  Use strndup
+       if it's available.
+       * make.h: Rename savestring to xstrndup.
+       * commands.c (chop_commands): Ditto.
+       * function.c (func_foreach): Ditto.
+       * read.c (eval, record_files): Ditto.
+       * variable.c (define_variable_in_set): Ditto.
+
 2008-09-30  Eli Zaretskii  <eliz@gnu.org>
 
        * build_w32.bat (GCCBuild): Use "-gdwarf-2 -g3" instead of
index 93d2e39..ee6fc6e 100644 (file)
@@ -313,7 +313,7 @@ chop_commands (struct commands *cmds)
           nlines += 2;
           lines = xrealloc (lines, nlines * sizeof (char *));
         }
-      lines[idx++] = savestring (p, end - p);
+      lines[idx++] = xstrndup (p, end - p);
       p = end;
       if (*p != '\0')
         ++p;
index 1fca38f..5e4de08 100644 (file)
@@ -147,7 +147,7 @@ if test "$ac_cv_func_gettimeofday" = yes; then
            [Define to 1 if you have a standard gettimeofday function])
 fi
 
-AC_CHECK_FUNCS( strdup mkstemp mktemp fdopen \
+AC_CHECK_FUNCS( strdup strndup mkstemp mktemp fdopen \
                bsd_signal dup2 getcwd realpath sigsetmask sigaction \
                 getgroups seteuid setegid setlinebuf setreuid setregid \
                 getrlimit setrlimit setvbuf pipe strerror strsignal \
index 10d3d6b..34d357a 100644 (file)
@@ -853,7 +853,7 @@ func_foreach (char *o, char **argv, const char *funcname UNUSED)
       char *result = 0;
 
       free (var->value);
-      var->value = savestring (p, len);
+      var->value = xstrndup (p, len);
 
       result = allocated_variable_expand (body);
 
diff --git a/make.h b/make.h
index c70d692..7836543 100644 (file)
--- a/make.h
+++ b/make.h
@@ -376,11 +376,11 @@ void die (int) __attribute__ ((noreturn));
 void log_working_directory (int);
 void pfatal_with_name (const char *) __attribute__ ((noreturn));
 void perror_with_name (const char *, const char *);
-char *savestring (const char *, unsigned int);
 char *concat (const char *, const char *, const char *);
 void *xmalloc (unsigned int);
 void *xrealloc (void *, unsigned int);
 char *xstrdup (const char *);
+char *xstrndup (const char *, unsigned int);
 char *find_next_token (const char **, unsigned int *);
 char *next_token (const char *);
 char *end_of_token (const char *);
diff --git a/misc.c b/misc.c
index fd5e558..6bd8208 100644 (file)
--- a/misc.c
+++ b/misc.c
@@ -388,13 +388,21 @@ xstrdup (const char *ptr)
 #endif  /* HAVE_DMALLOC_H */
 
 char *
-savestring (const char *str, unsigned int length)
+xstrndup (const char *str, unsigned int length)
 {
-  char *out = xmalloc (length + 1);
-  if (length > 0)
-    memcpy (out, str, length);
-  out[length] = '\0';
-  return out;
+  char *result;
+
+#ifdef HAVE_STRNDUP
+  result = strndup (str, length);
+  if (result == 0)
+    fatal (NILF, _("virtual memory exhausted"));
+#else
+  result = xmalloc (length + 1);
+  strncpy (result, str, length);
+  result[length] = '\0';
+#endif
+
+  return result;
 }
 \f
 
diff --git a/read.c b/read.c
index 6985d22..eb4b212 100644 (file)
--- a/read.c
+++ b/read.c
@@ -776,7 +776,7 @@ eval (struct ebuffer *ebuf, int set_default)
          p = find_next_token (&cp, &l);
          if (p != 0)
            {
-             vpat = savestring (p, l);
+             vpat = xstrndup (p, l);
              p = find_next_token (&cp, &l);
              /* No searchpath means remove all previous
                 selective VPATH's with the same pattern.  */
@@ -1891,7 +1891,7 @@ record_files (struct nameseq *filenames, const char *pattern,
       cmds = xmalloc (sizeof (struct commands));
       cmds->fileinfo.filenm = flocp->filenm;
       cmds->fileinfo.lineno = cmds_started;
-      cmds->commands = savestring (commands, commands_idx);
+      cmds->commands = xstrndup (commands, commands_idx);
       cmds->command_lines = 0;
     }
   else
@@ -2399,7 +2399,7 @@ parse_file_seq (char **stringp, int stopchar, unsigned int size, int strip)
 #ifdef VMS
 /* VMS filenames can have a ':' in them but they have to be '\'ed but we need
  *  to remove this '\' before we can use the filename.
- * Savestring called because q may be read-only string constant.
+ * xstrdup called because q may be read-only string constant.
  */
        {
          char *qbase = xstrdup (q);
index 3dc8a22..4dafab1 100644 (file)
@@ -206,7 +206,7 @@ define_variable_in_set (const char *name, unsigned int length,
   /* Create a new variable definition and add it to the hash table.  */
 
   v = xmalloc (sizeof (struct variable));
-  v->name = savestring (name, length);
+  v->name = xstrndup (name, length);
   v->length = length;
   hash_insert_at (&set->table, v, var_slot);
   v->value = xstrdup (value);