1 /* vi: set sw=4 ts=4: */
3 * Mini run-parts implementation for busybox
5 * Copyright (C) 2007 Bernhard Fischer
7 * Based on a older version that was in busybox which was 1k big..
8 * Copyright (C) 2001 by Emanuele Aina <emanuele.aina@tiscali.it>
10 * Based on the Debian run-parts program, version 1.15
11 * Copyright (C) 1996 Jeff Noxon <jeff@router.patch.net>,
12 * Copyright (C) 1996-1999 Guy Maor <maor@debian.org>
15 * Licensed under GPL v2 or later, see file LICENSE in this tarball for details.
18 /* This is my first attempt to write a program in C (well, this is my first
19 * attempt to write a program! :-) . */
21 /* This piece of code is heavily based on the original version of run-parts,
22 * taken from debian-utils. I've only removed the long options and a the
23 * report mode. As the original run-parts support only long options, I've
24 * broken compatibility because the BusyBox policy doesn't allow them.
25 * The supported options are:
26 * -t test. Print the name of the files to be executed, without
28 * -a ARG argument. Pass ARG as an argument the program executed. It can
29 * be repeated to pass multiple arguments.
30 * -u MASK umask. Set the umask of the program executed to MASK.
42 #define G (*(struct globals*)&bb_common_bufsiz1)
43 #define names (G.names)
47 enum { NUM_CMD = (COMMON_BUFSIZE - sizeof(struct globals)) / sizeof(cmd[0]) };
54 OPT_l = (1 << 4) * ENABLE_FEATURE_RUN_PARTS_FANCY,
57 #if ENABLE_FEATURE_RUN_PARTS_FANCY
58 #define list_mode (option_mask32 & OPT_l)
63 /* Is this a valid filename (upper/lower alpha, digits,
64 * underscores, and hyphens only?)
66 static bool invalid_name(const char *c)
70 while (*c && (isalnum(*c) || *c == '_' || *c == '-'))
73 return *c; /* TRUE (!0) if terminating NUL is not reached */
76 static int bb_alphasort(const void *p1, const void *p2)
78 int r = strcmp(*(char **) p1, *(char **) p2);
79 return (option_mask32 & OPT_r) ? -r : r;
82 static int act(const char *file, struct stat *statbuf, void *args ATTRIBUTE_UNUSED, int depth)
88 && ( !(statbuf->st_mode & (S_IFREG | S_IFLNK))
90 || (!list_mode && access(file, X_OK) != 0))
95 names = xrealloc(names, (cur + 2) * sizeof(names[0]));
96 names[cur++] = xstrdup(file);
102 #if ENABLE_FEATURE_RUN_PARTS_LONG_OPTIONS
103 static const char runparts_longopts[] ALIGN1 =
104 "arg\0" Required_argument "a"
105 "umask\0" Required_argument "u"
106 "test\0" No_argument "t"
107 #if ENABLE_FEATURE_RUN_PARTS_FANCY
108 "list\0" No_argument "l"
109 "reverse\0" No_argument "r"
110 //TODO: "verbose\0" No_argument "v"
115 int run_parts_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
116 int run_parts_main(int argc ATTRIBUTE_UNUSED, char **argv)
118 const char *umask_p = "22";
119 llist_t *arg_list = NULL;
123 #if ENABLE_FEATURE_RUN_PARTS_LONG_OPTIONS
124 applet_long_options = runparts_longopts;
126 /* We require exactly one argument: the directory name */
127 /* We require exactly one argument: the directory name */
128 opt_complementary = "=1:a::";
129 getopt32(argv, "ra:u:t"USE_FEATURE_RUN_PARTS_FANCY("l"), &arg_list, &umask_p);
131 umask(xstrtou_range(umask_p, 8, 0, 07777));
134 while (arg_list && n < NUM_CMD) {
135 cmd[n] = arg_list->data;
136 arg_list = arg_list->link;
139 /* cmd[n] = NULL; - is already zeroed out */
141 /* run-parts has to sort executables by name before running them */
143 recursive_action(argv[optind],
144 ACTION_RECURSE|ACTION_FOLLOWLINKS,
145 act, /* file action */
146 act, /* dir action */
147 NULL, /* user data */
154 qsort(names, cur, sizeof(char *), bb_alphasort);
158 char *name = *names++;
161 if (option_mask32 & (OPT_t | OPT_l)) {
166 ret = wait4pid(spawn(cmd));
171 bb_perror_msg("can't exec %s", name);
173 bb_error_msg("%s exited with code %d", name, ret);