| 1 |
/* appendstr.c -- append to a dynamically allocated string |
| 2 |
Copyright (C) 1994 Markus Armbruster |
| 3 |
|
| 4 |
This program is free software; you can redistribute it and/or |
| 5 |
modify it under the terms of the GNU General Library Public License |
| 6 |
as published by the Free Software Foundation; either version 2, or |
| 7 |
(at your option) any later version. |
| 8 |
|
| 9 |
This program is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
GNU General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU General Public License |
| 15 |
along with this program; see the file COPYING.LIB. If not, write |
| 16 |
to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA |
| 17 |
02139, USA. */ |
| 18 |
|
| 19 |
#ifdef HAVE_CONFIG_H |
| 20 |
# include "config.h" |
| 21 |
#endif /* HAVE_CONFIG_H */ |
| 22 |
|
| 23 |
#include <stdarg.h> |
| 24 |
#include <string.h> |
| 25 |
|
| 26 |
#include "xalloc.h" |
| 27 |
|
| 28 |
#include "pipeline-private.h" |
| 29 |
|
| 30 |
/* append strings to first argument, which is realloced to the correct size |
| 31 |
first arg may be NULL */ |
| 32 |
char *appendstr (char *str, ...) |
| 33 |
{ |
| 34 |
va_list ap; |
| 35 |
int len, newlen; |
| 36 |
char *next, *end; |
| 37 |
|
| 38 |
len = str ? strlen (str) : 0; |
| 39 |
|
| 40 |
va_start (ap, str); |
| 41 |
newlen = len + 1; |
| 42 |
while ((next = va_arg (ap, char *))) |
| 43 |
newlen += strlen (next); |
| 44 |
va_end (ap); |
| 45 |
|
| 46 |
str = xrealloc (str, newlen); |
| 47 |
end = str + len; |
| 48 |
|
| 49 |
va_start (ap, str); |
| 50 |
while ((next = va_arg (ap, char *))) { |
| 51 |
strcpy (end, next); |
| 52 |
end += strlen (next); |
| 53 |
} |
| 54 |
va_end (ap); |
| 55 |
|
| 56 |
return str; |
| 57 |
} |