From 14f3f501bc1abc821d859e964f71e69b8ed7eaa2 Mon Sep 17 00:00:00 2001 From: Paul Smith Date: Sun, 24 May 2009 18:31:18 +0000 Subject: [PATCH] Found this change in an old CVS workspace: rewrite savestring() to the more standard xstrndup(). --- ChangeLog | 11 +++++++++++ commands.c | 2 +- configure.in | 2 +- function.c | 2 +- make.h | 2 +- misc.c | 20 ++++++++++++++------ read.c | 6 +++--- variable.c | 2 +- 8 files changed, 33 insertions(+), 14 deletions(-) diff --git a/ChangeLog b/ChangeLog index cbdb12c..d55c716 100644 --- a/ChangeLog +++ b/ChangeLog @@ -18,6 +18,17 @@ * function.c (func_shell): Don't close pipedes[1] if it is -1. Fixes Savannah bug #20495. +2008-10-26 Paul Smith + + * 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 * build_w32.bat (GCCBuild): Use "-gdwarf-2 -g3" instead of diff --git a/commands.c b/commands.c index 93d2e39..ee6fc6e 100644 --- a/commands.c +++ b/commands.c @@ -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; diff --git a/configure.in b/configure.in index 1fca38f..5e4de08 100644 --- a/configure.in +++ b/configure.in @@ -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 \ diff --git a/function.c b/function.c index 10d3d6b..34d357a 100644 --- a/function.c +++ b/function.c @@ -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 --- 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 --- 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; } diff --git a/read.c b/read.c index 6985d22..eb4b212 100644 --- 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); diff --git a/variable.c b/variable.c index 3dc8a22..4dafab1 100644 --- a/variable.c +++ b/variable.c @@ -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); -- 2.7.4