1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33#include "libbb.h"
34
35struct globals {
36 char **names;
37 int cur;
38 char *cmd[1];
39};
40#define G (*(struct globals*)&bb_common_bufsiz1)
41#define names (G.names)
42#define cur (G.cur )
43#define cmd (G.cmd )
44
45enum { NUM_CMD = (COMMON_BUFSIZE - sizeof(G)) / sizeof(cmd[0]) - 1 };
46
47enum {
48 OPT_r = (1 << 0),
49 OPT_a = (1 << 1),
50 OPT_u = (1 << 2),
51 OPT_t = (1 << 3),
52 OPT_l = (1 << 4) * ENABLE_FEATURE_RUN_PARTS_FANCY,
53};
54
55#if ENABLE_FEATURE_RUN_PARTS_FANCY
56#define list_mode (option_mask32 & OPT_l)
57#else
58#define list_mode 0
59#endif
60
61
62
63
64static bool invalid_name(const char *c)
65{
66 c = bb_basename(c);
67
68 while (*c && (isalnum(*c) || *c == '_' || *c == '-'))
69 c++;
70
71 return *c;
72}
73
74static int bb_alphasort(const void *p1, const void *p2)
75{
76 int r = strcmp(*(char **) p1, *(char **) p2);
77 return (option_mask32 & OPT_r) ? -r : r;
78}
79
80static int FAST_FUNC act(const char *file, struct stat *statbuf, void *args UNUSED_PARAM, int depth)
81{
82 if (depth == 1)
83 return TRUE;
84
85 if (depth == 2
86 && ( !(statbuf->st_mode & (S_IFREG | S_IFLNK))
87 || invalid_name(file)
88 || (!list_mode && access(file, X_OK) != 0))
89 ) {
90 return SKIP;
91 }
92
93 names = xrealloc_vector(names, 4, cur);
94 names[cur++] = xstrdup(file);
95
96
97 return TRUE;
98}
99
100#if ENABLE_FEATURE_RUN_PARTS_LONG_OPTIONS
101static const char runparts_longopts[] ALIGN1 =
102 "arg\0" Required_argument "a"
103 "umask\0" Required_argument "u"
104 "test\0" No_argument "t"
105#if ENABLE_FEATURE_RUN_PARTS_FANCY
106 "list\0" No_argument "l"
107 "reverse\0" No_argument "r"
108
109#endif
110 ;
111#endif
112
113int run_parts_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
114int run_parts_main(int argc UNUSED_PARAM, char **argv)
115{
116 const char *umask_p = "22";
117 llist_t *arg_list = NULL;
118 unsigned n;
119 int ret;
120
121#if ENABLE_FEATURE_RUN_PARTS_LONG_OPTIONS
122 applet_long_options = runparts_longopts;
123#endif
124
125
126 opt_complementary = "=1:a::";
127 getopt32(argv, "ra:u:t"USE_FEATURE_RUN_PARTS_FANCY("l"), &arg_list, &umask_p);
128
129 umask(xstrtou_range(umask_p, 8, 0, 07777));
130
131 n = 1;
132 while (arg_list && n < NUM_CMD) {
133 cmd[n++] = llist_pop(&arg_list);
134 }
135
136
137
138
139 recursive_action(argv[optind],
140 ACTION_RECURSE|ACTION_FOLLOWLINKS,
141 act,
142 act,
143 NULL,
144 1
145 );
146
147 if (!names)
148 return 0;
149
150 qsort(names, cur, sizeof(char *), bb_alphasort);
151
152 n = 0;
153 while (1) {
154 char *name = *names++;
155 if (!name)
156 break;
157 if (option_mask32 & (OPT_t | OPT_l)) {
158 puts(name);
159 continue;
160 }
161 cmd[0] = name;
162 ret = wait4pid(spawn(cmd));
163 if (ret == 0)
164 continue;
165 n = 1;
166 if (ret < 0)
167 bb_perror_msg("can't exec %s", name);
168 else
169 bb_error_msg("%s exited with code %d", name, ret);
170 }
171
172 return n;
173}
174