/src/barebox/commands/rm.c
Line | Count | Source |
1 | | // SPDX-License-Identifier: GPL-2.0-only |
2 | | // SPDX-FileCopyrightText: © 2007 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix |
3 | | |
4 | | /* rm.c - remove files */ |
5 | | |
6 | | #include <common.h> |
7 | | #include <command.h> |
8 | | #include <fs.h> |
9 | | #include <libfile.h> |
10 | | #include <getopt.h> |
11 | | #include <errno.h> |
12 | | |
13 | | static int do_rm(int argc, char *argv[]) |
14 | 0 | { |
15 | 0 | int i, opt, recursive = 0, force = 0; |
16 | |
|
17 | 0 | while ((opt = getopt(argc, argv, "rf")) > 0) { |
18 | 0 | switch (opt) { |
19 | 0 | case 'r': |
20 | 0 | recursive = 1; |
21 | 0 | break; |
22 | 0 | case 'f': |
23 | 0 | force = 1; |
24 | 0 | break; |
25 | 0 | default: |
26 | 0 | return COMMAND_ERROR_USAGE; |
27 | 0 | } |
28 | 0 | } |
29 | | |
30 | 0 | if (argc < 2) |
31 | 0 | return COMMAND_ERROR_USAGE; |
32 | | |
33 | 0 | i = optind; |
34 | |
|
35 | 0 | while (i < argc) { |
36 | 0 | int ret; |
37 | |
|
38 | 0 | if (recursive) |
39 | 0 | ret = unlink_recursive(argv[i], NULL); |
40 | 0 | else |
41 | 0 | ret = unlink(argv[i]); |
42 | 0 | if (ret) { |
43 | 0 | if (!force || ret != -ENOENT) |
44 | 0 | printf("could not remove %s: %m\n", argv[i]); |
45 | 0 | if (!force) |
46 | 0 | return 1; |
47 | 0 | } |
48 | 0 | i++; |
49 | 0 | } |
50 | | |
51 | 0 | return 0; |
52 | 0 | } |
53 | | |
54 | | BAREBOX_CMD_HELP_START(rm) |
55 | | BAREBOX_CMD_HELP_TEXT("Options:") |
56 | | BAREBOX_CMD_HELP_OPT ("-r", "remove directories and their contents recursively") |
57 | | BAREBOX_CMD_HELP_OPT ("-f", "ignore nonexistent files") |
58 | | BAREBOX_CMD_HELP_END |
59 | | |
60 | | BAREBOX_CMD_START(rm) |
61 | | .cmd = do_rm, |
62 | | BAREBOX_CMD_DESC("remove files") |
63 | | BAREBOX_CMD_OPTS("[-rf] FILES...") |
64 | | BAREBOX_CMD_GROUP(CMD_GRP_FILE) |
65 | | BAREBOX_CMD_HELP(cmd_rm_help) |
66 | | BAREBOX_CMD_END |