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 source tree.
13 //config: bool "chmod (5.5 kb)"
16 //config: chmod is used to change the access permission of files.
18 //applet:IF_CHMOD(APPLET_NOEXEC(chmod, chmod, BB_DIR_BIN, BB_SUID_DROP, chmod))
20 //kbuild:lib-$(CONFIG_CHMOD) += chmod.o
22 /* BB_AUDIT SUSv3 compliant */
23 /* BB_AUDIT GNU defects - unsupported long options. */
24 /* http://www.opengroup.org/onlinepubs/007904975/utilities/chmod.html */
26 //usage:#define chmod_trivial_usage
27 //usage: "[-R"IF_DESKTOP("cvf")"] MODE[,MODE]... FILE..."
28 //usage:#define chmod_full_usage "\n\n"
29 //usage: "Each MODE is one or more of the letters ugoa, one of the\n"
30 //usage: "symbols +-= and one or more of the letters rwxst\n"
31 //usage: "\n -R Recurse"
33 //usage: "\n -c List changed files"
34 //usage: "\n -v List all files"
35 //usage: "\n -f Hide errors"
38 //usage:#define chmod_example_usage
39 //usage: "$ ls -l /tmp/foo\n"
40 //usage: "-rw-rw-r-- 1 root root 0 Apr 12 18:25 /tmp/foo\n"
41 //usage: "$ chmod u+x /tmp/foo\n"
42 //usage: "$ ls -l /tmp/foo\n"
43 //usage: "-rwxrw-r-- 1 root root 0 Apr 12 18:25 /tmp/foo*\n"
44 //usage: "$ chmod 444 /tmp/foo\n"
45 //usage: "$ ls -l /tmp/foo\n"
46 //usage: "-r--r--r-- 1 root root 0 Apr 12 18:25 /tmp/foo\n"
50 /* This is a NOEXEC applet. Be very careful! */
53 #define OPT_RECURSE (option_mask32 & 1)
54 #define OPT_VERBOSE (IF_DESKTOP(option_mask32 & 2) IF_NOT_DESKTOP(0))
55 #define OPT_CHANGED (IF_DESKTOP(option_mask32 & 4) IF_NOT_DESKTOP(0))
56 #define OPT_QUIET (IF_DESKTOP(option_mask32 & 8) IF_NOT_DESKTOP(0))
57 #define OPT_STR "R" IF_DESKTOP("vcf")
60 * chmod never changes the permissions of symbolic links; the chmod
61 * system call cannot change their permissions. This is not a problem
62 * since the permissions of symbolic links are never used.
63 * However, for each symbolic link listed on the command line, chmod changes
64 * the permissions of the pointed-to file. In contrast, chmod ignores
65 * symbolic links encountered during recursive directory traversals.
68 static int FAST_FUNC fileAction(const char *fileName, struct stat *statbuf, void* param, int depth)
72 /* match coreutils behavior */
74 /* statbuf holds lstat result, but we need stat (follow link) */
75 if (stat(fileName, statbuf))
77 } else { /* depth > 0: skip links */
78 if (S_ISLNK(statbuf->st_mode))
82 newmode = bb_parse_mode((char *)param, statbuf->st_mode);
83 if (newmode == (mode_t)-1)
84 bb_error_msg_and_die("invalid mode '%s'", (char *)param);
86 if (chmod(fileName, newmode) == 0) {
88 || (OPT_CHANGED && statbuf->st_mode != newmode)
90 printf("mode of '%s' changed to %04o (%s)\n", fileName,
91 newmode & 07777, bb_mode_string(newmode)+1);
97 bb_simple_perror_msg(fileName);
101 int chmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
102 int chmod_main(int argc UNUSED_PARAM, char **argv)
104 int retval = EXIT_SUCCESS;
108 /* Convert first encountered -r into ar, -w into aw etc
109 * so that getopt would not eat it */
111 while ((arg = *++argp)) {
112 /* Mode spec must be the first arg (sans -R etc) */
113 /* (protect against mishandling e.g. "chmod 644 -r") */
118 /* An option. Not a -- or valid option? */
119 if (arg[1] && !strchr("-"OPT_STR, arg[1])) {
126 getopt32(argv, "^" OPT_STR "\0" "-2");
129 /* Restore option-like mode if needed */
130 if (arg) arg[0] = '-';
132 /* Ok, ready to do the deed now */
135 if (!recursive_action(*argv,
136 OPT_RECURSE, // recurse
137 fileAction, // file action
138 fileAction, // dir action
142 retval = EXIT_FAILURE;
150 Security: chmod is too important and too subtle.
151 This is a test script (busybox chmod versus coreutils).
152 Run it in empty directory.
155 t1="/tmp/busybox chmod"
173 (cd test1; ls -lR) >out1
174 (cd test2; ls -lR) >out2
175 echo "chmod $1" >out.diff
176 if ! diff -u out1 out2 >>out.diff; then exit 1; fi
179 echo "If script produced 'out.diff' file, then at least one testcase failed"
180 create test1; create test2
187 tst "-R a+w linkfile"
189 tst "a-r,a+x linkfile"