]> git.ipfire.org Git - thirdparty/gcc.git/blame - libiberty/concat.c
linux.h (CPLUSPLUS_CPP_SPEC): Add -D_GNU_SOURCE.
[thirdparty/gcc.git] / libiberty / concat.c
CommitLineData
6599da04 1/* Concatenate variable number of strings.
0bdcca68 2 Copyright (C) 1991, 1994, 2001 Free Software Foundation, Inc.
6599da04
JM
3 Written by Fred Fish @ Cygnus Support
4
5This file is part of the libiberty library.
6Libiberty is free software; you can redistribute it and/or
7modify it under the terms of the GNU Library General Public
8License as published by the Free Software Foundation; either
9version 2 of the License, or (at your option) any later version.
10
11Libiberty is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14Library General Public License for more details.
15
16You should have received a copy of the GNU Library General Public
17License along with libiberty; see the file COPYING.LIB. If
18not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19Boston, MA 02111-1307, USA. */
20
21
22/*
23
24NAME
25
26 concat -- concatenate a variable number of strings
27
28SYNOPSIS
29
30 #include <varargs.h>
31
32 char *concat (s1, s2, s3, ..., NULL)
33
34DESCRIPTION
35
36 Concatenate a variable number of strings and return the result
37 in freshly malloc'd memory.
38
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.
42
43NOTES
44
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.
53*/
54
55
56#include "ansidecl.h"
57#include "libiberty.h"
c8b28221 58#include <sys/types.h> /* size_t */
6599da04
JM
59
60#ifdef ANSI_PROTOTYPES
61#include <stdarg.h>
62#else
63#include <varargs.h>
64#endif
65
0bdcca68
RH
66# if HAVE_STRING_H
67# include <string.h>
68# else
69# if HAVE_STRINGS_H
70# include <strings.h>
71# endif
72# endif
6599da04
JM
73
74/* VARARGS */
75#ifdef ANSI_PROTOTYPES
76char *
77concat (const char *first, ...)
78#else
79char *
80concat (va_alist)
81 va_dcl
82#endif
83{
0bdcca68 84 register size_t length;
6599da04
JM
85 register char *newstr;
86 register char *end;
87 register const char *arg;
88 va_list args;
89#ifndef ANSI_PROTOTYPES
90 const char *first;
91#endif
92
0bdcca68 93 /* First compute the size of the result and get sufficient memory. */
6599da04
JM
94#ifdef ANSI_PROTOTYPES
95 va_start (args, first);
96#else
97 va_start (args);
98 first = va_arg (args, const char *);
99#endif
100
0bdcca68
RH
101 length = 0;
102 for (arg = first; arg ; arg = va_arg (args, const char *))
103 length += strlen (arg);
104
6599da04
JM
105 va_end (args);
106
0bdcca68 107 newstr = (char *) xmalloc (length + 1);
6599da04 108
0bdcca68 109 /* Now copy the individual pieces to the result string. */
6599da04 110#ifdef ANSI_PROTOTYPES
0bdcca68 111 va_start (args, first);
6599da04 112#else
0bdcca68
RH
113 va_start (args);
114 first = va_arg (args, const char *);
6599da04 115#endif
0bdcca68
RH
116
117 end = newstr;
118 for (arg = first; arg ; arg = va_arg (args, const char *))
119 {
120 length = strlen (arg);
121 memcpy (end, arg, length);
122 end += length;
6599da04 123 }
0bdcca68
RH
124 *end = '\000';
125 va_end (args);
6599da04 126
0bdcca68 127 return newstr;
6599da04
JM
128}
129
130#ifdef MAIN
0bdcca68 131#define NULLP (char *)0
6599da04
JM
132
133/* Simple little test driver. */
134
135#include <stdio.h>
136
137int
138main ()
139{
140 printf ("\"\" = \"%s\"\n", concat (NULLP));
141 printf ("\"a\" = \"%s\"\n", concat ("a", NULLP));
142 printf ("\"ab\" = \"%s\"\n", concat ("a", "b", NULLP));
143 printf ("\"abc\" = \"%s\"\n", concat ("a", "b", "c", NULLP));
144 printf ("\"abcd\" = \"%s\"\n", concat ("ab", "cd", NULLP));
145 printf ("\"abcde\" = \"%s\"\n", concat ("ab", "c", "de", NULLP));
146 printf ("\"abcdef\" = \"%s\"\n", concat ("", "a", "", "bcd", "ef", NULLP));
147 return 0;
148}
149
150#endif