blob: a9758d58be78d917f771074fa40f54654a7f3c69 [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
Manuel Novoa III cad53642003-03-19 09:13:01 +000027/* 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
Eric Andersen9f0fedb2001-04-24 18:07:19 +000031#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34#include <unistd.h>
Manuel Novoa III cad53642003-03-19 09:13:01 +000035#include <sys/stat.h>
Eric Andersen9f0fedb2001-04-24 18:07:19 +000036#include "busybox.h"
Eric Andersen9f0fedb2001-04-24 18:07:19 +000037
Eric Andersen9f0fedb2001-04-24 18:07:19 +000038static int fileAction(const char *fileName, struct stat *statbuf, void* junk)
39{
Manuel Novoa III cad53642003-03-19 09:13:01 +000040 if (!bb_parse_mode((char *)junk, &(statbuf->st_mode)))
Manuel Novoa III ea4c4342003-03-19 18:09:03 +000041 bb_error_msg_and_die( "invalid mode: %s", (char *)junk);
Matt Kraai6aabfd52001-04-26 18:55:29 +000042 if (chmod(fileName, statbuf->st_mode) == 0)
Eric Andersen9f0fedb2001-04-24 18:07:19 +000043 return (TRUE);
Manuel Novoa III cad53642003-03-19 09:13:01 +000044 bb_perror_msg("%s", fileName); /* Avoid multibyte problems. */
Eric Andersen9f0fedb2001-04-24 18:07:19 +000045 return (FALSE);
46}
47
48int chmod_main(int argc, char **argv)
49{
Manuel Novoa III cad53642003-03-19 09:13:01 +000050 int retval = EXIT_SUCCESS;
Eric Andersen9f0fedb2001-04-24 18:07:19 +000051 int recursiveFlag = FALSE;
Manuel Novoa III cad53642003-03-19 09:13:01 +000052 int count;
Eric Andersend274b532002-10-10 03:47:01 +000053 char *smode;
Manuel Novoa III cad53642003-03-19 09:13:01 +000054 char **p;
55 char *p0;
56 char opt = '-';
Eric Andersen9f0fedb2001-04-24 18:07:19 +000057
Manuel Novoa III cad53642003-03-19 09:13:01 +000058 ++argv;
59 count = 0;
Eric Andersend274b532002-10-10 03:47:01 +000060
Manuel Novoa III cad53642003-03-19 09:13:01 +000061 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 }
Eric Andersen9f0fedb2001-04-24 18:07:19 +000082 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000083 argv[count] = p0;
84 ++count;
Eric Andersen9f0fedb2001-04-24 18:07:19 +000085 }
86
Manuel Novoa III cad53642003-03-19 09:13:01 +000087 argv[count] = NULL;
Eric Andersend274b532002-10-10 03:47:01 +000088
Manuel Novoa III cad53642003-03-19 09:13:01 +000089 if (count < 2) {
90 bb_show_usage();
Eric Andersen9f0fedb2001-04-24 18:07:19 +000091 }
92
Manuel Novoa III cad53642003-03-19 09:13:01 +000093 smode = *argv;
94 ++argv;
95
Eric Andersen9f0fedb2001-04-24 18:07:19 +000096 /* Ok, ready to do the deed now */
Manuel Novoa III cad53642003-03-19 09:13:01 +000097 do {
98 if (! recursive_action (*argv, recursiveFlag, FALSE, FALSE,
99 fileAction, fileAction, smode)) {
100 retval = EXIT_FAILURE;
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000101 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000102 } while (*++argv);
103
104 return retval;
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000105}
106
107/*
108Local Variables:
109c-file-style: "linux"
110c-basic-offset: 4
111tab-width: 4
112End:
113*/