diff options
author | bug1 <bug1@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2002-11-11 06:21:00 +0000 |
---|---|---|
committer | bug1 <bug1@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2002-11-11 06:21:00 +0000 |
commit | 5a951e0f39d1cb4302490f38245fc0564815a9a7 (patch) | |
tree | c5a1aa31cac00d7bd5bcfa466374ff746764f3f2 /libbb | |
parent | 1ea2348cede280ccd3d3f788ae40a1653b34fdf9 (diff) | |
download | busybox-w32-5a951e0f39d1cb4302490f38245fc0564815a9a7.tar.gz busybox-w32-5a951e0f39d1cb4302490f38245fc0564815a9a7.tar.bz2 busybox-w32-5a951e0f39d1cb4302490f38245fc0564815a9a7.zip |
Move awk from textutils to editors. Cleanup run-parts, saves 200 bytes, moves the guts of run_parts to libbb to be used by ifupdown.
git-svn-id: svn://busybox.net/trunk/busybox@5909 69ca8d6d-28ef-0310-b511-8ec308f3f277
Diffstat (limited to 'libbb')
-rw-r--r-- | libbb/Makefile.in | 4 | ||||
-rw-r--r-- | libbb/run_parts.c | 139 |
2 files changed, 141 insertions, 2 deletions
diff --git a/libbb/Makefile.in b/libbb/Makefile.in index 6703cf1f7..e00fd2f51 100644 --- a/libbb/Makefile.in +++ b/libbb/Makefile.in | |||
@@ -34,8 +34,8 @@ LIBBB_SRC:= \ | |||
34 | my_getgrgid.c my_getpwnam.c my_getpwnamegid.c my_getpwuid.c \ | 34 | my_getgrgid.c my_getpwnam.c my_getpwnamegid.c my_getpwuid.c \ |
35 | parse_mode.c parse_number.c perror_msg.c perror_msg_and_die.c \ | 35 | parse_mode.c parse_number.c perror_msg.c perror_msg_and_die.c \ |
36 | print_file.c process_escape_sequence.c read_package_field.c \ | 36 | print_file.c process_escape_sequence.c read_package_field.c \ |
37 | recursive_action.c safe_read.c safe_strncpy.c syscalls.c \ | 37 | recursive_action.c run_parts.c safe_read.c safe_strncpy.c \ |
38 | syslog_msg_with_name.c time_string.c trim.c \ | 38 | syscalls.c syslog_msg_with_name.c time_string.c trim.c \ |
39 | vdprintf.c verror_msg.c vperror_msg.c wfopen.c xgetcwd.c xreadlink.c \ | 39 | vdprintf.c verror_msg.c vperror_msg.c wfopen.c xgetcwd.c xreadlink.c \ |
40 | xregcomp.c interface.c remove_file.c last_char_is.c copyfd.c \ | 40 | xregcomp.c interface.c remove_file.c last_char_is.c copyfd.c \ |
41 | vherror_msg.c herror_msg.c herror_msg_and_die.c xgethostbyname.c \ | 41 | vherror_msg.c herror_msg.c herror_msg_and_die.c xgethostbyname.c \ |
diff --git a/libbb/run_parts.c b/libbb/run_parts.c new file mode 100644 index 000000000..bf906a399 --- /dev/null +++ b/libbb/run_parts.c | |||
@@ -0,0 +1,139 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Mini run-parts implementation for busybox | ||
4 | * | ||
5 | * | ||
6 | * Copyright (C) 2001 by Emanuele Aina <emanuele.aina@tiscali.it> | ||
7 | * | ||
8 | * Based on the Debian run-parts program, version 1.15 | ||
9 | * Copyright (C) 1996 Jeff Noxon <jeff@router.patch.net>, | ||
10 | * Copyright (C) 1996-1999 Guy Maor <maor@debian.org> | ||
11 | * | ||
12 | * | ||
13 | * This program is free software; you can redistribute it and/or modify | ||
14 | * it under the terms of the GNU General Public License as published by | ||
15 | * the Free Software Foundation; either version 2 of the License, or | ||
16 | * (at your option) any later version. | ||
17 | * | ||
18 | * This program is distributed in the hope that it will be useful, | ||
19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
21 | * General Public License for more details. | ||
22 | * | ||
23 | * You should have received a copy of the GNU General Public License | ||
24 | * along with this program; if not, write to the Free Software | ||
25 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA | ||
26 | * 02111-1307 USA | ||
27 | * | ||
28 | */ | ||
29 | |||
30 | /* This is my first attempt to write a program in C (well, this is my first | ||
31 | * attempt to write a program! :-) . */ | ||
32 | |||
33 | /* This piece of code is heavily based on the original version of run-parts, | ||
34 | * taken from debian-utils. I've only removed the long options and a the | ||
35 | * report mode. As the original run-parts support only long options, I've | ||
36 | * broken compatibility because the BusyBox policy doesn't allow them. | ||
37 | * The supported options are: | ||
38 | * -t test. Print the name of the files to be executed, without | ||
39 | * execute them. | ||
40 | * -a ARG argument. Pass ARG as an argument the program executed. It can | ||
41 | * be repeated to pass multiple arguments. | ||
42 | * -u MASK umask. Set the umask of the program executed to MASK. */ | ||
43 | |||
44 | /* TODO | ||
45 | * done - convert calls to error in perror... and remove error() | ||
46 | * done - convert malloc/realloc to their x... counterparts | ||
47 | * done - remove catch_sigchld | ||
48 | * done - use bb's concat_path_file() | ||
49 | * done - declare run_parts_main() as extern and any other function as static? | ||
50 | */ | ||
51 | |||
52 | #include <sys/types.h> | ||
53 | #include <sys/wait.h> | ||
54 | #include <stdlib.h> | ||
55 | #include <dirent.h> | ||
56 | #include <unistd.h> | ||
57 | #include <ctype.h> | ||
58 | |||
59 | #include "libbb.h" | ||
60 | |||
61 | /* valid_name */ | ||
62 | /* True or false? Is this a valid filename (upper/lower alpha, digits, | ||
63 | * underscores, and hyphens only?) | ||
64 | */ | ||
65 | static int valid_name(const struct dirent *d) | ||
66 | { | ||
67 | char *c = d->d_name; | ||
68 | |||
69 | while (*c) { | ||
70 | if (!isalnum(*c) && (*c != '_') && (*c != '-')) { | ||
71 | return 0; | ||
72 | } | ||
73 | ++c; | ||
74 | } | ||
75 | return 1; | ||
76 | } | ||
77 | |||
78 | /* run_parts */ | ||
79 | /* Find the parts to run & call run_part() */ | ||
80 | extern int run_parts(char **args, const unsigned char test_mode) | ||
81 | { | ||
82 | struct dirent **namelist = 0; | ||
83 | struct stat st; | ||
84 | char *filename; | ||
85 | int entries; | ||
86 | int i; | ||
87 | int exitstatus = 0; | ||
88 | |||
89 | /* scandir() isn't POSIX, but it makes things easy. */ | ||
90 | entries = scandir(args[0], &namelist, valid_name, alphasort); | ||
91 | |||
92 | if (entries == -1) { | ||
93 | perror_msg_and_die("failed to open directory %s", args[0]); | ||
94 | } | ||
95 | |||
96 | for (i = 0; i < entries; i++) { | ||
97 | |||
98 | filename = concat_path_file(args[0], namelist[i]->d_name); | ||
99 | |||
100 | if (stat(filename, &st) < 0) { | ||
101 | perror_msg_and_die("failed to stat component %s", filename); | ||
102 | } | ||
103 | if (S_ISREG(st.st_mode) && !access(filename, X_OK)) { | ||
104 | if (test_mode) | ||
105 | printf("run-parts would run %s\n", filename); | ||
106 | else { | ||
107 | int result; | ||
108 | int pid; | ||
109 | |||
110 | if ((pid = fork()) < 0) { | ||
111 | perror_msg_and_die("failed to fork"); | ||
112 | } else if (!pid) { | ||
113 | execv(args[0], args); | ||
114 | perror_msg_and_die("failed to exec %s", args[0]); | ||
115 | } | ||
116 | |||
117 | waitpid(pid, &result, 0); | ||
118 | |||
119 | if (WIFEXITED(result) && WEXITSTATUS(result)) { | ||
120 | perror_msg("%s exited with return code %d", args[0], WEXITSTATUS(result)); | ||
121 | exitstatus = 1; | ||
122 | } else if (WIFSIGNALED(result)) { | ||
123 | perror_msg("%s exited because of uncaught signal %d", args[0], WTERMSIG(result)); | ||
124 | exitstatus = 1; | ||
125 | } | ||
126 | } | ||
127 | } | ||
128 | else if (!S_ISDIR(st.st_mode)) { | ||
129 | error_msg("component %s is not an executable plain file", filename); | ||
130 | exitstatus = 1; | ||
131 | } | ||
132 | |||
133 | free(namelist[i]); | ||
134 | free(filename); | ||
135 | } | ||
136 | free(namelist); | ||
137 | |||
138 | return(exitstatus); | ||
139 | } | ||