/src/barebox/commands/dirname.c
Line | Count | Source |
1 | | // SPDX-License-Identifier: GPL-2.0-only |
2 | | // SPDX-FileCopyrightText: © 2012 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix |
3 | | |
4 | | /* dirname.c - strip directory and suffix from filenames */ |
5 | | |
6 | | #include <common.h> |
7 | | #include <command.h> |
8 | | #include <libgen.h> |
9 | | #include <environment.h> |
10 | | #include <fs.h> |
11 | | #include <getopt.h> |
12 | | |
13 | | static int do_dirname(int argc, char *argv[]) |
14 | 0 | { |
15 | 0 | int opt; |
16 | 0 | int path_fs = 0; |
17 | 0 | int len = 0; |
18 | |
|
19 | 0 | while ((opt = getopt(argc, argv, "V")) > 0) { |
20 | 0 | switch (opt) { |
21 | 0 | case 'V': |
22 | 0 | path_fs = 1; |
23 | 0 | break; |
24 | 0 | } |
25 | 0 | } |
26 | | |
27 | 0 | if (argc < optind + 2) |
28 | 0 | return COMMAND_ERROR_USAGE; |
29 | | |
30 | 0 | if (path_fs) |
31 | 0 | len = strlen(get_mounted_path(argv[optind])); |
32 | |
|
33 | 0 | setenv(argv[optind + 1], dirname(argv[optind]) + len); |
34 | |
|
35 | 0 | return 0; |
36 | 0 | } |
37 | | |
38 | | BAREBOX_CMD_HELP_START(dirname) |
39 | | BAREBOX_CMD_HELP_TEXT("Strip last component of NAME and store into $DIRNAME") |
40 | | BAREBOX_CMD_HELP_TEXT("") |
41 | | BAREBOX_CMD_HELP_TEXT("Options:") |
42 | | BAREBOX_CMD_HELP_OPT("-V", "return the path relative to the mountpoint.") |
43 | | BAREBOX_CMD_HELP_END |
44 | | |
45 | | BAREBOX_CMD_START(dirname) |
46 | | .cmd = do_dirname, |
47 | | BAREBOX_CMD_DESC("strip last component from a path") |
48 | | BAREBOX_CMD_OPTS("[-V] NAME DIRNAME") |
49 | | BAREBOX_CMD_GROUP(CMD_GRP_FILE) |
50 | | BAREBOX_CMD_HELP(cmd_dirname_help) |
51 | | BAREBOX_CMD_END |