blob: ba80e020a8a8445cba1f8004f286aa9bdd615bf2 [file] [log] [blame]
Eric Andersen9f0fedb2001-04-24 18:07:19 +00001/* vi: set sw=4 ts=4: */
2/*
Eric Andersenbdfd0d72001-10-24 05:00:29 +00003 * Mini chmod implementation for busybox
Eric Andersen9f0fedb2001-04-24 18:07:19 +00004 *
Eric Andersenbdfd0d72001-10-24 05:00:29 +00005 * Copyright (C) 1999,2000 by Lineo, inc. and Erik Andersen
6 * Copyright (C) 1999,2000,2001 by Erik Andersen <andersee@debian.org>
Eric Andersen9f0fedb2001-04-24 18:07:19 +00007 *
Eric Andersend274b532002-10-10 03:47:01 +00008 * Reworked by (C) 2002 Vladimir Oleynik <dzo@simtreas.ru>
9 * to correctly parse '-rwxgoa'
10 *
Eric Andersen9f0fedb2001-04-24 18:07:19 +000011 * 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 Andersen9f0fedb2001-04-24 18:07:19 +000033
Eric Andersen9f0fedb2001-04-24 18:07:19 +000034static int fileAction(const char *fileName, struct stat *statbuf, void* junk)
35{
Matt Kraai6aabfd52001-04-26 18:55:29 +000036 if (!parse_mode((char *)junk, &(statbuf->st_mode)))
Eric Andersend274b532002-10-10 03:47:01 +000037 error_msg_and_die( "unknown mode: %s", (char *)junk);
Matt Kraai6aabfd52001-04-26 18:55:29 +000038 if (chmod(fileName, statbuf->st_mode) == 0)
Eric Andersen9f0fedb2001-04-24 18:07:19 +000039 return (TRUE);
40 perror(fileName);
41 return (FALSE);
42}
43
44int chmod_main(int argc, char **argv)
45{
46 int opt;
47 int recursiveFlag = FALSE;
Eric Andersend274b532002-10-10 03:47:01 +000048 int modeind = 0; /* Index of the mode argument in `argv'. */
49 char *smode;
50 static const char chmod_modes[] = "Rrwxstugoa,+-=";
Eric Andersen9f0fedb2001-04-24 18:07:19 +000051
52 /* do normal option parsing */
Eric Andersend274b532002-10-10 03:47:01 +000053 while (1) {
54 int thisind = optind ? optind : 1;
55
56 opt = getopt(argc, argv, chmod_modes);
57 if (opt == EOF)
Matt Kraai468f5042001-04-26 18:27:47 +000058 break;
Eric Andersend274b532002-10-10 03:47:01 +000059 smode = strchr(chmod_modes, opt);
60 if(smode == NULL)
Eric Andersen9f0fedb2001-04-24 18:07:19 +000061 show_usage();
Eric Andersend274b532002-10-10 03:47:01 +000062 if(smode == chmod_modes) { /* 'R' */
63 recursiveFlag = TRUE;
64 } else {
65 if (modeind != 0 && modeind != thisind)
66 show_usage();
67 modeind = thisind;
Eric Andersen9f0fedb2001-04-24 18:07:19 +000068 }
69 }
70
Eric Andersend274b532002-10-10 03:47:01 +000071 if (modeind == 0)
72 modeind = optind++;
73
74 opt = optind;
75 if (opt >= argc) {
Eric Andersen9f0fedb2001-04-24 18:07:19 +000076 error_msg_and_die(too_few_args);
77 }
78
Eric Andersend274b532002-10-10 03:47:01 +000079 smode = argv[modeind];
Eric Andersen9f0fedb2001-04-24 18:07:19 +000080 /* Ok, ready to do the deed now */
Eric Andersend274b532002-10-10 03:47:01 +000081 for (; opt < argc; opt++) {
82 if (! recursive_action (argv[opt], recursiveFlag, FALSE, FALSE, fileAction,
83 fileAction, smode)) {
Eric Andersen9f0fedb2001-04-24 18:07:19 +000084 return EXIT_FAILURE;
85 }
86 }
87 return EXIT_SUCCESS;
88}
89
90/*
91Local Variables:
92c-file-style: "linux"
93c-basic-offset: 4
94tab-width: 4
95End:
96*/