2 * Copyright (c) 2002 Michal Moskal <malekith@pld-linux.org>.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Michal Moskal.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY MICHAL MOSKAL AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * This simple program is aimed to be used as script interpreter in rpm
38 * %post scriptlets and the like. It opens file passed as first argument
39 * and executes commands found there. Only linear execution is supported.
40 * For example, in glibc.spec:
42 * %post -p /sbin/postshell
46 * (full patch like /sbin/ldconfig or -/sbin/tellinit will also work).
48 * If command starts with - its exit status is ignored. Otherwise last
49 * non-zero exit status is returned.
51 * There are no builtin commands (yet :).
53 * Following commands *will* work as expected (as in Bourne shell):
55 * /bin/echo "Foo bar baz"
56 * insmod foobar options="foo bar 'qux'"
62 * echo foo || echo baz
65 * Patches and bugreports are welcome, direct them to Michal Moskal
66 * <malekith@pld-linux.org>.
70 #include <sys/types.h>
79 #define SEARCH_PATH { "/sbin/", "/bin/", "/usr/sbin/", "/usr/bin/", NULL }
84 void do_execve(char **argv)
86 char *path[] = SEARCH_PATH;
87 char file[MAX_LINE + 100];
90 if (**argv == '.' || **argv == '/')
91 execve(argv[0], argv, environ);
93 for (i = 0; path[i]; i++) {
94 strcpy(file, path[i]);
95 strcat(file, argv[0]);
96 execve(file, argv, environ);
100 int exec_and_wait(char **argv)
109 } else if (pid == 0) {
118 err = waitpid(pid, &status, 0);
123 return WEXITSTATUS(status);
127 void split_argv(char **argv, char *s)
132 for (argc = 0; argc < MAX_ARGS; argc++) {
133 while (*s == ' ' || *s == '\t')
142 while (*s && *s != ' ' && *s != '\t') {
143 if (*s == '\'' || *s == '"') {
145 while (*s && *s != delim)
164 void exec_line(char *s)
166 char *argv[MAX_ARGS + 1];
178 ret = exec_and_wait(argv);
180 if (ret && !ignore_status)
184 void exec_file(FILE *f)
188 while (fgets(line, sizeof(line), f)) {
190 if (line[strlen(line) - 1] == '\n')
191 line[strlen(line) - 1] = 0;
197 int main(int argc, char **argv)
202 fprintf(stderr, "USAGE: %s filename\n", argv[0]);
206 f = fopen(argv[1], "r");