Restrict octal perms to <= 07777. Cosmetic error message change.
[oweals/busybox.git] / coreutils / chmod.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini chmod implementation for busybox
4  *
5  * Copyright (C) 1999,2000 by Lineo, inc. and Erik Andersen
6  * Copyright (C) 1999,2000,2001 by Erik Andersen <andersee@debian.org>
7  *
8  * Reworked by (C) 2002 Vladimir Oleynik <dzo@simtreas.ru>
9  *  to correctly parse '-rwxgoa'
10  *
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 /* 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
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <sys/stat.h>
36 #include "busybox.h"
37
38 static int fileAction(const char *fileName, struct stat *statbuf, void* junk)
39 {
40         if (!bb_parse_mode((char *)junk, &(statbuf->st_mode)))
41                 bb_error_msg_and_die( "invalid mode: %s", (char *)junk);
42         if (chmod(fileName, statbuf->st_mode) == 0)
43                 return (TRUE);
44         bb_perror_msg("%s", fileName);  /* Avoid multibyte problems. */
45         return (FALSE);
46 }
47
48 int chmod_main(int argc, char **argv)
49 {
50         int retval = EXIT_SUCCESS;
51         int recursiveFlag = FALSE;
52         int count;
53         char *smode;
54         char **p;
55         char *p0;
56         char opt = '-';
57
58         ++argv;
59         count = 0;
60
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                         }
82                 }
83                 argv[count] = p0;
84                 ++count;
85         }
86
87         argv[count] = NULL;
88
89         if (count < 2) {
90                 bb_show_usage();
91         }
92
93         smode = *argv;
94         ++argv;
95
96         /* Ok, ready to do the deed now */
97         do {
98                 if (! recursive_action (*argv, recursiveFlag, FALSE, FALSE,
99                                                                 fileAction,     fileAction, smode)) {
100                         retval = EXIT_FAILURE;
101                 }
102         } while (*++argv);
103
104         return retval;
105 }
106
107 /*
108 Local Variables:
109 c-file-style: "linux"
110 c-basic-offset: 4
111 tab-width: 4
112 End:
113 */