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