aboutsummaryrefslogtreecommitdiff
path: root/coreutils/chmod.c
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2006-10-27 15:13:54 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2006-10-27 15:13:54 +0000
commitfefb279ace2da131cad369a3cc6983a1bc220a4a (patch)
tree374399ab7ac8a8bd422308482a548e0759490318 /coreutils/chmod.c
parentcf70433173663a5d627d2a049328cd580f7e9220 (diff)
downloadbusybox-w32-fefb279ace2da131cad369a3cc6983a1bc220a4a.tar.gz
busybox-w32-fefb279ace2da131cad369a3cc6983a1bc220a4a.tar.bz2
busybox-w32-fefb279ace2da131cad369a3cc6983a1bc220a4a.zip
chmod: support -vcf if CONFIG_DESKTOP
Diffstat (limited to 'coreutils/chmod.c')
-rw-r--r--coreutils/chmod.c97
1 files changed, 44 insertions, 53 deletions
diff --git a/coreutils/chmod.c b/coreutils/chmod.c
index c871ad10e..18334b8bb 100644
--- a/coreutils/chmod.c
+++ b/coreutils/chmod.c
@@ -11,78 +11,69 @@
11 */ 11 */
12 12
13/* BB_AUDIT SUSv3 compliant */ 13/* BB_AUDIT SUSv3 compliant */
14/* BB_AUDIT GNU defects - unsupported options -c, -f, -v, and long options. */ 14/* BB_AUDIT GNU defects - unsupported long options. */
15/* http://www.opengroup.org/onlinepubs/007904975/utilities/chmod.html */ 15/* http://www.opengroup.org/onlinepubs/007904975/utilities/chmod.html */
16 16
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <unistd.h>
21#include <sys/stat.h>
22#include "busybox.h" 17#include "busybox.h"
23 18
19#define OPT_RECURSE (option_mask32 & 1)
20#define OPT_VERBOSE (USE_DESKTOP(option_mask32 & 2) SKIP_DESKTOP(0))
21#define OPT_CHANGED (USE_DESKTOP(option_mask32 & 4) SKIP_DESKTOP(0))
22#define OPT_QUIET (USE_DESKTOP(option_mask32 & 8) SKIP_DESKTOP(0))
23#define OPT_STR ("-R" USE_DESKTOP("vcf"))
24
24static int fileAction(const char *fileName, struct stat *statbuf, void* junk) 25static int fileAction(const char *fileName, struct stat *statbuf, void* junk)
25{ 26{
26 if (!bb_parse_mode((char *)junk, &(statbuf->st_mode))) 27 mode_t newmode = statbuf->st_mode;
27 bb_error_msg_and_die( "invalid mode: %s", (char *)junk); 28 if (!bb_parse_mode((char *)junk, &newmode))
28 if (chmod(fileName, statbuf->st_mode) == 0) 29 bb_error_msg_and_die("invalid mode: %s", (char *)junk);
29 return (TRUE); 30
30 bb_perror_msg("%s", fileName); /* Avoid multibyte problems. */ 31 if (chmod(fileName, statbuf->st_mode) == 0) {
31 return (FALSE); 32 if (OPT_VERBOSE /* -v verbose? or -c changed? */
33 || (OPT_CHANGED && statbuf->st_mode != newmode)
34 ) {
35 printf("mode of '%s' changed to %04o (%s)\n", fileName,
36 newmode & 7777, bb_mode_string(newmode)+1);
37 }
38 return TRUE;
39 }
40 if (!OPT_QUIET) /* not silent (-f)? */
41 bb_perror_msg("%s", fileName);
42 return FALSE;
32} 43}
33 44
34int chmod_main(int ATTRIBUTE_UNUSED argc, char **argv) 45int chmod_main(int argc, char **argv)
35{ 46{
36 int retval = EXIT_SUCCESS; 47 int retval = EXIT_SUCCESS;
37 int recursiveFlag = FALSE; 48 char *arg, **argp;
38 int count;
39 char *smode; 49 char *smode;
40 char **p;
41 char *p0;
42 char opt = '-';
43 50
44 ++argv; 51 /* Convert first encountered -r into a-r, etc */
45 count = 0; 52 argp = argv + 1;
46 53 while ((arg = *argp)) {
47 for (p = argv ; *p ; p++) { 54 /* Protect against mishandling e.g. "chmod 644 -r" */
48 p0 = p[0]; 55 if (arg[0] != '-')
49 if (p0[0] == opt) { 56 break;
50 if ((p0[1] == '-') && !p0[2]) { 57 /* An option. Not a -- or valid option? */
51 opt = 0; /* Disable further option processing. */ 58 if (arg[1] && !strchr(OPT_STR, arg[1])) {
52 continue; 59 argp[0] = xasprintf("a%s", arg);
53 } 60 break;
54 if (p0[1] == 'R') {
55 char *s = p0 + 2;
56 while (*s == 'R') {
57 ++s;
58 }
59 if (*s) {
60 bb_show_usage();
61 }
62 recursiveFlag = TRUE;
63 continue;
64 }
65 if (count) {
66 bb_show_usage();
67 }
68 } 61 }
69 argv[count] = p0; 62 argp++;
70 ++count;
71 } 63 }
64 /* "chmod -rzzz abc" will say "invalid mode: a-rzzz"!
65 * It is easily fixable, but deemed not worth the code */
72 66
73 argv[count] = NULL; 67 opt_complementary = "-2";
74 68 getopt32(argc, argv, OPT_STR + 1); /* Reuse string */
75 if (count < 2) { 69 argv += optind;
76 bb_show_usage();
77 }
78 70
79 smode = *argv; 71 smode = *argv++;
80 ++argv;
81 72
82 /* Ok, ready to do the deed now */ 73 /* Ok, ready to do the deed now */
83 do { 74 do {
84 if (! recursive_action (*argv, recursiveFlag, TRUE, FALSE, 75 if (!recursive_action(*argv, OPT_RECURSE, TRUE, FALSE,
85 fileAction, fileAction, smode)) { 76 fileAction, fileAction, smode)) {
86 retval = EXIT_FAILURE; 77 retval = EXIT_FAILURE;
87 } 78 }
88 } while (*++argv); 79 } while (*++argv);