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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
/* rm - remove files and directories with -r */
/* See Makefile for compilation details. */
/*
Copyright (C) 2016 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 <stdio.h>
#include <errno.h>
#include <dirent.h>
#include "builtins.h"
#include "shell.h"
#include "common.h"
#include "bashgetopt.h"
#if !defined (errno)
extern int errno;
#endif
static int rm_file(const char *fname);
static int force, recursive;
static int
_remove_directory(const char *dirname)
{
DIR *dir;
struct dirent *dp;
size_t dirlen;
int err;
dirlen = strlen (dirname);
err = 0;
if ((dir = opendir(dirname)))
{
while ((dp = readdir(dir)))
{
#ifdef __GNUC__
char fname[dirlen + 1 + strlen (dp->d_name) + 1];
#else
char *fname;
int fnsize;
#endif
if (*dp->d_name == '.' && (dp->d_name[1] == 0 || (dp->d_name[1] == '.' && dp->d_name[2] == 0)))
continue;
#ifdef __GNUC__
snprintf(fname, sizeof (fname), "%s/%s", dirname, dp->d_name);
#else
fnsize = dirlen + 1 + strlen (dp->d_name) + 1;
fname = xmalloc (fnsize);
snprintf(fname, fnsize, "%s/%s", dirname, dp->d_name);
#endif
if (rm_file (fname) && force == 0)
err = 1;
#ifndef __GNUC__
free (fname);
#endif
}
closedir(dir);
if (err == 0 && rmdir (dirname) && force == 0)
err = 1;
}
else if (force == 0)
err = 1;
if (err)
builtin_error ("%s: %s", dirname, strerror (errno));
return err;
}
static int
rm_file(const char *fname)
{
if (unlink (fname) == 0)
return 0;
/* If FNAME is a directory glibc returns EISDIR but correct POSIX value
would be EPERM. If we get that error and FNAME is a directory and -r
was supplied, recursively remove the directory and its contents */
if ((errno == EISDIR || errno == EPERM) && recursive && file_isdir (fname))
return _remove_directory(fname);
else if (force)
return 0;
builtin_error ("%s: %s", fname, strerror (errno));
return 1;
}
int
rm_builtin (list)
WORD_LIST *list;
{
const char *name;
WORD_LIST *l;
int rval, opt;
recursive = force = 0;
rval = EXECUTION_SUCCESS;
reset_internal_getopt ();
while ((opt = internal_getopt (list, "Rrfi")) != -1)
{
switch (opt)
{
case 'R':
case 'r':
recursive = 1;
break;
case 'f':
force = 1;
break;
case 'i':
return (EX_DISKFALLBACK);
CASE_HELPOPT;
default:
builtin_usage ();
return (EX_USAGE);
}
}
list = loptend;
if (list == 0)
{
if (force == 0)
{
builtin_usage ();
return (EXECUTION_FAILURE);
}
return (EXECUTION_SUCCESS);
}
for (l = list; l; l = l->next)
{
if (rm_file(l->word->word) && force == 0)
rval = EXECUTION_FAILURE;
}
return rval;
}
char *rm_doc[] = {
"Remove files.",
"",
"rm removes the files specified as arguments.",
(char *)NULL
};
/* The standard structure describing a builtin command. bash keeps an array
of these structures. */
struct builtin rm_struct = {
"rm", /* builtin name */
rm_builtin, /* function implementing the builtin */
BUILTIN_ENABLED, /* initial flags for builtin */
rm_doc, /* array of long documentation strings. */
"rm [-rf] file ...", /* usage synopsis; becomes short_doc */
0 /* reserved for internal use */
};
|