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> |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 11 | * |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 12 | * |
Bernhard Reutner-Fischer | e15d757 | 2006-06-02 20:56:16 +0000 | [diff] [blame] | 13 | * Licensed under GPL v2, see file LICENSE in this tarball for details. |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | /* This is my first attempt to write a program in C (well, this is my first |
| 17 | * attempt to write a program! :-) . */ |
| 18 | |
| 19 | /* This piece of code is heavily based on the original version of run-parts, |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 20 | * taken from debian-utils. I've only removed the long options and a the |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 21 | * report mode. As the original run-parts support only long options, I've |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 22 | * broken compatibility because the BusyBox policy doesn't allow them. |
| 23 | * The supported options are: |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 24 | * -t test. Print the name of the files to be executed, without |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 25 | * execute them. |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 26 | * -a ARG argument. Pass ARG as an argument the program executed. It can |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 27 | * be repeated to pass multiple arguments. |
| 28 | * -u MASK umask. Set the umask of the program executed to MASK. */ |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 29 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 30 | /* TODO |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 31 | * done - convert calls to error in perror... and remove error() |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 32 | * done - convert malloc/realloc to their x... counterparts |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 33 | * done - remove catch_sigchld |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 34 | * done - use bb's concat_path_file() |
Eric Andersen | fb74a45 | 2001-12-18 14:06:03 +0000 | [diff] [blame] | 35 | * done - declare run_parts_main() as extern and any other function as static? |
| 36 | */ |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 37 | |
Bernhard Reutner-Fischer | e15d757 | 2006-06-02 20:56:16 +0000 | [diff] [blame] | 38 | #include "busybox.h" |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 39 | #include <getopt.h> |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 40 | #include <stdlib.h> |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 41 | |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 42 | |
Eric Andersen | a1ed06b | 2003-07-26 09:16:00 +0000 | [diff] [blame] | 43 | static const struct option runparts_long_options[] = { |
| 44 | { "test", 0, NULL, 't' }, |
| 45 | { "umask", 1, NULL, 'u' }, |
| 46 | { "arg", 1, NULL, 'a' }, |
| 47 | { 0, 0, 0, 0 } |
| 48 | }; |
| 49 | |
Eric Andersen | 2a18689 | 2003-07-29 07:05:40 +0000 | [diff] [blame] | 50 | extern char **environ; |
| 51 | |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 52 | /* run_parts_main */ |
| 53 | /* Process options */ |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 54 | int run_parts_main(int argc, char **argv) |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 55 | { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 56 | char **args = xmalloc(2 * sizeof(char *)); |
| 57 | unsigned char test_mode = 0; |
| 58 | unsigned short argcount = 1; |
| 59 | int opt; |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 60 | |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 61 | umask(022); |
| 62 | |
Eric Andersen | a1ed06b | 2003-07-26 09:16:00 +0000 | [diff] [blame] | 63 | while ((opt = getopt_long (argc, argv, "tu:a:", |
| 64 | runparts_long_options, NULL)) > 0) |
| 65 | { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 66 | switch (opt) { |
Eric Andersen | a1ed06b | 2003-07-26 09:16:00 +0000 | [diff] [blame] | 67 | /* Enable test mode */ |
| 68 | case 't': |
| 69 | test_mode++; |
| 70 | break; |
| 71 | /* Set the umask of the programs executed */ |
| 72 | case 'u': |
| 73 | /* Check and set the umask of the program executed. As stated in the original |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 74 | * run-parts, the octal conversion in libc is not foolproof; it will take the |
Eric Andersen | a1ed06b | 2003-07-26 09:16:00 +0000 | [diff] [blame] | 75 | * 8 and 9 digits under some circumstances. We'll just have to live with it. |
| 76 | */ |
| 77 | umask(bb_xgetlarg(optarg, 8, 0, 07777)); |
| 78 | break; |
| 79 | /* Pass an argument to the programs */ |
| 80 | case 'a': |
| 81 | /* Add an argument to the commands that we will call. |
| 82 | * Called once for every argument. */ |
| 83 | args = xrealloc(args, (argcount + 2) * (sizeof(char *))); |
| 84 | args[argcount++] = optarg; |
| 85 | break; |
| 86 | default: |
| 87 | bb_show_usage(); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
| 91 | /* We require exactly one argument: the directory name */ |
| 92 | if (optind != (argc - 1)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 93 | bb_show_usage(); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 94 | } |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 95 | |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 96 | args[0] = argv[optind]; |
Glenn L McGrath | a69fd2e | 2003-01-08 03:26:47 +0000 | [diff] [blame] | 97 | args[argcount] = 0; |
| 98 | |
Eric Andersen | 2a18689 | 2003-07-29 07:05:40 +0000 | [diff] [blame] | 99 | return(run_parts(args, test_mode, environ)); |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 100 | } |