run_parts.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. /* This is my first attempt to write a program in C (well, this is my first
  30. * attempt to write a program! :-) . */
  31. /* This piece of code is heavily based on the original version of run-parts,
  32. * taken from debian-utils. I've only removed the long options and a the
  33. * report mode. As the original run-parts support only long options, I've
  34. * broken compatibility because the BusyBox policy doesn't allow them.
  35. * The supported options are:
  36. * -t test. Print the name of the files to be executed, without
  37. * execute them.
  38. * -a ARG argument. Pass ARG as an argument the program executed. It can
  39. * be repeated to pass multiple arguments.
  40. * -u MASK umask. Set the umask of the program executed to MASK. */
  41. /* TODO
  42. * done - convert calls to error in perror... and remove error()
  43. * done - convert malloc/realloc to their x... counterparts
  44. * done - remove catch_sigchld
  45. * done - use bb's concat_path_file()
  46. * done - declare run_parts_main() as extern and any other function as static?
  47. */
  48. #include <getopt.h>
  49. #include <stdlib.h>
  50. #include "libbb.h"
  51. static const struct option runparts_long_options[] = {
  52. { "test", 0, NULL, 't' },
  53. { "umask", 1, NULL, 'u' },
  54. { "arg", 1, NULL, 'a' },
  55. { 0, 0, 0, 0 }
  56. };
  57. extern char **environ;
  58. /* run_parts_main */
  59. /* Process options */
  60. int run_parts_main(int argc, char **argv)
  61. {
  62. char **args = xmalloc(2 * sizeof(char *));
  63. unsigned char test_mode = 0;
  64. unsigned short argcount = 1;
  65. int opt;
  66. umask(022);
  67. while ((opt = getopt_long (argc, argv, "tu:a:",
  68. runparts_long_options, NULL)) > 0)
  69. {
  70. switch (opt) {
  71. /* Enable test mode */
  72. case 't':
  73. test_mode++;
  74. break;
  75. /* Set the umask of the programs executed */
  76. case 'u':
  77. /* Check and set the umask of the program executed. As stated in the original
  78. * run-parts, the octal conversion in libc is not foolproof; it will take the
  79. * 8 and 9 digits under some circumstances. We'll just have to live with it.
  80. */
  81. umask(bb_xgetlarg(optarg, 8, 0, 07777));
  82. break;
  83. /* Pass an argument to the programs */
  84. case 'a':
  85. /* Add an argument to the commands that we will call.
  86. * Called once for every argument. */
  87. args = xrealloc(args, (argcount + 2) * (sizeof(char *)));
  88. args[argcount++] = optarg;
  89. break;
  90. default:
  91. bb_show_usage();
  92. }
  93. }
  94. /* We require exactly one argument: the directory name */
  95. if (optind != (argc - 1)) {
  96. bb_show_usage();
  97. }
  98. args[0] = argv[optind];
  99. args[argcount] = 0;
  100. return(run_parts(args, test_mode, environ));
  101. }