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>
82 #define SEARCH_PATH { "/sbin/", "/bin/", "/usr/sbin/", "/usr/bin/", NULL }
87 void do_execve(char **argv)
89 char *path[] = SEARCH_PATH;
90 char file[MAX_LINE + 100];
93 if (**argv == '.' || **argv == '/')
94 execve(argv[0], argv, environ);
96 for (i = 0; path[i]; i++) {
97 strcpy(file, path[i]);
98 strcat(file, argv[0]);
99 execve(file, argv, environ);
103 int exec_and_wait(char **argv)
112 } else if (pid == 0) {
121 err = waitpid(pid, &status, 0);
126 return WEXITSTATUS(status);
130 void split_argv(char **argv, char *s)
135 for (argc = 0; argc < MAX_ARGS; argc++) {
136 while (*s == ' ' || *s == '\t')
145 while (*s && *s != ' ' && *s != '\t') {
146 if (*s == '\'' || *s == '"') {
148 while (*s && *s != delim)
167 void exec_line(char *s)
169 char *argv[MAX_ARGS + 1];
181 ret = exec_and_wait(argv);
183 if (ret && !ignore_status)
187 void exec_file(int fd)
193 if (fstat(fd, &sbuf) < 0) {
198 if ((p = mmap(0, sbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0)) < 0) {
203 for (a = s = p; s < p + sbuf.st_size; s++) {
205 memcpy(line, a, s - a);
212 // last line was not terminated.
213 if (s == p + sbuf.st_size) {
214 memcpy(line, a, s - a);
219 if (munmap(p, sbuf.st_size) < 0) {
225 #define error(msg) write(2, msg, strlen(msg))
226 int main(int argc, char **argv)
233 error(" filename\n");
237 fd = open(argv[1], O_RDONLY);