chmod.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini chown/chmod/chgrp implementation for busybox
  4. *
  5. *
  6. * Copyright (C) 1999,2000 by Lineo, inc. and Erik Andersen
  7. * Copyright (C) 1999,2000,2001 by Erik Andersen <andersee@debian.org>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. *
  23. */
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <unistd.h>
  28. #include <getopt.h>
  29. #include "busybox.h"
  30. static int fileAction(const char *fileName, struct stat *statbuf, void* junk)
  31. {
  32. if (!parse_mode((char *)junk, &(statbuf->st_mode)))
  33. error_msg_and_die("internal error");
  34. if (chmod(fileName, statbuf->st_mode) == 0)
  35. return (TRUE);
  36. perror(fileName);
  37. return (FALSE);
  38. }
  39. int chmod_main(int argc, char **argv)
  40. {
  41. int i;
  42. int opt;
  43. int recursiveFlag = FALSE;
  44. int opt_eq_modeFlag = FALSE;
  45. /* do normal option parsing */
  46. while ((opt = getopt(argc, argv, "Rrwxst")) > 0) {
  47. switch (opt) {
  48. case 'R':
  49. recursiveFlag = TRUE;
  50. break;
  51. case 'r':
  52. case 'w':
  53. case 'x':
  54. case 's':
  55. case 't':
  56. opt_eq_modeFlag = TRUE;
  57. break;
  58. default:
  59. show_usage();
  60. }
  61. }
  62. if (opt_eq_modeFlag == TRUE) {
  63. optind--;
  64. }
  65. if (argc > optind && argc > 2 && argv[optind]) {
  66. /* Parse the specified mode */
  67. mode_t mode;
  68. if (parse_mode(argv[optind], &mode) == FALSE) {
  69. error_msg_and_die( "unknown mode: %s", argv[optind]);
  70. }
  71. } else {
  72. error_msg_and_die(too_few_args);
  73. }
  74. /* Ok, ready to do the deed now */
  75. for (i = optind + 1; i < argc; i++) {
  76. if (recursive_action (argv[i], recursiveFlag, FALSE, FALSE, fileAction,
  77. fileAction, argv[optind]) == FALSE) {
  78. return EXIT_FAILURE;
  79. }
  80. }
  81. return EXIT_SUCCESS;
  82. }
  83. /*
  84. Local Variables:
  85. c-file-style: "linux"
  86. c-basic-offset: 4
  87. tab-width: 4
  88. End:
  89. */