Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 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 |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 48 | * done - use bb's concat_path_file() |
Eric Andersen | fb74a45 | 2001-12-18 14:06:03 +0000 | [diff] [blame] | 49 | * done - declare run_parts_main() as extern and any other function as static? |
| 50 | */ |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 51 | |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 52 | #include <getopt.h> |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 53 | #include <stdlib.h> |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 54 | |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 55 | #include "libbb.h" |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 56 | |
Eric Andersen | a1ed06b | 2003-07-26 09:16:00 +0000 | [diff] [blame] | 57 | static const struct option runparts_long_options[] = { |
| 58 | { "test", 0, NULL, 't' }, |
| 59 | { "umask", 1, NULL, 'u' }, |
| 60 | { "arg", 1, NULL, 'a' }, |
| 61 | { 0, 0, 0, 0 } |
| 62 | }; |
| 63 | |
Eric Andersen | 2a18689 | 2003-07-29 07:05:40 +0000 | [diff] [blame] | 64 | extern char **environ; |
| 65 | |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 66 | /* run_parts_main */ |
| 67 | /* Process options */ |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 68 | int run_parts_main(int argc, char **argv) |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 69 | { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 70 | char **args = xmalloc(2 * sizeof(char *)); |
| 71 | unsigned char test_mode = 0; |
| 72 | unsigned short argcount = 1; |
| 73 | int opt; |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 74 | |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 75 | umask(022); |
| 76 | |
Eric Andersen | a1ed06b | 2003-07-26 09:16:00 +0000 | [diff] [blame] | 77 | while ((opt = getopt_long (argc, argv, "tu:a:", |
| 78 | runparts_long_options, NULL)) > 0) |
| 79 | { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 80 | switch (opt) { |
Eric Andersen | a1ed06b | 2003-07-26 09:16:00 +0000 | [diff] [blame] | 81 | /* Enable test mode */ |
| 82 | case 't': |
| 83 | test_mode++; |
| 84 | break; |
| 85 | /* Set the umask of the programs executed */ |
| 86 | case 'u': |
| 87 | /* Check and set the umask of the program executed. As stated in the original |
| 88 | * run-parts, the octal conversion in libc is not foolproof; it will take the |
| 89 | * 8 and 9 digits under some circumstances. We'll just have to live with it. |
| 90 | */ |
| 91 | umask(bb_xgetlarg(optarg, 8, 0, 07777)); |
| 92 | break; |
| 93 | /* Pass an argument to the programs */ |
| 94 | case 'a': |
| 95 | /* Add an argument to the commands that we will call. |
| 96 | * Called once for every argument. */ |
| 97 | args = xrealloc(args, (argcount + 2) * (sizeof(char *))); |
| 98 | args[argcount++] = optarg; |
| 99 | break; |
| 100 | default: |
| 101 | bb_show_usage(); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
| 105 | /* We require exactly one argument: the directory name */ |
| 106 | if (optind != (argc - 1)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 107 | bb_show_usage(); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 108 | } |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 109 | |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 110 | args[0] = argv[optind]; |
Glenn L McGrath | a69fd2e | 2003-01-08 03:26:47 +0000 | [diff] [blame] | 111 | args[argcount] = 0; |
| 112 | |
Eric Andersen | 2a18689 | 2003-07-29 07:05:40 +0000 | [diff] [blame] | 113 | return(run_parts(args, test_mode, environ)); |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 114 | } |