1: /* $Header$ */
2:
3: /*
4: * Author: Peter J. Nicklin
5: */
6:
7: /*
8: * argvtos() copies a list of arguments contained in an array of character
9: * strings to a single dynamically allocated string. Each argument is
10: * separated by one blank space. Returns a pointer to the string or null
11: * if out of memory.
12: */
13: #include "null.h"
14:
15: #define SBUFINCR 1024
16: #define SBUFMAX 10240
17:
18: char *
19: argvtos(argc, argv)
20: char **argv;
21: int argc;
22: {
23: register char *s; /* string pointer */
24: register int i; /* string buffer pointer */
25: char *malloc(); /* memory allocator */
26: char *realloc(); /* increase size of storage */
27: char *sbuf; /* string buffer */
28: int nbytes; /* bytes of memory required */
29: int nu; /* no. of SBUFINCR units required */
30: int sbufsize; /* current size of sbuf */
31: int strlen(); /* string length */
32:
33: sbufsize = SBUFINCR;
34: if ((sbuf = malloc((unsigned)sbufsize)) == NULL)
35: {
36: warn("out of memory");
37: return(NULL);
38: }
39:
40: for (i = 0; argc-- > 0; ++argv)
41: {
42: if ((nbytes = (i+strlen(*argv)+1-sbufsize)) > 0)
43: {
44: nu = (nbytes+SBUFINCR-1)/SBUFINCR;
45: sbufsize += nu * SBUFINCR;
46: if (sbufsize > SBUFMAX)
47: {
48: warn("command string too long");
49: return(NULL);
50: }
51: if ((sbuf = realloc(sbuf, (unsigned)sbufsize)) == NULL)
52: {
53: warn("out of memory");
54: return(NULL);
55: }
56: }
57: for (s = *argv; *s != '\0'; i++, s++)
58: sbuf[i] = *s;
59: sbuf[i++] = ' ';
60: }
61: sbuf[--i] = '\0';
62: return(sbuf);
63: }
Defined functions
Defined macros