blob: 390cc6d2c7b36b8d8e338677d510afaeeab95710 [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 Andersencb81e642003-07-14 21:21:08 +00005 * Copyright (C) 1999-2003 by Erik Andersen <andersen@codepoet.org>
Eric Andersen9f0fedb2001-04-24 18:07:19 +00006 *
Eric Andersend274b532002-10-10 03:47:01 +00007 * Reworked by (C) 2002 Vladimir Oleynik <dzo@simtreas.ru>
8 * to correctly parse '-rwxgoa'
9 *
Eric Andersen9f0fedb2001-04-24 18:07:19 +000010 * 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
Manuel Novoa III cad53642003-03-19 09:13:01 +000026/* 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
Eric Andersen9f0fedb2001-04-24 18:07:19 +000030#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <unistd.h>
Manuel Novoa III cad53642003-03-19 09:13:01 +000034#include <sys/stat.h>
Eric Andersen9f0fedb2001-04-24 18:07:19 +000035#include "busybox.h"
Eric Andersen9f0fedb2001-04-24 18:07:19 +000036
Eric Andersen9f0fedb2001-04-24 18:07:19 +000037static int fileAction(const char *fileName, struct stat *statbuf, void* junk)
38{
Manuel Novoa III cad53642003-03-19 09:13:01 +000039 if (!bb_parse_mode((char *)junk, &(statbuf->st_mode)))
Manuel Novoa III ea4c4342003-03-19 18:09:03 +000040 bb_error_msg_and_die( "invalid mode: %s", (char *)junk);
Matt Kraai6aabfd52001-04-26 18:55:29 +000041 if (chmod(fileName, statbuf->st_mode) == 0)
Eric Andersen9f0fedb2001-04-24 18:07:19 +000042 return (TRUE);
Manuel Novoa III cad53642003-03-19 09:13:01 +000043 bb_perror_msg("%s", fileName); /* Avoid multibyte problems. */
Eric Andersen9f0fedb2001-04-24 18:07:19 +000044 return (FALSE);
45}
46
47int chmod_main(int argc, char **argv)
48{
Manuel Novoa III cad53642003-03-19 09:13:01 +000049 int retval = EXIT_SUCCESS;
Eric Andersen9f0fedb2001-04-24 18:07:19 +000050 int recursiveFlag = FALSE;
Manuel Novoa III cad53642003-03-19 09:13:01 +000051 int count;
Eric Andersend274b532002-10-10 03:47:01 +000052 char *smode;
Manuel Novoa III cad53642003-03-19 09:13:01 +000053 char **p;
54 char *p0;
55 char opt = '-';
Eric Andersen9f0fedb2001-04-24 18:07:19 +000056
Manuel Novoa III cad53642003-03-19 09:13:01 +000057 ++argv;
58 count = 0;
Eric Andersend274b532002-10-10 03:47:01 +000059
Manuel Novoa III cad53642003-03-19 09:13:01 +000060 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 }
Eric Andersen9f0fedb2001-04-24 18:07:19 +000081 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000082 argv[count] = p0;
83 ++count;
Eric Andersen9f0fedb2001-04-24 18:07:19 +000084 }
85
Manuel Novoa III cad53642003-03-19 09:13:01 +000086 argv[count] = NULL;
Eric Andersend274b532002-10-10 03:47:01 +000087
Manuel Novoa III cad53642003-03-19 09:13:01 +000088 if (count < 2) {
89 bb_show_usage();
Eric Andersen9f0fedb2001-04-24 18:07:19 +000090 }
91
Manuel Novoa III cad53642003-03-19 09:13:01 +000092 smode = *argv;
93 ++argv;
94
Eric Andersen9f0fedb2001-04-24 18:07:19 +000095 /* Ok, ready to do the deed now */
Manuel Novoa III cad53642003-03-19 09:13:01 +000096 do {
97 if (! recursive_action (*argv, recursiveFlag, FALSE, FALSE,
98 fileAction, fileAction, smode)) {
99 retval = EXIT_FAILURE;
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000100 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000101 } while (*++argv);
102
103 return retval;
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000104}
105
106/*
107Local Variables:
108c-file-style: "linux"
109c-basic-offset: 4
110tab-width: 4
111End:
112*/