xref: /titanic_51/usr/src/lib/libast/common/vec/vecargs.c (revision c10c16dec587a0662068f6e2991c29ed3a9db943)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1985-2010 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *            http://www.opensource.org/licenses/cpl1.0.txt             *
11 *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *                 Glenn Fowler <gsf@research.att.com>                  *
18 *                  David Korn <dgk@research.att.com>                   *
19 *                   Phong Vo <kpv@research.att.com>                    *
20 *                                                                      *
21 ***********************************************************************/
22 #pragma prototyped
23 /*
24  * Glenn Fowler
25  * AT&T Bell Laboratories
26  *
27  * string vector argv insertion
28  */
29 
30 #include <ast.h>
31 #include <vecargs.h>
32 #include <ctype.h>
33 
34 /*
35  * insert the string vector vec between
36  * (*argvp)[0] and (*argvp)[1], sliding (*argvp)[1] ... over
37  * null and blank args are deleted
38  *
39  * vecfree always called
40  *
41  * -1 returned if insertion failed
42  */
43 
44 int
45 vecargs(register char** vec, int* argcp, char*** argvp)
46 {
47 	register char**	argv;
48 	register char**	oargv;
49 	char**		ovec;
50 	char*		s;
51 	int		num;
52 
53 	if (!vec) return(-1);
54 	if ((num = (char**)(*(vec - 1)) - vec) > 0)
55 	{
56 		if (!(argv = newof(0, char*, num + *argcp + 1, 0)))
57 		{
58 			vecfree(vec, 0);
59 			return(-1);
60 		}
61 		oargv = *argvp;
62 		*argvp = argv;
63 		*argv++ = *oargv++;
64 		ovec = vec;
65 		while (s = *argv = *vec++)
66 		{
67 			while (isspace(*s)) s++;
68 			if (*s) argv++;
69 		}
70 		vecfree(ovec, 1);
71 		while (*argv = *oargv++) argv++;
72 		*argcp = argv - *argvp;
73 	}
74 	else vecfree(vec, 0);
75 	return(0);
76 }
77