* archive.c (offsetof): Remove define.
[binutils.git] / libiberty / concat.c
blob01270eadcf2d74643c94c4a63ccbfed0935c4496
1 /* Concatenate variable number of strings.
2 Copyright (C) 1991, 1994, 2001 Free Software Foundation, Inc.
3 Written by Fred Fish @ Cygnus Support
5 This file is part of the libiberty library.
6 Libiberty is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
11 Libiberty is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with libiberty; see the file COPYING.LIB. If
18 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
24 NAME
26 concat -- concatenate a variable number of strings
28 SYNOPSIS
30 #include <varargs.h>
32 char *concat (s1, s2, s3, ..., NULL)
34 DESCRIPTION
36 Concatenate a variable number of strings and return the result
37 in freshly malloc'd memory.
39 Returns NULL if insufficient memory is available. The argument
40 list is terminated by the first NULL pointer encountered. Pointers
41 to empty strings are ignored.
43 NOTES
45 This function uses xmalloc() which is expected to be a front end
46 function to malloc() that deals with low memory situations. In
47 typical use, if malloc() returns NULL then xmalloc() diverts to an
48 error handler routine which never returns, and thus xmalloc will
49 never return a NULL pointer. If the client application wishes to
50 deal with low memory situations itself, it should supply an xmalloc
51 that just directly invokes malloc and blindly returns whatever
52 malloc returns.
56 #ifdef HAVE_CONFIG_H
57 #include "config.h"
58 #endif
59 #include "ansidecl.h"
60 #include "libiberty.h"
61 #include <sys/types.h> /* size_t */
63 #ifdef ANSI_PROTOTYPES
64 #include <stdarg.h>
65 #else
66 #include <varargs.h>
67 #endif
69 # if HAVE_STRING_H
70 # include <string.h>
71 # else
72 # if HAVE_STRINGS_H
73 # include <strings.h>
74 # endif
75 # endif
77 /* VARARGS */
78 #ifdef ANSI_PROTOTYPES
79 char *
80 concat (const char *first, ...)
81 #else
82 char *
83 concat (va_alist)
84 va_dcl
85 #endif
87 register size_t length;
88 register char *newstr;
89 register char *end;
90 register const char *arg;
91 va_list args;
92 #ifndef ANSI_PROTOTYPES
93 const char *first;
94 #endif
96 /* First compute the size of the result and get sufficient memory. */
97 #ifdef ANSI_PROTOTYPES
98 va_start (args, first);
99 #else
100 va_start (args);
101 first = va_arg (args, const char *);
102 #endif
104 length = 0;
105 for (arg = first; arg ; arg = va_arg (args, const char *))
106 length += strlen (arg);
108 va_end (args);
110 newstr = (char *) xmalloc (length + 1);
112 /* Now copy the individual pieces to the result string. */
113 #ifdef ANSI_PROTOTYPES
114 va_start (args, first);
115 #else
116 va_start (args);
117 first = va_arg (args, const char *);
118 #endif
120 end = newstr;
121 for (arg = first; arg ; arg = va_arg (args, const char *))
123 length = strlen (arg);
124 memcpy (end, arg, length);
125 end += length;
127 *end = '\000';
128 va_end (args);
130 return newstr;
133 #ifdef MAIN
134 #define NULLP (char *)0
136 /* Simple little test driver. */
138 #include <stdio.h>
141 main ()
143 printf ("\"\" = \"%s\"\n", concat (NULLP));
144 printf ("\"a\" = \"%s\"\n", concat ("a", NULLP));
145 printf ("\"ab\" = \"%s\"\n", concat ("a", "b", NULLP));
146 printf ("\"abc\" = \"%s\"\n", concat ("a", "b", "c", NULLP));
147 printf ("\"abcd\" = \"%s\"\n", concat ("ab", "cd", NULLP));
148 printf ("\"abcde\" = \"%s\"\n", concat ("ab", "c", "de", NULLP));
149 printf ("\"abcdef\" = \"%s\"\n", concat ("", "a", "", "bcd", "ef", NULLP));
150 return 0;
153 #endif