Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 3 | * Mini chmod implementation for busybox |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 4 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 6 | * |
Eric Andersen | d274b53 | 2002-10-10 03:47:01 +0000 | [diff] [blame] | 7 | * Reworked by (C) 2002 Vladimir Oleynik <dzo@simtreas.ru> |
| 8 | * to correctly parse '-rwxgoa' |
| 9 | * |
Bernhard Reutner-Fischer | 20f4000 | 2006-01-30 17:17:14 +0000 | [diff] [blame] | 10 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 11 | */ |
| 12 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 13 | /* BB_AUDIT SUSv3 compliant */ |
Denis Vlasenko | fefb279 | 2006-10-27 15:13:54 +0000 | [diff] [blame] | 14 | /* BB_AUDIT GNU defects - unsupported long options. */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 15 | /* http://www.opengroup.org/onlinepubs/007904975/utilities/chmod.html */ |
| 16 | |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 17 | #include "busybox.h" |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 18 | |
Denis Vlasenko | fefb279 | 2006-10-27 15:13:54 +0000 | [diff] [blame] | 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 | |
Denis Vlasenko | 51b4c92 | 2006-10-27 16:07:20 +0000 | [diff] [blame^] | 25 | /* TODO: |
| 26 | * chmod never changes the permissions of symbolic links; the chmod |
| 27 | * system call cannot change their permissions. This is not a problem |
| 28 | * since the permissions of symbolic links are never used. |
| 29 | * However, for each symbolic link listed on the command line, chmod changes |
| 30 | * the permissions of the pointed-to file. In contrast, chmod ignores |
| 31 | * symbolic links encountered during recursive directory traversals. |
| 32 | */ |
| 33 | |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 34 | static int fileAction(const char *fileName, struct stat *statbuf, void* junk) |
| 35 | { |
Denis Vlasenko | fefb279 | 2006-10-27 15:13:54 +0000 | [diff] [blame] | 36 | mode_t newmode = statbuf->st_mode; |
Denis Vlasenko | 51b4c92 | 2006-10-27 16:07:20 +0000 | [diff] [blame^] | 37 | |
| 38 | // TODO: match GNU behavior: |
| 39 | // if (depth > 0 && S_ISLNK(statbuf->st_mode)) return TRUE; |
| 40 | // if (depth == 0) follow link |
| 41 | |
Denis Vlasenko | fefb279 | 2006-10-27 15:13:54 +0000 | [diff] [blame] | 42 | if (!bb_parse_mode((char *)junk, &newmode)) |
| 43 | bb_error_msg_and_die("invalid mode: %s", (char *)junk); |
| 44 | |
| 45 | if (chmod(fileName, statbuf->st_mode) == 0) { |
| 46 | if (OPT_VERBOSE /* -v verbose? or -c changed? */ |
| 47 | || (OPT_CHANGED && statbuf->st_mode != newmode) |
| 48 | ) { |
| 49 | printf("mode of '%s' changed to %04o (%s)\n", fileName, |
Denis Vlasenko | 51b4c92 | 2006-10-27 16:07:20 +0000 | [diff] [blame^] | 50 | newmode & 07777, bb_mode_string(newmode)+1); |
Denis Vlasenko | fefb279 | 2006-10-27 15:13:54 +0000 | [diff] [blame] | 51 | } |
| 52 | return TRUE; |
| 53 | } |
| 54 | if (!OPT_QUIET) /* not silent (-f)? */ |
| 55 | bb_perror_msg("%s", fileName); |
| 56 | return FALSE; |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Denis Vlasenko | fefb279 | 2006-10-27 15:13:54 +0000 | [diff] [blame] | 59 | int chmod_main(int argc, char **argv) |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 60 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 61 | int retval = EXIT_SUCCESS; |
Denis Vlasenko | fefb279 | 2006-10-27 15:13:54 +0000 | [diff] [blame] | 62 | char *arg, **argp; |
Eric Andersen | d274b53 | 2002-10-10 03:47:01 +0000 | [diff] [blame] | 63 | char *smode; |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 64 | |
Denis Vlasenko | 51b4c92 | 2006-10-27 16:07:20 +0000 | [diff] [blame^] | 65 | /* Convert first encountered -r into a-r, -w into a-w etc */ |
Denis Vlasenko | fefb279 | 2006-10-27 15:13:54 +0000 | [diff] [blame] | 66 | argp = argv + 1; |
| 67 | while ((arg = *argp)) { |
Denis Vlasenko | 51b4c92 | 2006-10-27 16:07:20 +0000 | [diff] [blame^] | 68 | /* Mode spec must be the first arg (sans -R etc) */ |
| 69 | /* (protect against mishandling e.g. "chmod 644 -r") */ |
Denis Vlasenko | fefb279 | 2006-10-27 15:13:54 +0000 | [diff] [blame] | 70 | if (arg[0] != '-') |
| 71 | break; |
| 72 | /* An option. Not a -- or valid option? */ |
| 73 | if (arg[1] && !strchr(OPT_STR, arg[1])) { |
| 74 | argp[0] = xasprintf("a%s", arg); |
| 75 | break; |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 76 | } |
Denis Vlasenko | fefb279 | 2006-10-27 15:13:54 +0000 | [diff] [blame] | 77 | argp++; |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 78 | } |
Denis Vlasenko | fefb279 | 2006-10-27 15:13:54 +0000 | [diff] [blame] | 79 | /* "chmod -rzzz abc" will say "invalid mode: a-rzzz"! |
| 80 | * It is easily fixable, but deemed not worth the code */ |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 81 | |
Denis Vlasenko | fefb279 | 2006-10-27 15:13:54 +0000 | [diff] [blame] | 82 | opt_complementary = "-2"; |
| 83 | getopt32(argc, argv, OPT_STR + 1); /* Reuse string */ |
| 84 | argv += optind; |
Eric Andersen | d274b53 | 2002-10-10 03:47:01 +0000 | [diff] [blame] | 85 | |
Denis Vlasenko | fefb279 | 2006-10-27 15:13:54 +0000 | [diff] [blame] | 86 | smode = *argv++; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 87 | |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 88 | /* Ok, ready to do the deed now */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 89 | do { |
Denis Vlasenko | 51b4c92 | 2006-10-27 16:07:20 +0000 | [diff] [blame^] | 90 | if (!recursive_action(*argv, |
| 91 | OPT_RECURSE, // recurse |
| 92 | FALSE, // follow links: GNU doesn't |
| 93 | FALSE, // depth first |
| 94 | fileAction, // file action |
| 95 | fileAction, // dir action |
| 96 | smode) // user data |
| 97 | ) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 98 | retval = EXIT_FAILURE; |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 99 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 100 | } while (*++argv); |
| 101 | |
| 102 | return retval; |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 103 | } |