Subversion Repositories HelenOS

Rev

Rev 3277 | Rev 3337 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3296 post 1
/* Copyright (c) 2008, Tim Post <tinkertim@gmail.com>
2
 * All rights reserved.
3277 post 3
 *
3296 post 4
 * Redistribution and use in source and binary forms, with or without
5
 * modification, are permitted provided that the following conditions are met:
6
 *
7
 * Redistributions of source code must retain the above copyright notice, this
8
 * list of conditions and the following disclaimer.
9
 *
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.
13
 *
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.
17
 *
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.
29
 */
3277 post 30
 
31
#include <stdio.h>
32
#include <stdlib.h>
3296 post 33
#include <unistd.h>
34
#include <fcntl.h>
35
#include <dirent.h>
36
#include "config.h"
37
#include "errors.h"
38
#include "util.h"
3277 post 39
#include "entry.h"
40
#include "rm.h"
41
#include "cmds.h"
42
 
43
static char *cmdname = "rm";
44
 
3296 post 45
unsigned int rm_recursive(const char *path)
46
{
47
    cli_error(CL_ENOTSUP, "I do not yet remove directories");
48
    return 1;
49
}
50
 
51
unsigned int rm_single(const char *path)
52
{
53
    if (unlink(path)) {
54
        cli_error(CL_EFAIL, "rm: could not remove file %s", path);
55
        return 1;
56
    }
57
    return 0;
58
}
59
 
60
unsigned int rm_scope(const char *path)
61
{
62
    int fd;
63
    DIR *dirp;
64
 
65
    dirp = opendir(path);
66
    if (dirp) {
67
        closedir(dirp);
68
        return RM_DIR;
69
    }
70
 
71
    fd = open(path, O_RDONLY);
72
    if (fd > 0) {
73
        close(fd);
74
        return RM_FILE;
75
    }
76
 
77
    return RM_BOGUS;
78
}
79
 
3277 post 80
/* Dispays help for rm in various levels */
81
void * help_cmd_rm(unsigned int level)
82
{
3296 post 83
    if (level == HELP_SHORT) {
84
        printf("`%s' removes files and directories.\n", cmdname);
85
    } else {
86
        help_cmd_rm(HELP_SHORT);
87
        printf("  %s <path> If <path> is a directory, all files and "
88
            "directories\n  within will be unconditionally removed.\n",
89
                cmdname);
90
    }
91
    return CMD_VOID;
3277 post 92
}
93
 
94
/* Main entry point for rm, accepts an array of arguments */
95
int * cmd_rm(char **argv)
96
{
97
    unsigned int argc;
3296 post 98
    unsigned int i, scope, ret = 0;
99
    char *buff;
3277 post 100
 
101
    /* Count the arguments */
102
    for (argc = 0; argv[argc] != NULL; argc ++);
103
 
104
    if (argc < 2) {
3296 post 105
        cli_error(CL_EFAIL,
106
            "%s - insufficient arguments. Try `help %s extended'",
107
                cmdname, cmdname);
108
        return CMD_FAILURE;
3277 post 109
    }
110
 
3296 post 111
    for (i = 1; i < argc; i++) {
112
        buff = cli_strdup(argv[i]);
113
        scope = rm_scope(buff);
114
        switch (scope) {
115
        case RM_BOGUS:
116
            cli_error(CL_ENOENT, "Can not unlink %s", buff);
117
            break;
118
        case RM_FILE:
119
            ret += rm_single(buff);
120
            break;
121
        case RM_DIR:
122
            ret += rm_recursive(buff);
123
            break;
124
        }
125
        free(buff);
126
    }
3277 post 127
 
3296 post 128
    if (ret)
129
        return CMD_FAILURE;
130
    else
131
        return CMD_SUCCESS;
3277 post 132
}
133