1 /* $NetBSD: fexec.c,v 1.1.1.3 2009/08/06 16:55:26 joerg Exp $ */
4 * Copyright (c) 2003 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
38 #include <sys/cdefs.h>
62 __RCSID("$NetBSD: fexec.c,v 1.1.1.3 2009/08/06 16:55:26 joerg Exp $");
64 static int vfcexec(const char *, int, const char *, va_list);
67 * fork, then change current working directory to path and
68 * execute the command and arguments in the argv array.
69 * wait for the command to finish, then return the exit status.
72 pfcexec(const char *path
, const char *file
, const char **argv
)
80 if ((path
!= NULL
) && (chdir(path
) < 0))
83 (void)execvp(file
, __UNCONST(argv
));
90 while (waitpid(child
, &status
, 0) < 0) {
95 if (!WIFEXITED(status
))
98 return WEXITSTATUS(status
);
102 vfcexec(const char *path
, int skipempty
, const char *arg
, va_list ap
)
105 size_t argv_size
, argc
;
109 argv
= xcalloc(argv_size
, sizeof(*argv
));
115 if (argc
== argv_size
) {
117 argv
= xrealloc(argv
, argv_size
* sizeof(*argv
));
119 arg
= va_arg(ap
, const char *);
120 if (skipempty
&& arg
&& strlen(arg
) == 0)
123 } while (arg
!= NULL
);
125 retval
= pfcexec(path
, argv
[0], argv
);
131 fexec(const char *arg
, ...)
137 result
= vfcexec(NULL
, 0, arg
, ap
);
144 fexec_skipempty(const char *arg
, ...)
150 result
= vfcexec(NULL
, 1, arg
, ap
);
157 fcexec(const char *path
, const char *arg
, ...)
163 result
= vfcexec(path
, 0, arg
, ap
);