summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhathach <[email protected]>2022-11-20 16:40:15 +0700
committerhathach <[email protected]>2022-11-20 16:40:15 +0700
commit2312bfe3c4be116403974f922d08e5f5cf8a166c (patch)
tree8758c7302fb6067f552153709ba4f4ec39f4bc9a
parentbf69ffb8f7cf2068baff34a197b6770332ddb794 (diff)
implement cp command
-rw-r--r--examples/host/msc_file_explorer/src/msc_app.c61
1 files changed, 59 insertions, 2 deletions
diff --git a/examples/host/msc_file_explorer/src/msc_app.c b/examples/host/msc_file_explorer/src/msc_app.c
index f435e6b96..5b9b72d43 100644
--- a/examples/host/msc_file_explorer/src/msc_app.c
+++ b/examples/host/msc_file_explorer/src/msc_app.c
@@ -269,8 +269,9 @@ DRESULT disk_ioctl (
// CLI Commands
//--------------------------------------------------------------------+
-void cli_cmd_cd(EmbeddedCli *cli, char *args, void *context);
void cli_cmd_cat(EmbeddedCli *cli, char *args, void *context);
+void cli_cmd_cd(EmbeddedCli *cli, char *args, void *context);
+void cli_cmd_cp(EmbeddedCli *cli, char *args, void *context);
void cli_cmd_ls(EmbeddedCli *cli, char *args, void *context);
void cli_cmd_mkdir(EmbeddedCli *cli, char *args, void *context);
void cli_cmd_mv(EmbeddedCli *cli, char *args, void *context);
@@ -321,6 +322,14 @@ bool cli_init(void)
});
embeddedCliAddBinding(_cli, (CliCommandBinding) {
+ "cp",
+ "Usage: cp SOURCE DEST\r\n\tCopy SOURCE to DEST.",
+ true,
+ NULL,
+ cli_cmd_cp
+ });
+
+ embeddedCliAddBinding(_cli, (CliCommandBinding) {
"ls",
"Usage: ls [DIR]...\r\n\tList information about the FILEs (the current directory by default).",
true,
@@ -370,7 +379,7 @@ void cli_cmd_cat(EmbeddedCli *cli, char *args, void *context)
printf("%s: No such file or directory\r\n", fpath);
}else
{
- uint8_t buf[64];
+ uint8_t buf[512];
size_t count = 0;
while ( (FR_OK == f_read(&fi, buf, sizeof(buf), &count)) && (count > 0) )
{
@@ -415,6 +424,54 @@ void cli_cmd_cd(EmbeddedCli *cli, char *args, void *context)
}
}
+void cli_cmd_cp(EmbeddedCli *cli, char *args, void *context)
+{
+ (void) cli; (void) context;
+
+ uint16_t argc = embeddedCliGetTokenCount(args);
+ if ( argc != 2 )
+ {
+ printf("invalid arguments\r\n");
+ return;
+ }
+
+ // default is current directory
+ const char* src = embeddedCliGetToken(args, 1);
+ const char* dst = embeddedCliGetToken(args, 2);
+
+ FIL f_src;
+ FIL f_dst;
+
+ if ( FR_OK != f_open(&f_src, src, FA_READ) )
+ {
+ printf("cannot stat '%s': No such file or directory\r\n", src);
+ return;
+ }
+
+ if ( FR_OK != f_open(&f_dst, dst, FA_WRITE | FA_CREATE_ALWAYS) )
+ {
+ printf("cannot create '%s'\r\n", dst);
+ return;
+ }else
+ {
+ uint8_t buf[512];
+ size_t rd_count = 0;
+ while ( (FR_OK == f_read(&f_src, buf, sizeof(buf), &rd_count)) && (rd_count > 0) )
+ {
+ size_t wr_count = 0;
+
+ if ( FR_OK != f_write(&f_dst, buf, rd_count, &wr_count) )
+ {
+ printf("cannot write to '%s'\r\n", dst);
+ break;
+ }
+ }
+ }
+
+ f_close(&f_src);
+ f_close(&f_dst);
+}
+
void cli_cmd_ls(EmbeddedCli *cli, char *args, void *context)
{
(void) cli; (void) context;