1
/* Copyright (c) 2008, Tim Post <tinkertim@gmail.com>
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions are met:
7
* Redistributions of source code must retain the above copyright notice, this
8
* list of conditions and the following disclaimer.
10
* Redistributions in binary form must reproduce the above copyright notice,
11
* this list of conditions and the following disclaimer in the documentation
12
* and/or other materials provided with the distribution.
14
* Neither the name of the original program's authors nor the names of its
15
* contributors may be used to endorse or promote products derived from this
16
* software without specific prior written permission.
18
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
* POSSIBILITY OF SUCH DAMAGE.
47
static char *cmdname = "rm";
48
#define RM_VERSION "0.0.1"
52
static struct option const long_options[] = {
53
{ "help", no_argument, 0, 'h' },
54
{ "version", no_argument, 0, 'v' },
55
{ "recursive", no_argument, 0, 'r' },
56
{ "force", no_argument, 0, 'f' },
57
{ "safe", no_argument, 0, 's' },
61
static unsigned int rm_start(rm_job_t *rm)
67
/* Make sure we can allocate enough memory to store
68
* what is needed in the job structure */
69
if (NULL == (rm->nwd = (char *) malloc(PATH_MAX)))
71
memset(rm->nwd, 0, sizeof(rm->nwd));
73
if (NULL == (rm->owd = (char *) malloc(PATH_MAX)))
75
memset(rm->owd, 0, sizeof(rm->owd));
77
if (NULL == (rm->cwd = (char *) malloc(PATH_MAX)))
79
memset(rm->cwd, 0, sizeof(rm->cwd));
83
if (NULL == (getcwd(rm->owd, PATH_MAX)))
89
static void rm_end(rm_job_t *rm)
103
static unsigned int rm_recursive(const char *path)
107
/* First see if it will just go away */
112
/* Its not empty, recursively scan it */
113
cli_error(CL_ENOTSUP,
114
"Can not remove %s, directory not empty", path);
118
static unsigned int rm_single(const char *path)
121
cli_error(CL_EFAIL, "rm: could not remove file %s", path);
127
static unsigned int rm_scope(const char *path)
132
dirp = opendir(path);
138
fd = open(path, O_RDONLY);
147
/* Dispays help for rm in various levels */
148
void help_cmd_rm(unsigned int level)
150
if (level == HELP_SHORT) {
151
printf("`%s' removes files and directories.\n", cmdname);
153
help_cmd_rm(HELP_SHORT);
155
"Usage: %s [options] <path>\n"
157
" -h, --help A short option summary\n"
158
" -v, --version Print version information and exit\n"
159
" -r, --recursive Recursively remove sub directories\n"
160
" -f, --force Do not prompt prior to removing files\n"
161
" -s, --safe Stop if directories change during removal\n\n"
162
"Currently, %s is under development, some options don't work.\n",
168
/* Main entry point for rm, accepts an array of arguments */
169
int cmd_rm(char **argv)
172
unsigned int i, scope, ret = 0;
177
argc = cli_count_args(argv);
181
"%s: insufficient arguments. Try %s --help", cmdname, cmdname);
185
if (!rm_start(&rm)) {
186
cli_error(CL_ENOMEM, "%s: could not initialize", cmdname);
191
for (c = 0, optind = 0, opt_ind = 0; c != -1;) {
192
c = getopt_long(argc, argv, "hvrfs", long_options, &opt_ind);
195
help_cmd_rm(HELP_LONG);
198
printf("%s\n", RM_VERSION);
212
if ((unsigned) optind == argc) {
214
"%s: insufficient arguments. Try %s --help", cmdname, cmdname);
220
while (NULL != argv[i]) {
221
len = str_size(argv[i]) + 2;
222
buff = (char *) realloc(buff, len);
224
printf("rm: out of memory\n");
228
memset(buff, 0, sizeof(buff));
229
snprintf(buff, len, argv[i]);
231
scope = rm_scope(buff);
233
case RM_BOGUS: /* FIXME */
235
ret += rm_single(buff);
238
if (! rm.recursive) {
239
printf("%s is a directory, use -r to remove it.\n", buff);
242
ret += rm_recursive(buff);