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 | * |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 | * General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program; if not, write to the Free Software |
| 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 23 | * |
| 24 | */ |
| 25 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 26 | /* BB_AUDIT SUSv3 compliant */ |
| 27 | /* BB_AUDIT GNU defects - unsupported options -c, -f, -v, and long options. */ |
| 28 | /* http://www.opengroup.org/onlinepubs/007904975/utilities/chmod.html */ |
| 29 | |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 30 | #include <stdio.h> |
| 31 | #include <stdlib.h> |
| 32 | #include <string.h> |
| 33 | #include <unistd.h> |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 34 | #include <sys/stat.h> |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 35 | #include "busybox.h" |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 36 | |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 37 | static int fileAction(const char *fileName, struct stat *statbuf, void* junk) |
| 38 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 39 | if (!bb_parse_mode((char *)junk, &(statbuf->st_mode))) |
Manuel Novoa III | ea4c434 | 2003-03-19 18:09:03 +0000 | [diff] [blame] | 40 | bb_error_msg_and_die( "invalid mode: %s", (char *)junk); |
Matt Kraai | 6aabfd5 | 2001-04-26 18:55:29 +0000 | [diff] [blame] | 41 | if (chmod(fileName, statbuf->st_mode) == 0) |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 42 | return (TRUE); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 43 | bb_perror_msg("%s", fileName); /* Avoid multibyte problems. */ |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 44 | return (FALSE); |
| 45 | } |
| 46 | |
| 47 | int chmod_main(int argc, char **argv) |
| 48 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 49 | int retval = EXIT_SUCCESS; |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 50 | int recursiveFlag = FALSE; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 51 | int count; |
Eric Andersen | d274b53 | 2002-10-10 03:47:01 +0000 | [diff] [blame] | 52 | char *smode; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 53 | char **p; |
| 54 | char *p0; |
| 55 | char opt = '-'; |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 56 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 57 | ++argv; |
| 58 | count = 0; |
Eric Andersen | d274b53 | 2002-10-10 03:47:01 +0000 | [diff] [blame] | 59 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 60 | for (p = argv ; *p ; p++) { |
| 61 | p0 = p[0]; |
| 62 | if (p0[0] == opt) { |
| 63 | if ((p0[1] == '-') && !p0[2]) { |
| 64 | opt = 0; /* Disable further option processing. */ |
| 65 | continue; |
| 66 | } |
| 67 | if (p0[1] == 'R') { |
| 68 | char *s = p0 + 2; |
| 69 | while (*s == 'R') { |
| 70 | ++s; |
| 71 | } |
| 72 | if (*s) { |
| 73 | bb_show_usage(); |
| 74 | } |
| 75 | recursiveFlag = TRUE; |
| 76 | continue; |
| 77 | } |
| 78 | if (count) { |
| 79 | bb_show_usage(); |
| 80 | } |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 81 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 82 | argv[count] = p0; |
| 83 | ++count; |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 86 | argv[count] = NULL; |
Eric Andersen | d274b53 | 2002-10-10 03:47:01 +0000 | [diff] [blame] | 87 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 88 | if (count < 2) { |
| 89 | bb_show_usage(); |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 92 | smode = *argv; |
| 93 | ++argv; |
| 94 | |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 95 | /* Ok, ready to do the deed now */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 96 | do { |
Rob Landley | a75c180 | 2005-12-12 06:58:53 +0000 | [diff] [blame] | 97 | if (! recursive_action (*argv, recursiveFlag, TRUE, FALSE, |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 98 | fileAction, fileAction, smode)) { |
| 99 | retval = EXIT_FAILURE; |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 100 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 101 | } while (*++argv); |
| 102 | |
| 103 | return retval; |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | /* |
| 107 | Local Variables: |
| 108 | c-file-style: "linux" |
| 109 | c-basic-offset: 4 |
| 110 | tab-width: 4 |
| 111 | End: |
| 112 | */ |