#define OPT_QUIET (USE_DESKTOP(option_mask32 & 8) SKIP_DESKTOP(0))
#define OPT_STR ("-R" USE_DESKTOP("vcf"))
+/* TODO:
+ * chmod never changes the permissions of symbolic links; the chmod
+ * system call cannot change their permissions. This is not a problem
+ * since the permissions of symbolic links are never used.
+ * However, for each symbolic link listed on the command line, chmod changes
+ * the permissions of the pointed-to file. In contrast, chmod ignores
+ * symbolic links encountered during recursive directory traversals.
+ */
+
static int fileAction(const char *fileName, struct stat *statbuf, void* junk)
{
mode_t newmode = statbuf->st_mode;
+
+ // TODO: match GNU behavior:
+ // if (depth > 0 && S_ISLNK(statbuf->st_mode)) return TRUE;
+ // if (depth == 0) follow link
+
if (!bb_parse_mode((char *)junk, &newmode))
bb_error_msg_and_die("invalid mode: %s", (char *)junk);
|| (OPT_CHANGED && statbuf->st_mode != newmode)
) {
printf("mode of '%s' changed to %04o (%s)\n", fileName,
- newmode & 7777, bb_mode_string(newmode)+1);
+ newmode & 07777, bb_mode_string(newmode)+1);
}
return TRUE;
}
char *arg, **argp;
char *smode;
- /* Convert first encountered -r into a-r, etc */
+ /* Convert first encountered -r into a-r, -w into a-w etc */
argp = argv + 1;
while ((arg = *argp)) {
- /* Protect against mishandling e.g. "chmod 644 -r" */
+ /* Mode spec must be the first arg (sans -R etc) */
+ /* (protect against mishandling e.g. "chmod 644 -r") */
if (arg[0] != '-')
break;
/* An option. Not a -- or valid option? */
/* Ok, ready to do the deed now */
do {
- if (!recursive_action(*argv, OPT_RECURSE, TRUE, FALSE,
- fileAction, fileAction, smode)) {
+ if (!recursive_action(*argv,
+ OPT_RECURSE, // recurse
+ FALSE, // follow links: GNU doesn't
+ FALSE, // depth first
+ fileAction, // file action
+ fileAction, // dir action
+ smode) // user data
+ ) {
retval = EXIT_FAILURE;
}
} while (*++argv);
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
*/
-/* BB_AUDIT SUSv3 defects - unsupported options -h, -H, -L, and -P. */
-/* BB_AUDIT GNU defects - unsupported options -h, -c, -f, -v, and long options. */
-/* BB_AUDIT Note: gnu chown does not support -H, -L, or -P. */
+/* BB_AUDIT SUSv3 defects - unsupported options -H, -L, and -P. */
+/* BB_AUDIT GNU defects - unsupported long options. */
/* http://www.opengroup.org/onlinepubs/007904975/utilities/chown.html */
-#include <stdlib.h>
-#include <unistd.h>
-#include <string.h>
#include "busybox.h"
static uid_t uid = -1;
static int (*chown_func)(const char *, uid_t, gid_t) = chown;
+#define OPT_RECURSE (option_mask32 & 1)
+#define OPT_NODEREF (option_mask32 & 2)
+#define OPT_VERBOSE (USE_DESKTOP(option_mask32 & 4) SKIP_DESKTOP(0))
+#define OPT_CHANGED (USE_DESKTOP(option_mask32 & 8) SKIP_DESKTOP(0))
+#define OPT_QUIET (USE_DESKTOP(option_mask32 & 0x10) SKIP_DESKTOP(0))
+#define OPT_STR ("Rh" USE_DESKTOP("vcf"))
+
+/* TODO:
+ * -H if a command line argument is a symbolic link to a directory, traverse it
+ * -L traverse every symbolic link to a directory encountered
+ * -P do not traverse any symbolic links (default)
+ */
+
static int fileAction(const char *fileName, struct stat *statbuf,
void ATTRIBUTE_UNUSED *junk)
{
+ // TODO: -H/-L/-P
+ // if (depth ... && S_ISLNK(statbuf->st_mode)) ....
+
if (!chown_func(fileName,
(uid == (uid_t)-1) ? statbuf->st_uid : uid,
(gid == (gid_t)-1) ? statbuf->st_gid : gid)) {
+ if (OPT_VERBOSE
+ || (OPT_CHANGED && (statbuf->st_uid != uid || statbuf->st_gid != gid))
+ ) {
+ printf("changed ownership of '%s' to %u:%u\n", fileName, uid, gid);
+ }
return TRUE;
}
- bb_perror_msg("%s", fileName); /* A filename can have % in it... */
+ if (!OPT_QUIET)
+ bb_perror_msg("%s", fileName); /* A filename can have % in it... */
return FALSE;
}
-#define FLAG_R 1
-#define FLAG_h 2
-
int chown_main(int argc, char **argv)
{
- int flags;
int retval = EXIT_SUCCESS;
char *groupName;
- flags = getopt32(argc, argv, "Rh");
+ opt_complementary = "-2";
+ getopt32(argc, argv, OPT_STR);
- if (flags & FLAG_h) chown_func = lchown;
-
- if (argc - optind < 2) {
- bb_show_usage();
- }
+ if (OPT_NODEREF) chown_func = lchown;
argv += optind;
/* First, check if there is a group name here */
- if ((groupName = strchr(*argv, '.')) == NULL) {
+ groupName = strchr(*argv, '.');
+ if (!groupName) {
groupName = strchr(*argv, ':');
}
/* Ok, ready to do the deed now */
do {
- if (! recursive_action (*argv, (flags & FLAG_R), FALSE, FALSE,
- fileAction, fileAction, NULL)) {
+ if (!recursive_action(*argv,
+ OPT_RECURSE, // recurse
+ FALSE, // follow links: TODO: -H/-L/-P
+ FALSE, // depth first
+ fileAction, // file action
+ fileAction, // dir action
+ NULL) // user data
+ ) {
retval = EXIT_FAILURE;
}
} while (*++argv);
"-r--r--r-- 1 root root 0 Apr 12 18:25 /tmp/foo\n"
#define chown_trivial_usage \
- "[ -Rh ]... OWNER[<.|:>[GROUP]] FILE..."
+ "[-Rh"USE_DESKTOP("cvf")"]... OWNER[<.|:>[GROUP]] FILE..."
#define chown_full_usage \
"Change the owner and/or group of each FILE to OWNER and/or GROUP.\n" \
"\nOptions:\n" \
"\t-R\tChanges files and directories recursively\n" \
- "\t-h\tDo not dereference symbolic links"
+ "\t-h\tDo not dereference symbolic links" \
+ USE_DESKTOP( \
+ "\n\t-c\tList changed files" \
+ "\n\t-v\tList all files" \
+ "\n\t-f\tHide errors" \
+ )
#define chown_example_usage \
"$ ls -l /tmp/foo\n" \
"-r--r--r-- 1 andersen andersen 0 Apr 12 18:25 /tmp/foo\n" \