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 | |
| 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
| 30 | #include <unistd.h> |
| 31 | #include <getopt.h> |
| 32 | #include "busybox.h" |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 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 | { |
Matt Kraai | 6aabfd5 | 2001-04-26 18:55:29 +0000 | [diff] [blame] | 36 | if (!parse_mode((char *)junk, &(statbuf->st_mode))) |
Eric Andersen | d274b53 | 2002-10-10 03:47:01 +0000 | [diff] [blame^] | 37 | error_msg_and_die( "unknown mode: %s", (char *)junk); |
Matt Kraai | 6aabfd5 | 2001-04-26 18:55:29 +0000 | [diff] [blame] | 38 | if (chmod(fileName, statbuf->st_mode) == 0) |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 39 | return (TRUE); |
| 40 | perror(fileName); |
| 41 | return (FALSE); |
| 42 | } |
| 43 | |
| 44 | int chmod_main(int argc, char **argv) |
| 45 | { |
| 46 | int opt; |
| 47 | int recursiveFlag = FALSE; |
Eric Andersen | d274b53 | 2002-10-10 03:47:01 +0000 | [diff] [blame^] | 48 | int modeind = 0; /* Index of the mode argument in `argv'. */ |
| 49 | char *smode; |
| 50 | static const char chmod_modes[] = "Rrwxstugoa,+-="; |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 51 | |
| 52 | /* do normal option parsing */ |
Eric Andersen | d274b53 | 2002-10-10 03:47:01 +0000 | [diff] [blame^] | 53 | while (1) { |
| 54 | int thisind = optind ? optind : 1; |
| 55 | |
| 56 | opt = getopt(argc, argv, chmod_modes); |
| 57 | if (opt == EOF) |
Matt Kraai | 468f504 | 2001-04-26 18:27:47 +0000 | [diff] [blame] | 58 | break; |
Eric Andersen | d274b53 | 2002-10-10 03:47:01 +0000 | [diff] [blame^] | 59 | smode = strchr(chmod_modes, opt); |
| 60 | if(smode == NULL) |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 61 | show_usage(); |
Eric Andersen | d274b53 | 2002-10-10 03:47:01 +0000 | [diff] [blame^] | 62 | if(smode == chmod_modes) { /* 'R' */ |
| 63 | recursiveFlag = TRUE; |
| 64 | } else { |
| 65 | if (modeind != 0 && modeind != thisind) |
| 66 | show_usage(); |
| 67 | modeind = thisind; |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | |
Eric Andersen | d274b53 | 2002-10-10 03:47:01 +0000 | [diff] [blame^] | 71 | if (modeind == 0) |
| 72 | modeind = optind++; |
| 73 | |
| 74 | opt = optind; |
| 75 | if (opt >= argc) { |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 76 | error_msg_and_die(too_few_args); |
| 77 | } |
| 78 | |
Eric Andersen | d274b53 | 2002-10-10 03:47:01 +0000 | [diff] [blame^] | 79 | smode = argv[modeind]; |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 80 | /* Ok, ready to do the deed now */ |
Eric Andersen | d274b53 | 2002-10-10 03:47:01 +0000 | [diff] [blame^] | 81 | for (; opt < argc; opt++) { |
| 82 | if (! recursive_action (argv[opt], recursiveFlag, FALSE, FALSE, fileAction, |
| 83 | fileAction, smode)) { |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 84 | return EXIT_FAILURE; |
| 85 | } |
| 86 | } |
| 87 | return EXIT_SUCCESS; |
| 88 | } |
| 89 | |
| 90 | /* |
| 91 | Local Variables: |
| 92 | c-file-style: "linux" |
| 93 | c-basic-offset: 4 |
| 94 | tab-width: 4 |
| 95 | End: |
| 96 | */ |