Rev 3277 | Rev 3337 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3277 | Rev 3296 | ||
---|---|---|---|
Line 1... | Line -... | ||
1 | /* Automatically generated by mknewcmd on Mon Aug 11 00:14:56 PHT 2008 |
- | |
2 | * This is machine generated output. The author of mknewcmd claims no |
- | |
3 | * copyright over the contents of this file. Where legally permitted, the |
1 | /* Copyright (c) 2008, Tim Post <tinkertim@gmail.com> |
4 | * contents herein are donated to the public domain. |
2 | * All rights reserved. |
5 | * |
3 | * |
6 | * You should apply any license and copyright that you wish to this file, |
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 |
|
7 | * replacing this header in its entirety. */ |
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 | */ |
|
8 | 30 | ||
9 | #include <stdio.h> |
31 | #include <stdio.h> |
10 | #include <stdlib.h> |
32 | #include <stdlib.h> |
- | 33 | #include <unistd.h> |
|
- | 34 | #include <fcntl.h> |
|
- | 35 | #include <dirent.h> |
|
- | 36 | #include "config.h" |
|
- | 37 | #include "errors.h" |
|
- | 38 | #include "util.h" |
|
11 | #include "entry.h" |
39 | #include "entry.h" |
12 | #include "rm.h" |
40 | #include "rm.h" |
13 | #include "cmds.h" |
41 | #include "cmds.h" |
14 | 42 | ||
15 | static char *cmdname = "rm"; |
43 | static char *cmdname = "rm"; |
16 | 44 | ||
- | 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 | ||
17 | /* Dispays help for rm in various levels */ |
80 | /* Dispays help for rm in various levels */ |
18 | void * help_cmd_rm(unsigned int level) |
81 | void * help_cmd_rm(unsigned int level) |
19 | { |
82 | { |
- | 83 | if (level == HELP_SHORT) { |
|
20 | printf("This is the %s help for '%s'.\n", |
84 | printf("`%s' removes files and directories.\n", cmdname); |
- | 85 | } else { |
|
21 | level ? EXT_HELP : SHORT_HELP, cmdname); |
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 | } |
|
22 | return 0; |
91 | return CMD_VOID; |
23 | } |
92 | } |
24 | 93 | ||
25 | /* Main entry point for rm, accepts an array of arguments */ |
94 | /* Main entry point for rm, accepts an array of arguments */ |
26 | int * cmd_rm(char **argv) |
95 | int * cmd_rm(char **argv) |
27 | { |
96 | { |
28 | unsigned int argc; |
97 | unsigned int argc; |
29 | unsigned int i; |
98 | unsigned int i, scope, ret = 0; |
- | 99 | char *buff; |
|
30 | 100 | ||
31 | /* Count the arguments */ |
101 | /* Count the arguments */ |
32 | for (argc = 0; argv[argc] != NULL; argc ++); |
102 | for (argc = 0; argv[argc] != NULL; argc ++); |
33 | 103 | ||
34 | printf("%s %s\n", TEST_ANNOUNCE, cmdname); |
- | |
35 | printf("%d arguments passed to %s", argc - 1, cmdname); |
- | |
36 | - | ||
37 | if (argc < 2) { |
104 | if (argc < 2) { |
- | 105 | cli_error(CL_EFAIL, |
|
- | 106 | "%s - insufficient arguments. Try `help %s extended'", |
|
38 | printf("\n"); |
107 | cmdname, cmdname); |
39 | return 0; |
108 | return CMD_FAILURE; |
40 | } |
109 | } |
41 | 110 | ||
42 | printf(":\n"); |
- | |
43 | for (i = 1; i < argc; i++) |
111 | for (i = 1; i < argc; i++) { |
44 | printf("[%d] -> %s\n", i, argv[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 | } |
|
45 | 127 | ||
- | 128 | if (ret) |
|
- | 129 | return CMD_FAILURE; |
|
- | 130 | else |
|
46 | return 0; |
131 | return CMD_SUCCESS; |
47 | } |
132 | } |
48 | 133 |