(root)/
man-db-2.12.0/
lib/
appendstr.c
       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 docs/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 <string.h>
      24  #include <stdarg.h>
      25  
      26  #include "xalloc.h"
      27  
      28  #include "manconfig.h"
      29  
      30  #include "appendstr.h"
      31  
      32  /* append strings to first argument, which is realloced to the correct size
      33     first arg may be NULL */
      34  char *appendstr (char *str, ...)
      35  {
      36        va_list ap;
      37        size_t len, newlen;
      38        char *next, *end;
      39  
      40        len = str ? strlen (str) : 0;
      41  
      42        va_start (ap, str);
      43        newlen = len + 1;
      44        while ((next = va_arg (ap, char *)))
      45                newlen += strlen (next);
      46        va_end (ap);
      47  
      48        str = xrealloc (str, newlen);
      49        end = str + len;
      50  
      51        va_start (ap, str);
      52        while ((next = va_arg (ap, char *))) {
      53                strcpy (end, next);
      54                end += strlen (next);
      55        }
      56        va_end (ap);
      57  
      58        return str;
      59  }