blob: b69cccdf1a20fcf23a01c8179b31fdd8a9e201d3 [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 Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 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 *
Bernhard Reutner-Fischer20f40002006-01-30 17:17:14 +000010 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersen9f0fedb2001-04-24 18:07:19 +000011 */
12
Manuel Novoa III cad53642003-03-19 09:13:01 +000013/* BB_AUDIT SUSv3 compliant */
14/* BB_AUDIT GNU defects - unsupported options -c, -f, -v, and long options. */
15/* http://www.opengroup.org/onlinepubs/007904975/utilities/chmod.html */
16
Eric Andersen9f0fedb2001-04-24 18:07:19 +000017#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <unistd.h>
Manuel Novoa III cad53642003-03-19 09:13:01 +000021#include <sys/stat.h>
Eric Andersen9f0fedb2001-04-24 18:07:19 +000022#include "busybox.h"
Eric Andersen9f0fedb2001-04-24 18:07:19 +000023
Eric Andersen9f0fedb2001-04-24 18:07:19 +000024static int fileAction(const char *fileName, struct stat *statbuf, void* junk)
25{
Manuel Novoa III cad53642003-03-19 09:13:01 +000026 if (!bb_parse_mode((char *)junk, &(statbuf->st_mode)))
Manuel Novoa III ea4c4342003-03-19 18:09:03 +000027 bb_error_msg_and_die( "invalid mode: %s", (char *)junk);
Matt Kraai6aabfd52001-04-26 18:55:29 +000028 if (chmod(fileName, statbuf->st_mode) == 0)
Eric Andersen9f0fedb2001-04-24 18:07:19 +000029 return (TRUE);
Manuel Novoa III cad53642003-03-19 09:13:01 +000030 bb_perror_msg("%s", fileName); /* Avoid multibyte problems. */
Eric Andersen9f0fedb2001-04-24 18:07:19 +000031 return (FALSE);
32}
33
Bernhard Reutner-Fischer20f40002006-01-30 17:17:14 +000034int chmod_main(int ATTRIBUTE_UNUSED argc, char **argv)
Eric Andersen9f0fedb2001-04-24 18:07:19 +000035{
Manuel Novoa III cad53642003-03-19 09:13:01 +000036 int retval = EXIT_SUCCESS;
Eric Andersen9f0fedb2001-04-24 18:07:19 +000037 int recursiveFlag = FALSE;
Manuel Novoa III cad53642003-03-19 09:13:01 +000038 int count;
Eric Andersend274b532002-10-10 03:47:01 +000039 char *smode;
Manuel Novoa III cad53642003-03-19 09:13:01 +000040 char **p;
41 char *p0;
42 char opt = '-';
Eric Andersen9f0fedb2001-04-24 18:07:19 +000043
Manuel Novoa III cad53642003-03-19 09:13:01 +000044 ++argv;
45 count = 0;
Eric Andersend274b532002-10-10 03:47:01 +000046
Manuel Novoa III cad53642003-03-19 09:13:01 +000047 for (p = argv ; *p ; p++) {
48 p0 = p[0];
49 if (p0[0] == opt) {
50 if ((p0[1] == '-') && !p0[2]) {
51 opt = 0; /* Disable further option processing. */
52 continue;
53 }
54 if (p0[1] == 'R') {
55 char *s = p0 + 2;
56 while (*s == 'R') {
57 ++s;
58 }
59 if (*s) {
60 bb_show_usage();
61 }
62 recursiveFlag = TRUE;
63 continue;
64 }
65 if (count) {
66 bb_show_usage();
67 }
Eric Andersen9f0fedb2001-04-24 18:07:19 +000068 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000069 argv[count] = p0;
70 ++count;
Eric Andersen9f0fedb2001-04-24 18:07:19 +000071 }
72
Manuel Novoa III cad53642003-03-19 09:13:01 +000073 argv[count] = NULL;
Eric Andersend274b532002-10-10 03:47:01 +000074
Manuel Novoa III cad53642003-03-19 09:13:01 +000075 if (count < 2) {
76 bb_show_usage();
Eric Andersen9f0fedb2001-04-24 18:07:19 +000077 }
78
Manuel Novoa III cad53642003-03-19 09:13:01 +000079 smode = *argv;
80 ++argv;
81
Eric Andersen9f0fedb2001-04-24 18:07:19 +000082 /* Ok, ready to do the deed now */
Manuel Novoa III cad53642003-03-19 09:13:01 +000083 do {
Rob Landleya75c1802005-12-12 06:58:53 +000084 if (! recursive_action (*argv, recursiveFlag, TRUE, FALSE,
Manuel Novoa III cad53642003-03-19 09:13:01 +000085 fileAction, fileAction, smode)) {
86 retval = EXIT_FAILURE;
Eric Andersen9f0fedb2001-04-24 18:07:19 +000087 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000088 } while (*++argv);
89
90 return retval;
Eric Andersen9f0fedb2001-04-24 18:07:19 +000091}
92
93/*
94Local Variables:
95c-file-style: "linux"
96c-basic-offset: 4
97tab-width: 4
98End:
99*/