Patch for the debian-cvs package, ip* applets in /bin, patch from Bastian Blank
[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 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <getopt.h>
32 #include "busybox.h"
33
34 static int fileAction(const char *fileName, struct stat *statbuf, void* junk)
35 {
36         if (!parse_mode((char *)junk, &(statbuf->st_mode)))
37                 error_msg_and_die( "unknown mode: %s", (char *)junk);
38         if (chmod(fileName, statbuf->st_mode) == 0)
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;
48         int modeind = 0;   /* Index of the mode argument in `argv'. */
49         char *smode;
50         static const char chmod_modes[] = "Rrwxstugoa,+-=";
51
52         /* do normal option parsing */
53         while (1) {
54                 int thisind = optind ? optind : 1;
55
56                 opt = getopt(argc, argv, chmod_modes);
57                 if (opt == EOF)
58                                 break;
59                 smode = strchr(chmod_modes, opt);
60                 if(smode == NULL)
61                                 show_usage();
62                 if(smode == chmod_modes) {      /* 'R' */
63                         recursiveFlag = TRUE;
64                 } else {
65                       if (modeind != 0 && modeind != thisind)
66                         show_usage();
67                       modeind = thisind;
68                 }
69         }
70
71         if (modeind == 0)
72                 modeind = optind++;
73
74         opt = optind;
75         if (opt >= argc) {
76                 error_msg_and_die(too_few_args);
77         }
78
79         smode = argv[modeind];
80         /* Ok, ready to do the deed now */
81         for (; opt < argc; opt++) {
82                 if (! recursive_action (argv[opt], recursiveFlag, FALSE, FALSE, fileAction,
83                                         fileAction, smode)) {
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 */