From 9659201a5ff853ca1549f6859050c4ea3074d83b Mon Sep 17 00:00:00 2001 From: ghazi Date: Mon, 17 Sep 2001 22:15:10 +0000 Subject: [PATCH] include: * libiberty.h (concat_length, concat_copy, concat_copy2, libiberty_concat_ptr, ACONCAT): New. libiberty: * concat.c (vconcat_length, vconcat_copy, concat_length, concat_copy, concat_copy2): New functions. (concat): Use vconcat_length/vconcat_copy. gcc: * gcc.c (find_file): Use ACONCAT in lieu of alloca/strcpy/strcat. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@45664 138bc75d-0d04-0410-961f-82ee72b054a4 --- gcc/ChangeLog | 4 +++ gcc/gcc.c | 7 +--- include/ChangeLog | 3 ++ include/libiberty.h | 27 +++++++++++++++ libiberty/ChangeLog | 4 +++ libiberty/concat.c | 98 ++++++++++++++++++++++++++++++++++++++++++----------- 6 files changed, 118 insertions(+), 25 deletions(-) diff --git a/gcc/ChangeLog b/gcc/ChangeLog index c66d0d93e35..1e8d4f04ce8 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,7 @@ +2001-09-17 Kaveh R. Ghazi + + * gcc.c (find_file): Use ACONCAT in lieu of alloca/strcpy/strcat. + 2001-09-17 Joseph S. Myers * dostage2, dostage3, listing, make-l2.com, makefile.vms, diff --git a/gcc/gcc.c b/gcc/gcc.c index 55188cf6359..ad692eb5b32 100644 --- a/gcc/gcc.c +++ b/gcc/gcc.c @@ -5419,12 +5419,7 @@ find_file (name) /* Try multilib_dir if it is defined. */ if (multilib_dir != NULL) { - char *try; - - try = (char *) alloca (strlen (multilib_dir) + strlen (name) + 2); - strcpy (try, multilib_dir); - strcat (try, dir_separator_str); - strcat (try, name); + const char *const try = ACONCAT ((multilib_dir, dir_separator_str, name, NULL)); newname = find_a_file (&startfile_prefixes, try, R_OK); diff --git a/include/ChangeLog b/include/ChangeLog index d745a289647..a20b8b82e5c 100644 --- a/include/ChangeLog +++ b/include/ChangeLog @@ -1,5 +1,8 @@ 2001-09-17 Kaveh R. Ghazi + * libiberty.h (concat_length, concat_copy, concat_copy2, + libiberty_concat_ptr, ACONCAT): New. + * libiberty.h (ASTRDUP): New macro. libiberty_optr, libiberty_nptr, libiberty_len): Declare. diff --git a/include/libiberty.h b/include/libiberty.h index 7220ec0643c..43f1b7d401e 100644 --- a/include/libiberty.h +++ b/include/libiberty.h @@ -90,6 +90,33 @@ extern const char *lbasename PARAMS ((const char *)); extern char *concat PARAMS ((const char *, ...)) ATTRIBUTE_MALLOC; +/* Determine the length of concatenating an arbitrary number of + strings, up to (char *) NULL. */ + +extern unsigned long concat_length PARAMS ((const char *, ...)); + +/* Concatenate an arbitrary number of strings into a SUPPLIED area of + memory, up to (char *) NULL. The supplied memory is assumed to be + large enough. */ + +extern char *concat_copy PARAMS ((char *, const char *, ...)); + +/* Concatenate an arbitrary number of strings into a GLOBAL area of + memory, up to (char *) NULL. The supplied memory is assumed to be + large enough. */ + +extern char *concat_copy2 PARAMS ((const char *, ...)); + +/* This is the global area used by concat_copy2. */ + +extern char *libiberty_concat_ptr; + +/* Concatenate an arbitrary number of strings, up to (char *) NULL. + Allocates memory using alloca. Arguments are evaluated twice!. */ +#define ACONCAT(ACONCAT_PARAMS) \ + (libiberty_concat_ptr = alloca (concat_length ACONCAT_PARAMS + 1), \ + concat_copy2 ACONCAT_PARAMS) + /* Check whether two file descriptors refer to the same file. */ extern int fdmatch PARAMS ((int fd1, int fd2)); diff --git a/libiberty/ChangeLog b/libiberty/ChangeLog index 970dc98ad58..b68c4c5b812 100644 --- a/libiberty/ChangeLog +++ b/libiberty/ChangeLog @@ -1,5 +1,9 @@ 2001-09-17 Kaveh R. Ghazi + * concat.c (vconcat_length, vconcat_copy, concat_length, + concat_copy, concat_copy2): New functions. + (concat): Use vconcat_length/vconcat_copy. + * alloca.c (libiberty_optr, libiberty_nptr, libiberty_len): Define. diff --git a/libiberty/concat.c b/libiberty/concat.c index 2e31e833f4d..feed0df819b 100644 --- a/libiberty/concat.c +++ b/libiberty/concat.c @@ -74,38 +74,98 @@ NOTES # endif # endif -char * -concat VPARAMS ((const char *first, ...)) +static inline unsigned long vconcat_length PARAMS ((const char *, va_list)); +static inline unsigned long +vconcat_length (first, args) + const char *first; + va_list args; { - register size_t length; - register char *newstr; - register char *end; - register const char *arg; + unsigned long length = 0; + const char *arg; - /* First compute the size of the result and get sufficient memory. */ - VA_OPEN (args, first); - VA_FIXEDARG (args, const char *, first); - - length = 0; for (arg = first; arg ; arg = va_arg (args, const char *)) length += strlen (arg); - VA_CLOSE (args); - - newstr = (char *) xmalloc (length + 1); + return length; +} - /* Now copy the individual pieces to the result string. */ - VA_OPEN (args, first); - VA_FIXEDARG (args, const char *, first); +static inline char *vconcat_copy PARAMS ((char *, const char *, va_list)); +static inline char * +vconcat_copy (dst, first, args) + char *dst; + const char *first; + va_list args; +{ + char *end = dst; + const char *arg; - end = newstr; for (arg = first; arg ; arg = va_arg (args, const char *)) { - length = strlen (arg); + unsigned long length = strlen (arg); memcpy (end, arg, length); end += length; } *end = '\000'; + + return dst; +} + +unsigned long +concat_length VPARAMS ((const char *first, ...)) +{ + unsigned long length; + + VA_OPEN (args, first); + VA_FIXEDARG (args, const char *, first); + length = vconcat_length (first, args); + VA_CLOSE (args); + + return length; +} + +char * +concat_copy VPARAMS ((char *dst, const char *first, ...)) +{ + char *save_dst; + + VA_OPEN (args, first); + VA_FIXEDARG (args, char *, dst); + VA_FIXEDARG (args, const char *, first); + vconcat_copy (dst, first, args); + save_dst = dst; /* With K&R C, dst goes out of scope here. */ + VA_CLOSE (args); + + return save_dst; +} + +char *libiberty_concat_ptr; + +char * +concat_copy2 VPARAMS ((const char *first, ...)) +{ + VA_OPEN (args, first); + VA_FIXEDARG (args, const char *, first); + vconcat_copy (libiberty_concat_ptr, first, args); + VA_CLOSE (args); + + return libiberty_concat_ptr; +} + +char * +concat VPARAMS ((const char *first, ...)) +{ + char *newstr; + + /* First compute the size of the result and get sufficient memory. */ + VA_OPEN (args, first); + VA_FIXEDARG (args, const char *, first); + newstr = (char *) xmalloc (vconcat_length (first, args) + 1); + VA_CLOSE (args); + + /* Now copy the individual pieces to the result string. */ + VA_OPEN (args, first); + VA_FIXEDARG (args, const char *, first); + vconcat_copy (newstr, first, args); VA_CLOSE (args); return newstr; -- 2.11.4.GIT