-#include <stdlib.h>
+/* vi: set sw=4 ts=4: */
+/*
+ * Mini chmod implementation for busybox
+ *
+ * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
+ *
+ * Reworked by (C) 2002 Vladimir Oleynik <dzo@simtreas.ru>
+ * to correctly parse '-rwxgoa'
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ */
+
+/* BB_AUDIT SUSv3 compliant */
+/* BB_AUDIT GNU defects - unsupported options -c, -f, -v, and long options. */
+/* http://www.opengroup.org/onlinepubs/007904975/utilities/chmod.html */
+
#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
-#include "internal.h"
+#include "busybox.h"
-const char chmod_usage[] = "chmod [-R] mode file [file ...]\n"
-"\nmode may be an octal integer representing the bit pattern for the\n"
-"\tnew mode, or a symbolic value matching the pattern\n"
-"\t[ugoa]{+|-|=}[rwxst] .\n"
-"\t\tu:\tUser\n"
-"\t\tg:\tGroup\n"
-"\t\to:\tOthers\n"
-"\t\ta:\tAll\n"
-"\n"
-"\n+:\tAdd privilege\n"
-"\n-:\tRemove privilege\n"
-"\n=:\tSet privilege\n"
-"\n"
-"\t\tr:\tRead\n"
-"\t\tw:\tWrite\n"
-"\t\tx:\tExecute\n"
-"\t\ts:\tSet User ID\n"
-"\t\tt:\t\"Sticky\" Text\n"
-"\n"
-"\tModes may be concatenated, as in \"u=rwx,g=rx,o=rx,-t,-s\n"
-"\n"
-"\t-R:\tRecursively change the mode of all files and directories\n"
-"\t\tunder the argument directory.";
+static int fileAction(const char *fileName, struct stat *statbuf, void* junk)
+{
+ if (!bb_parse_mode((char *)junk, &(statbuf->st_mode)))
+ bb_error_msg_and_die( "invalid mode: %s", (char *)junk);
+ if (chmod(fileName, statbuf->st_mode) == 0)
+ return (TRUE);
+ bb_perror_msg("%s", fileName); /* Avoid multibyte problems. */
+ return (FALSE);
+}
-int
-parse_mode(
- const char * s
-,mode_t * or
-,mode_t * and
-,int * group_execute)
+int chmod_main(int argc, char **argv)
{
- /* [ugoa]{+|-|=}[rwxstl] */
- mode_t mode = 0;
- mode_t groups = S_ISVTX;
- char type;
- char c;
+ int retval = EXIT_SUCCESS;
+ int recursiveFlag = FALSE;
+ int count;
+ char *smode;
+ char **p;
+ char *p0;
+ char opt = '-';
- do {
- for ( ; ; ) {
- switch ( c = *s++ ) {
- case '\0':
- return -1;
- case 'u':
- groups |= S_ISUID|S_IRWXU;
- continue;
- case 'g':
- groups |= S_ISGID|S_IRWXG;
- continue;
- case 'o':
- groups |= S_IRWXO;
- continue;
- case 'a':
- groups |= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
- continue;
- case '+':
- case '=':
- case '-':
- type = c;
- if ( groups == S_ISVTX ) /* The default is "all" */
- groups |= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
- break;
- default:
- if ( c >= '0' && c <= '7' && mode == 0 && groups == S_ISVTX ) {
- *and = 0;
- *or = strtol(--s, 0, 010);
- return 0;
- }
- else
- return -1;
- }
- break;
- }
+ ++argv;
+ count = 0;
- while ( (c = *s++) != '\0' ) {
- switch ( c ) {
- case ',':
- break;
- case 'r':
- mode |= S_IRUSR|S_IRGRP|S_IROTH;
- continue;
- case 'w':
- mode |= S_IWUSR|S_IWGRP|S_IWOTH;
+ for (p = argv ; *p ; p++) {
+ p0 = p[0];
+ if (p0[0] == opt) {
+ if ((p0[1] == '-') && !p0[2]) {
+ opt = 0; /* Disable further option processing. */
continue;
- case 'x':
- mode |= S_IXUSR|S_IXGRP|S_IXOTH;
- continue;
- case 's':
- if ( group_execute != 0 && (groups & S_IRWXG) ) {
- if ( *group_execute < 0 )
- return -1;
- if ( type != '-' ) {
- mode |= S_IXGRP;
- *group_execute = 1;
- }
+ }
+ if (p0[1] == 'R') {
+ char *s = p0 + 2;
+ while (*s == 'R') {
+ ++s;
}
- mode |= S_ISUID|S_ISGID;
- continue;
- case 'l':
- if ( *group_execute > 0 )
- return -1;
- if ( type != '-' ) {
- *and &= ~S_IXGRP;
- *group_execute = -1;
+ if (*s) {
+ bb_show_usage();
}
- mode |= S_ISGID;
- groups |= S_ISGID;
- continue;
- case 't':
- mode |= S_ISVTX;
+ recursiveFlag = TRUE;
continue;
- default:
- return -1;
}
- break;
- }
- switch ( type ) {
- case '=':
- *and &= ~(groups);
- /* fall through */
- case '+':
- *or |= mode & groups;
- break;
- case '-':
- *and &= ~(mode & groups);
- *or &= *and;
- break;
+ if (count) {
+ bb_show_usage();
+ }
}
- } while ( c == ',' );
- return 0;
-}
+ argv[count] = p0;
+ ++count;
+ }
-extern int
-chmod_main(struct FileInfo * i, int argc, char * * argv)
-{
- i->andWithMode = S_ISVTX|S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
- i->orWithMode = 0;
+ argv[count] = NULL;
- while ( argc >= 3 ) {
- if ( parse_mode(argv[1], &i->orWithMode, &i->andWithMode, 0)
- == 0 ) {
- argc--;
- argv++;
- }
- else if ( strcmp(argv[1], "-R") == 0 ) {
- i->recursive = 1;
- argc--;
- argv++;
- }
- else
- break;
+ if (count < 2) {
+ bb_show_usage();
}
- i->changeMode = 1;
- i->complainInPostProcess = 1;
+ smode = *argv;
+ ++argv;
- return monadic_main(i, argc, argv);
+ /* Ok, ready to do the deed now */
+ do {
+ if (! recursive_action (*argv, recursiveFlag, FALSE, FALSE,
+ fileAction, fileAction, smode)) {
+ retval = EXIT_FAILURE;
+ }
+ } while (*++argv);
+
+ return retval;
}
+
+/*
+Local Variables:
+c-file-style: "linux"
+c-basic-offset: 4
+tab-width: 4
+End:
+*/