Ignore:
Timestamp:
Aug 16, 2003, 6:59:22 PM (22 years ago)
Author:
bird
Message:

binutils v2.14 - offical sources.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/GNU/src/binutils/libiberty/concat.c

    • Property cvs2svn:cvs-rev changed from 1.1 to 1.1.1.2
    r608 r609  
    11/* Concatenate variable number of strings.
    2    Copyright (C) 1991, 1994 Free Software Foundation, Inc.
     2   Copyright (C) 1991, 1994, 2001 Free Software Foundation, Inc.
    33   Written by Fred Fish @ Cygnus Support
    44
     
    2222/*
    2323
    24 NAME
    25 
    26         concat -- concatenate a variable number of strings
    27 
    28 SYNOPSIS
    29 
    30         #include <varargs.h>
    31 
    32         char *concat (s1, s2, s3, ..., NULL)
    33 
    34 DESCRIPTION
    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.
     24@deftypefn Extension char* concat (const char *@var{s1}, const char *@var{s2}, @dots{}, @code{NULL})
     25
     26Concatenate zero or more of strings and return the result in freshly
     27@code{xmalloc}ed memory.  Returns @code{NULL} if insufficient memory is
     28available.  The argument list is terminated by the first @code{NULL}
     29pointer encountered.  Pointers to empty strings are ignored.
     30
     31@end deftypefn
    4232
    4333NOTES
     
    5141        that just directly invokes malloc and blindly returns whatever
    5242        malloc returns.
     43
    5344*/
    5445
    5546
     47#ifdef HAVE_CONFIG_H
     48#include "config.h"
     49#endif
    5650#include "ansidecl.h"
    5751#include "libiberty.h"
     52#include <sys/types.h>          /* size_t */
    5853
    5954#ifdef ANSI_PROTOTYPES
     
    6358#endif
    6459
    65 #ifdef __STDC__
    66 #include <stddef.h>
    67 extern size_t strlen (const char *s);
    68 #else
    69 extern int strlen ();
    70 #endif
    71 
     60# if HAVE_STRING_H
     61#  include <string.h>
     62# else
     63#  if HAVE_STRINGS_H
     64#   include <strings.h>
     65#  endif
     66# endif
     67
     68#if HAVE_STDLIB_H
     69#include <stdlib.h>
     70#endif
     71
     72static inline unsigned long vconcat_length PARAMS ((const char *, va_list));
     73static inline unsigned long
     74vconcat_length (first, args)
     75     const char *first;
     76     va_list args;
     77{
     78  unsigned long length = 0;
     79  const char *arg;
     80
     81  for (arg = first; arg ; arg = va_arg (args, const char *))
     82    length += strlen (arg);
     83
     84  return length;
     85}
     86
     87static inline char *vconcat_copy PARAMS ((char *, const char *, va_list));
     88static inline char *
     89vconcat_copy (dst, first, args)
     90     char *dst;
     91     const char *first;
     92     va_list args;
     93{
     94  char *end = dst;
     95  const char *arg;
     96
     97  for (arg = first; arg ; arg = va_arg (args, const char *))
     98    {
     99      unsigned long length = strlen (arg);
     100      memcpy (end, arg, length);
     101      end += length;
     102    }
     103  *end = '\000';
     104
     105  return dst;
     106}
     107
     108/* @undocumented concat_length */
     109
     110unsigned long
     111concat_length VPARAMS ((const char *first, ...))
     112{
     113  unsigned long length;
     114
     115  VA_OPEN (args, first);
     116  VA_FIXEDARG (args, const char *, first);
     117  length = vconcat_length (first, args);
     118  VA_CLOSE (args);
     119
     120  return length;
     121}
     122
     123/* @undocumented concat_copy */
     124
     125char *
     126concat_copy VPARAMS ((char *dst, const char *first, ...))
     127{
     128  char *save_dst;
     129
     130  VA_OPEN (args, first);
     131  VA_FIXEDARG (args, char *, dst);
     132  VA_FIXEDARG (args, const char *, first);
     133  vconcat_copy (dst, first, args);
     134  save_dst = dst; /* With K&R C, dst goes out of scope here.  */
     135  VA_CLOSE (args);
     136
     137  return save_dst;
     138}
     139
     140char *libiberty_concat_ptr;
     141
     142/* @undocumented concat_copy2 */
     143
     144char *
     145concat_copy2 VPARAMS ((const char *first, ...))
     146{
     147  VA_OPEN (args, first);
     148  VA_FIXEDARG (args, const char *, first);
     149  vconcat_copy (libiberty_concat_ptr, first, args);
     150  VA_CLOSE (args);
     151
     152  return libiberty_concat_ptr;
     153}
     154
     155char *
     156concat VPARAMS ((const char *first, ...))
     157{
     158  char *newstr;
     159
     160  /* First compute the size of the result and get sufficient memory.  */
     161  VA_OPEN (args, first);
     162  VA_FIXEDARG (args, const char *, first);
     163  newstr = (char *) xmalloc (vconcat_length (first, args) + 1);
     164  VA_CLOSE (args);
     165
     166  /* Now copy the individual pieces to the result string. */
     167  VA_OPEN (args, first);
     168  VA_FIXEDARG (args, const char *, first);
     169  vconcat_copy (newstr, first, args);
     170  VA_CLOSE (args);
     171
     172  return newstr;
     173}
     174
     175/*
     176
     177@deftypefn Extension char* reconcat (char *@var{optr}, const char *@var{s1}, @dots{}, @code{NULL})
     178
     179Same as @code{concat}, except that if @var{optr} is not @code{NULL} it
     180is freed after the string is created.  This is intended to be useful
     181when you're extending an existing string or building up a string in a
     182loop:
     183
     184@example
     185  str = reconcat (str, "pre-", str, NULL);
     186@end example
     187
     188@end deftypefn
     189
     190*/
     191
     192char *
     193reconcat VPARAMS ((char *optr, const char *first, ...))
     194{
     195  char *newstr;
     196
     197  /* First compute the size of the result and get sufficient memory.  */
     198  VA_OPEN (args, first);
     199  VA_FIXEDARG (args, char *, optr);
     200  VA_FIXEDARG (args, const char *, first);
     201  newstr = (char *) xmalloc (vconcat_length (first, args) + 1);
     202  VA_CLOSE (args);
     203
     204  /* Now copy the individual pieces to the result string. */
     205  VA_OPEN (args, first);
     206  VA_FIXEDARG (args, char *, optr);
     207  VA_FIXEDARG (args, const char *, first);
     208  vconcat_copy (newstr, first, args);
     209  if (optr) /* Done before VA_CLOSE so optr stays in scope for K&R C.  */
     210    free (optr);
     211  VA_CLOSE (args);
     212
     213  return newstr;
     214}
     215
     216#ifdef MAIN
    72217#define NULLP (char *)0
    73 
    74 /* VARARGS */
    75 #ifdef ANSI_PROTOTYPES
    76 char *
    77 concat (const char *first, ...)
    78 #else
    79 char *
    80 concat (va_alist)
    81      va_dcl
    82 #endif
    83 {
    84   register int length;
    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 
    93   /* First compute the size of the result and get sufficient memory. */
    94 
    95 #ifdef ANSI_PROTOTYPES
    96   va_start (args, first);
    97 #else
    98   va_start (args);
    99   first = va_arg (args, const char *);
    100 #endif
    101 
    102   if (first == NULLP)
    103     length = 0;
    104   else
    105     {
    106       length = strlen (first);
    107       while ((arg = va_arg (args, const char *)) != NULLP)
    108         {
    109           length += strlen (arg);
    110         }
    111     }
    112   newstr = (char *) xmalloc (length + 1);
    113   va_end (args);
    114 
    115   /* Now copy the individual pieces to the result string. */
    116 
    117   if (newstr != NULLP)
    118     {
    119 #ifdef ANSI_PROTOTYPES
    120       va_start (args, first);
    121 #else
    122       va_start (args);
    123       first = va_arg (args, const char *);
    124 #endif
    125       end = newstr;
    126       if (first != NULLP)
    127         {
    128           arg = first;
    129           while (*arg)
    130             {
    131               *end++ = *arg++;
    132             }
    133           while ((arg = va_arg (args, const char *)) != NULLP)
    134             {
    135               while (*arg)
    136                 {
    137                   *end++ = *arg++;
    138                 }
    139             }
    140         }
    141       *end = '\000';
    142       va_end (args);
    143     }
    144 
    145   return (newstr);
    146 }
    147 
    148 #ifdef MAIN
    149218
    150219/* Simple little test driver. */
Note: See TracChangeset for help on using the changeset viewer.