aboutsummaryrefslogtreecommitdiff
path: root/coreutils/chown.c
diff options
context:
space:
mode:
Diffstat (limited to 'coreutils/chown.c')
-rw-r--r--coreutils/chown.c58
1 files changed, 38 insertions, 20 deletions
diff --git a/coreutils/chown.c b/coreutils/chown.c
index 42f973f32..bef89ce86 100644
--- a/coreutils/chown.c
+++ b/coreutils/chown.c
@@ -7,14 +7,10 @@
* 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;
@@ -22,39 +18,55 @@ static gid_t gid = -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, ':');
}
@@ -68,8 +80,14 @@ int chown_main(int argc, char **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);