File: chmod.c

package info (click to toggle)
bash 5.3-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 43,860 kB
  • sloc: ansic: 134,738; sh: 8,866; yacc: 5,966; makefile: 4,697; perl: 4,105; asm: 48; awk: 23; sed: 16
file content (156 lines) | stat: -rw-r--r-- 3,650 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/* chmod - change file mode bits */

/* See Makefile for compilation details. */

/*
   Copyright (C) 2024-2025 Free Software Foundation, Inc.

   This file is part of GNU Bash.
   Bash 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 3 of the License, or
   (at your option) any later version.

   Bash 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 Bash.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <config.h>

#include "bashtypes.h"
#include "posixstat.h"
#include <errno.h>
#include <stdio.h>
#include "bashansi.h"
#if defined (HAVE_UNISTD_H)
#  include <unistd.h>
#endif

#include "builtins.h"
#include "shell.h"
#include "bashgetopt.h"
#include "common.h"

#if !defined (errno)
extern int errno;
#endif

#define ISOCTAL(c)	((c) >= '0' && (c) <= '7')

extern int parse_symbolic_mode (char *, mode_t);

#define STANDARD_BITS	(S_IRWXU | S_IRWXG | S_IRWXO)
#define ALLBITS (STANDARD_BITS | S_ISUID| S_ISGID | S_ISVTX)

int
chmod_builtin (WORD_LIST *list)
{
  int opt, nmode, lmode, rval;
  char *mode;
  struct stat st;
  WORD_LIST *l;

  reset_internal_getopt ();
  mode = (char *)NULL;
  while ((opt = internal_getopt(list, "fhvRHLP")) != -1)
    switch (opt)
      {
	CASE_HELPOPT;
	default:
	  return (EX_DISKFALLBACK);
      }
  list = loptend;

  if (list == 0)
    {
      builtin_usage ();
      return (EX_USAGE);
    }

  mode = list->word->word;
  list = list->next;

  if (list == 0)
    {
      builtin_usage ();
      return (EX_USAGE);
    }

  nmode = -1;
  if (ISOCTAL (*mode))	/* octal number */
    {
      nmode = read_octal (mode);
      if (nmode < 0)
	{
	  builtin_error ("invalid file mode: %s", mode);
	  return (EXECUTION_FAILURE);
	}
    }
  else 				/* test for valid symbolic mode */
    {
      /* initial bits are a=rwx; the mode argument modifies them */
      lmode = parse_symbolic_mode (mode, ALLBITS);
      if (lmode < 0)
	{
	  builtin_error ("invalid file mode: %s", mode);
	  return (EXECUTION_FAILURE);
	}
    }

  for (rval = EXECUTION_SUCCESS, l = list; l; l = l->next)
    {
      lmode = nmode;
      if (stat (l->word->word, &st) < 0)
	{
	  builtin_error ("`%s': cannot stat: %s", l->word->word, strerror (errno));
	  rval = EXECUTION_FAILURE;
	  continue;
	}

      if (lmode == -1)
	{
	  lmode = parse_symbolic_mode (mode, st.st_mode & ALLBITS);
	  if (lmode < 0)
	    {
	      builtin_error ("`%s': invalid file mode: %s", l->word->word, mode);
	      rval = EXECUTION_FAILURE;
	      continue;
	    }
	}

      if (chmod (l->word->word, lmode))
	{
	  builtin_error ("`%s': cannot change mode: %s", l->word->word, strerror (errno));
	  rval = EXECUTION_FAILURE;
	  continue;
	}
    }
  return rval;
}

char *chmod_doc[] = {
	"Change file mode bits.",
	"",
	"Change file mode bits.  Change the mode bits of files named as",
	"arguments, in the order specified, as specified by MODE."
	"The MODE argument may be an octal number or a symbolic mode like",
	"that described in chmod(1).  If a symbolic mode is used, the",
	"operations are interpreted relative to an initial mode of \"a=rwx\".",
	"",
	"The return value is 0 unless an error occurs.",
	(char *)NULL
};

struct builtin chmod_struct = {
	"chmod",
	chmod_builtin,
	BUILTIN_ENABLED,
	chmod_doc,
	"chmod [-R] mode file [file...]",
	0
};