Skip to content

Commit aaf4e9d

Browse files
JianyuWang0623xiaoxiang781216
authored andcommitted
nshlib: Support "-f" option for command "rm"
In below two cases "rm" command with "-f" option will return 0: a. Bad arguments b. File not exists Following "rm" of GNU coreutils 8.32 GNU coreutils $ rm --version rm (GNU coreutils) 8.32 Copyright (C) 2020 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Written by Paul Rubin, David MacKenzie, Richard M. Stallman, and Jim Meyering. $ rm /FILE_NOT_EXISTS rm: cannot remove '/FILE_NOT_EXISTS': No such file or directory $ echo $? 1 $ rm -f $ echo $? 0 $ rm -f /FILE_NOT_EXISTS $ echo $? 0 Without this patch nsh> rm nsh: rm: missing required argument(s) nsh> rm /FILE_NOT_EXISTS nsh: rm: unlink failed: 2 nsh> echo $? 1 With this patch nsh> rm -f nsh> echo $? 0 nsh> rm -f /FILE_NOT_EXISTS nsh> echo $? 0 Signed-off-by: wangjianyu3 <[email protected]>
1 parent d94aa77 commit aaf4e9d

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed

nshlib/nsh_command.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ static const struct cmdmap_s g_cmdmap[] =
505505

506506
#ifdef NSH_HAVE_DIROPTS
507507
# ifndef CONFIG_NSH_DISABLE_RM
508-
CMD_MAP("rm", cmd_rm, 2, 3, "[-r] <file-path>"),
508+
CMD_MAP("rm", cmd_rm, 2, 3, "[-rf] <file-path>"),
509509
# endif
510510
#endif
511511

nshlib/nsh_fscmds.c

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2178,20 +2178,48 @@ static int unlink_recursive(FAR char *path, FAR struct stat *stat)
21782178

21792179
int cmd_rm(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
21802180
{
2181-
bool recursive = (strcmp(argv[1], "-r") == 0);
2181+
bool recursive = false;
2182+
bool force = false;
21822183
FAR char *fullpath;
21832184
char buf[PATH_MAX];
21842185
struct stat stat;
21852186
int ret = ERROR;
2187+
int c;
21862188

2187-
if (recursive && argc == 2)
2189+
while ((c = getopt(argc, argv, "rf")) != ERROR)
21882190
{
2189-
nsh_error(vtbl, g_fmtargrequired, argv[0]);
2190-
return ret;
2191+
switch (c)
2192+
{
2193+
case 'r':
2194+
recursive = true;
2195+
break;
2196+
case 'f':
2197+
force = true;
2198+
break;
2199+
case '?':
2200+
nsh_output(vtbl, "Unknown option 0x%x\n", optopt);
2201+
return ret;
2202+
default:
2203+
nsh_error(vtbl, g_fmtargrequired, argv[0]);
2204+
return ret;
2205+
}
21912206
}
21922207

2193-
fullpath = nsh_getfullpath(vtbl, recursive ? argv[2] : argv[1]);
2208+
if (optind >= argc)
2209+
{
2210+
if (force)
2211+
{
2212+
ret = OK;
2213+
}
2214+
else
2215+
{
2216+
nsh_error(vtbl, g_fmtargrequired, argv[0]);
2217+
}
21942218

2219+
return ret;
2220+
}
2221+
2222+
fullpath = nsh_getfullpath(vtbl, argv[optind]);
21952223
if (fullpath != NULL)
21962224
{
21972225
if (recursive)
@@ -2204,6 +2232,11 @@ int cmd_rm(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
22042232
ret = unlink(fullpath);
22052233
}
22062234

2235+
if (force && errno == ENOENT)
2236+
{
2237+
ret = 0;
2238+
}
2239+
22072240
if (ret < 0)
22082241
{
22092242
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "unlink", NSH_ERRNO);

0 commit comments

Comments
 (0)