Found this change in an old CVS workspace: rewrite savestring() to the

more standard xstrndup().
This commit is contained in:
Paul Smith 2009-05-24 18:31:18 +00:00
parent 3fd62c76c2
commit 14f3f501bc
8 changed files with 33 additions and 14 deletions

View file

@ -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 <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

View 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;

View 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 \

View 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);

2
make.h
View file

@ -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 *);

20
misc.c
View file

@ -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;
}

6
read.c
View file

@ -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);

View 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);