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