8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / lib / libinstzones / common / zones_args.c
bloba400cfef9a5b118e12ae6dc20d2c6b3bc1a7d2ee
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
29 * Module: zones_args.c
30 * Group: libinstzones
31 * Description: Private functions used by zones library functions to manipulate
32 * argument lists
34 * Public Methods:
36 * _z_add_arg - add new argument to argument array for use in exec() calls
37 * _z_free_args - free all storage contained in an argument array previously
38 * _z_get_argc - return (int) argc count from argument array
39 * _z_get_argv - return (char **)argv pointer from argument array
40 * _z_new_args - create a new argument array for use in exec() calls
44 * System includes
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <unistd.h>
50 #include <fcntl.h>
51 #include <ctype.h>
52 #include <sys/types.h>
53 #include <sys/param.h>
54 #include <string.h>
55 #include <strings.h>
56 #include <stdarg.h>
57 #include <limits.h>
58 #include <errno.h>
59 #include <stropts.h>
60 #include <libintl.h>
61 #include <locale.h>
62 #include <assert.h>
65 * local includes
68 #include "instzones_lib.h"
69 #include "zones_strings.h"
72 * Private structures
76 * Library Function Prototypes
80 * Local Function Prototypes
84 * Global internal (private) declarations
88 * *****************************************************************************
89 * global external (public) functions
90 * *****************************************************************************
94 * Name: _z_add_arg
95 * Description: add new argument to argument array for use in exec() calls
96 * Arguments: a_args - [RO, *RW] - (argArray_t *)
97 * Pointer to argument array (previously allocated via
98 * a call to _z_new_args) to add the argument to
99 * a_format - [RO, *RO] - (char *)
100 * Pointer to "printf(3C)" style format argument
101 * ... - [RO, *RO] - (varies)
102 * Arguments as appropriate for format argument specified
103 * Returns: boolean_t
104 * B_TRUE - success
105 * B_FALSE - failure
106 * Examples:
107 * - to add an argument that specifies a file descriptor:
108 * int fd;
109 * _z_add_arg(aa, "/proc/self/fd/%d", fd);
110 * - to add a flag or other known text:
111 * _z_add_arg(aa, "-s")
112 * - to add random text:
113 * char *random_text;
114 * _z_add_arg(aa, "%s", random_text);
117 /*PRINTFLIKE2*/
118 boolean_t
119 _z_add_arg(argArray_t *a_args, char *a_format, ...)
121 char *rstr = NULL;
122 char bfr[MAX_CANON];
123 size_t vres = 0;
124 va_list ap;
126 /* entry assertions */
128 assert(a_args != NULL);
129 assert(a_format != NULL);
130 assert(*a_format != '\0');
133 * double argument array if array is full
136 if (a_args->_aaNumArgs >= a_args->_aaMaxArgs) {
137 int newMax;
138 char **newArgs;
140 newMax = a_args->_aaMaxArgs * 2;
141 newArgs = (char **)_z_realloc(a_args->_aaArgs,
142 (newMax+1) * sizeof (char *));
143 a_args->_aaArgs = newArgs;
144 a_args->_aaMaxArgs = newMax;
148 * determine size of argument to add to list
151 va_start(ap, a_format);
152 vres = vsnprintf(bfr, sizeof (bfr), a_format, ap);
153 va_end(ap);
156 * use the expanded argument if it will fit in the built in buffer,
157 * otherwise, allocate space to hold the argument
160 if (vres < sizeof (bfr)) {
161 /* duplicate text already generated in buffer */
162 rstr = _z_strdup(bfr);
163 } else {
164 /* allocate new space for argument to add */
166 rstr = (char *)_z_malloc(vres+2);
168 /* generate argument to add */
170 va_start(ap, a_format);
171 vres = vsnprintf(rstr, vres+1, a_format, ap);
172 va_end(ap);
175 /* add argument to the end of the argument array */
177 a_args->_aaArgs[a_args->_aaNumArgs++] = rstr;
178 a_args->_aaArgs[a_args->_aaNumArgs] = NULL;
180 /* successful - return */
182 return (B_TRUE);
186 * Name: _z_free_args
187 * Description: free all storage contained in an argument array previously
188 * allocated by a call to _z_new_args
189 * Arguments: a_args - [RO, *RW] - (argArray_t *)
190 * Pointer to argument array (previously allocated via
191 * a call to _z_new_args) to free
192 * Returns: void
193 * NOTE: preserves errno (usually called right after e_execCmd*())
196 void
197 _z_free_args(argArray_t *a_args)
199 int i;
200 int lerrno = errno;
202 /* entry assertions */
204 assert(a_args != NULL);
205 assert(a_args->_aaArgs != NULL);
207 /* free all arguments in the argument array */
209 for (i = (a_args->_aaNumArgs-1); i >= 0; i--) {
210 assert(a_args->_aaArgs[i] != NULL);
211 (void) free(a_args->_aaArgs[i]);
214 /* free argument array */
216 (void) free(a_args->_aaArgs);
218 /* free argument array structure */
220 (void) free(a_args);
222 /* restore errno */
224 errno = lerrno;
228 * Name: _z_get_argc
229 * Description: return (int) argc count from argument array
230 * Arguments: a_args - [RO, *RW] - (argArray_t *)
231 * Pointer to argument array (previously allocated via
232 * a call to _z_new_args) to return argc count for
233 * Returns: int
234 * Count of the number of arguments in the argument array
235 * suitable for use in an exec*() call
239 _z_get_argc(argArray_t *a_args)
241 return (a_args->_aaNumArgs);
245 * Name: _z_get_argv
246 * Description: return (char **)argv pointer from argument array
247 * Arguments: a_args - [RO, *RW] - (argArray_t *)
248 * Pointer to argument array (previously allocated via
249 * a call to _z_new_args) to return argv pointer for
250 * Returns: char **
251 * Pointer to (char **)argv pointer suitable for use
252 * in an exec*() call
253 * NOTE: the actual character array is always terminated with a NULL
256 char **
257 _z_get_argv(argArray_t *a_args)
259 return (a_args->_aaArgs);
263 * Name: _z_new_args
264 * Description: create a new argument array for use in exec() calls
265 * Arguments: initialCount - [RO, *RO] - (int)
266 * Initial number of elements to populate the
267 * argument array with - use best guess
268 * Returns: argArray_t *
269 * Pointer to argument array that can be used in other
270 * functions that accept it as an argument
271 * == (argArray_t *)NULL - error
272 * NOTE: you must call _z_free_args() when the returned argument array is
273 * no longer needed so that all storage used can be freed up.
276 argArray_t *
277 _z_new_args(int initialCount)
279 argArray_t *aa;
281 /* entry assertions */
283 assert(initialCount >= 0);
285 /* if no guess on size, then assume 1 */
287 if (initialCount == 0) {
288 initialCount = 1;
291 /* allocate new argument array structure */
293 aa = (argArray_t *)_z_calloc(sizeof (argArray_t));
295 /* allocate initial argument array */
297 aa->_aaArgs = (char **)_z_calloc((initialCount+1) * sizeof (char *));
299 /* initialize argument indexes */
301 aa->_aaNumArgs = 0;
302 aa->_aaMaxArgs = initialCount;
304 /* successful - return pointer to argument array created */
306 return (aa);