1 /* vi: set sw=4 ts=4: */
3 * Mini chmod implementation for busybox
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
7 * Reworked by (C) 2002 Vladimir Oleynik <dzo@simtreas.ru>
8 * to correctly parse '-rwxgoa'
10 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
13 /* BB_AUDIT SUSv3 compliant */
14 /* BB_AUDIT GNU defects - unsupported long options. */
15 /* http://www.opengroup.org/onlinepubs/007904975/utilities/chmod.html */
19 /* This is a NOEXEC applet. Be very careful! */
22 #define OPT_RECURSE (option_mask32 & 1)
23 #define OPT_VERBOSE (IF_DESKTOP(option_mask32 & 2) IF_NOT_DESKTOP(0))
24 #define OPT_CHANGED (IF_DESKTOP(option_mask32 & 4) IF_NOT_DESKTOP(0))
25 #define OPT_QUIET (IF_DESKTOP(option_mask32 & 8) IF_NOT_DESKTOP(0))
26 #define OPT_STR "R" IF_DESKTOP("vcf")
29 * chmod never changes the permissions of symbolic links; the chmod
30 * system call cannot change their permissions. This is not a problem
31 * since the permissions of symbolic links are never used.
32 * However, for each symbolic link listed on the command line, chmod changes
33 * the permissions of the pointed-to file. In contrast, chmod ignores
34 * symbolic links encountered during recursive directory traversals.
37 static int FAST_FUNC fileAction(const char *fileName, struct stat *statbuf, void* param, int depth)
41 /* match coreutils behavior */
43 /* statbuf holds lstat result, but we need stat (follow link) */
44 if (stat(fileName, statbuf))
46 } else { /* depth > 0: skip links */
47 if (S_ISLNK(statbuf->st_mode))
50 newmode = statbuf->st_mode;
52 if (!bb_parse_mode((char *)param, &newmode))
53 bb_error_msg_and_die("invalid mode: %s", (char *)param);
55 if (chmod(fileName, newmode) == 0) {
57 || (OPT_CHANGED && statbuf->st_mode != newmode)
59 printf("mode of '%s' changed to %04o (%s)\n", fileName,
60 newmode & 07777, bb_mode_string(newmode)+1);
66 bb_simple_perror_msg(fileName);
70 int chmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
71 int chmod_main(int argc UNUSED_PARAM, char **argv)
73 int retval = EXIT_SUCCESS;
77 /* Convert first encountered -r into ar, -w into aw etc
78 * so that getopt would not eat it */
80 while ((arg = *++argp)) {
81 /* Mode spec must be the first arg (sans -R etc) */
82 /* (protect against mishandling e.g. "chmod 644 -r") */
87 /* An option. Not a -- or valid option? */
88 if (arg[1] && !strchr("-"OPT_STR, arg[1])) {
95 opt_complementary = "-2";
96 getopt32(argv, ("-"OPT_STR) + 1); /* Reuse string */
99 /* Restore option-like mode if needed */
100 if (arg) arg[0] = '-';
102 /* Ok, ready to do the deed now */
105 if (!recursive_action(*argv,
106 OPT_RECURSE, // recurse
107 fileAction, // file action
108 fileAction, // dir action
112 retval = EXIT_FAILURE;
120 Security: chmod is too important and too subtle.
121 This is a test script (busybox chmod versus coreutils).
122 Run it in empty directory.
125 t1="/tmp/busybox chmod"
143 (cd test1; ls -lR) >out1
144 (cd test2; ls -lR) >out2
145 echo "chmod $1" >out.diff
146 if ! diff -u out1 out2 >>out.diff; then exit 1; fi
149 echo "If script produced 'out.diff' file, then at least one testcase failed"
150 create test1; create test2
157 tst "-R a+w linkfile"
159 tst "a-r,a+x linkfile"