File: strappend.c

package info (click to toggle)
c2man 2.41-11
  • links: PTS
  • area: main
  • in suites: potato
  • size: 800 kB
  • ctags: 863
  • sloc: ansic: 6,559; sh: 5,236; yacc: 839; lex: 621; makefile: 229; perl: 81
file content (64 lines) | stat: -rw-r--r-- 1,340 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/* $Id: strappend.c,v 2.0.1.2 1994/01/07 07:05:39 greyham Exp $
 */
#include "c2man.h"
#include "strappend.h"

#ifdef I_STDARG
#include <stdarg.h>
#endif
#ifdef I_VARARGS
#include <varargs.h>
#endif

extern void outmem();

/*
 * append a list of strings to another, storing them in a malloc'ed region.
 * The first string may be NULL, in which case the rest are simply concatenated.
 */
#ifdef I_STDARG
char *strappend(char *first, ...)
#else
char *strappend(va_alist)
    va_dcl
#endif
{
    size_t totallen;
    va_list argp;
    char *s, *retstring;
#ifndef I_STDARG
    char *first;
#endif    
    /* add up the total length */
#ifdef I_STDARG
    va_start(argp,first);
#else
    va_start(argp);
    first = va_arg(argp, char *);
#endif
    totallen = first ? strlen(first) : 0;
    while ((s = va_arg(argp,char *)) != NULL)
	totallen += strlen(s);
    va_end(argp);
    
    /* malloc the memory */
    totallen++;	/* add space for the nul terminator */
    if ((retstring = first ? realloc(first,totallen) : malloc(totallen)) == 0)
	outmem();

    if (first == NULL)	*retstring = '\0';

#ifdef I_STDARG
    va_start(argp,first);
#else
    va_start(argp);
    first = va_arg(argp, char *);	/* skip the first arg */
#endif

    while ((s = va_arg(argp,char *)) != NULL)
	strcat(retstring,s);

    va_end(argp);

    return retstring;
}