1 /* vi: set sw=4 ts=4: */
3 * run command from specified directory
6 * Copyright (C) 2001 by Emanuele Aina <emanuele.aina@tiscali.it>
7 * rewrite to vfork usage by
8 * Copyright (C) 2002 by Vladimir Oleynik <dzo@simtreas.ru>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
19 #include <sys/types.h>
30 /* True or false? Is this a valid filename (upper/lower alpha, digits,
31 * underscores, and hyphens only?)
33 static int valid_name(const struct dirent *d)
38 if (!isalnum(*c) && (*c != '_') && (*c != '-')) {
47 /* Find the parts to run & call run_part() */
48 extern int run_parts(char **args, const unsigned char test_mode)
50 struct dirent **namelist = 0;
59 /* Avoid longjmp clobbering */
63 /* scandir() isn't POSIX, but it makes things easy. */
64 entries = scandir(arg0, &namelist, valid_name, alphasort);
67 perror_msg_and_die("failed to open directory %s", arg0);
70 for (i = 0; i < entries; i++) {
72 filename = concat_path_file(arg0, namelist[i]->d_name);
74 if (stat(filename, &st) < 0) {
75 perror_msg_and_die("failed to stat component %s", filename);
77 if (S_ISREG(st.st_mode) && !access(filename, X_OK)) {
81 /* exec_errno is common vfork variable */
82 volatile int exec_errno = 0;
86 if ((pid = vfork()) < 0) {
87 perror_msg_and_die("failed to fork");
90 execv(filename, args);
95 waitpid(pid, &result, 0);
98 perror_msg_and_die("failed to exec %s", filename);
100 if (WIFEXITED(result) && WEXITSTATUS(result)) {
101 perror_msg("%s exited with return code %d", filename, WEXITSTATUS(result));
103 } else if (WIFSIGNALED(result)) {
104 perror_msg("%s exited because of uncaught signal %d", filename, WTERMSIG(result));
109 else if (!S_ISDIR(st.st_mode)) {
110 error_msg("component %s is not an executable plain file", filename);