]> git.ipfire.org Git - thirdparty/gcc.git/blame - libiberty/concat.c
dostage2, [...]: Remove obsolete files.
[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
b6a2f884
AJ
56#ifdef HAVE_CONFIG_H
57#include "config.h"
58#endif
6599da04
JM
59#include "ansidecl.h"
60#include "libiberty.h"
c8b28221 61#include <sys/types.h> /* size_t */
6599da04
JM
62
63#ifdef ANSI_PROTOTYPES
64#include <stdarg.h>
65#else
66#include <varargs.h>
67#endif
68
0bdcca68
RH
69# if HAVE_STRING_H
70# include <string.h>
71# else
72# if HAVE_STRINGS_H
73# include <strings.h>
74# endif
75# endif
6599da04 76
6599da04 77char *
e2dff3f2 78concat VPARAMS ((const char *first, ...))
6599da04 79{
0bdcca68 80 register size_t length;
6599da04
JM
81 register char *newstr;
82 register char *end;
83 register const char *arg;
6599da04 84
0bdcca68 85 /* First compute the size of the result and get sufficient memory. */
e2dff3f2
KG
86 VA_OPEN (args, first);
87 VA_FIXEDARG (args, const char *, first);
88
0bdcca68
RH
89 length = 0;
90 for (arg = first; arg ; arg = va_arg (args, const char *))
91 length += strlen (arg);
92
e2dff3f2 93 VA_CLOSE (args);
6599da04 94
0bdcca68 95 newstr = (char *) xmalloc (length + 1);
6599da04 96
0bdcca68 97 /* Now copy the individual pieces to the result string. */
e2dff3f2
KG
98 VA_OPEN (args, first);
99 VA_FIXEDARG (args, const char *, first);
0bdcca68
RH
100
101 end = newstr;
102 for (arg = first; arg ; arg = va_arg (args, const char *))
103 {
104 length = strlen (arg);
105 memcpy (end, arg, length);
106 end += length;
6599da04 107 }
0bdcca68 108 *end = '\000';
e2dff3f2 109 VA_CLOSE (args);
6599da04 110
0bdcca68 111 return newstr;
6599da04
JM
112}
113
114#ifdef MAIN
0bdcca68 115#define NULLP (char *)0
6599da04
JM
116
117/* Simple little test driver. */
118
119#include <stdio.h>
120
121int
122main ()
123{
124 printf ("\"\" = \"%s\"\n", concat (NULLP));
125 printf ("\"a\" = \"%s\"\n", concat ("a", NULLP));
126 printf ("\"ab\" = \"%s\"\n", concat ("a", "b", NULLP));
127 printf ("\"abc\" = \"%s\"\n", concat ("a", "b", "c", NULLP));
128 printf ("\"abcd\" = \"%s\"\n", concat ("ab", "cd", NULLP));
129 printf ("\"abcde\" = \"%s\"\n", concat ("ab", "c", "de", NULLP));
130 printf ("\"abcdef\" = \"%s\"\n", concat ("", "a", "", "bcd", "ef", NULLP));
131 return 0;
132}
133
134#endif