+ if (res)
+ cmd_error(CMD_RET_FAILURE, res, NULL);
+
+ return CMD_RET_SUCCESS;
+}
+
+
+typedef void (*fatfunc_t)(const TCHAR* path_old, const TCHAR* path_new);
+
+command_ret_t exit_val;
+uint8_t cmd_flags;
+#define I_FLAG (1<<1) // prompt before overwrite (overrides a previous -n option)
+#define N_FLAG (1<<2) // do not overwrite an existing file (overrides a previous -i option)
+#define F_FLAG (1<<3) // overwrite existing file ignoring write protection
+#define P_FLAG (1<<4) // preserve attributes and timestamps
+#define V_FLAG (1<<5) // explain what is being done
+
+char* splitpath(const char* path)
+{
+ char* fs = strrchr(path, '/');
+ char* bs = strrchr(path, '\\');
+ if (fs > bs) {
+ *fs = '\0';
+ return fs + 1;
+ } else if (bs != NULL) {
+ *bs = '\0';
+ return bs + 1;
+ }
+ return NULL;
+}
+
+/*
+ * Copy File
+*/
+FRESULT copy_file(const TCHAR* src, const TCHAR* dst)
+{
+ FIL fsrc, fdst;
+ FRESULT res;
+
+ /* File copy buffer */
+ uint8_t *buffer = (uint8_t *) malloc(CPY_BUF_SIZE);
+ if (buffer == NULL) {
+ res = (FRESULT) ENOMEM;
+ } else {
+
+ /* Open source file */
+ res = f_open(&fsrc, src, FA_READ);
+ if (res == FR_OK) {
+
+ /* Create destination file */
+ res = f_open(&fdst, dst, FA_WRITE | FA_CREATE_NEW);
+ if (res == FR_OK) {
+ UINT br, bw; /* File read/write count */
+
+ /* Copy source to destination */
+ for (;;) {
+ res = f_read(&fsrc, buffer, CPY_BUF_SIZE, &br); /* Read a chunk of source file */
+ if (res || br == 0)
+ break; /* error or eof */
+ res = f_write(&fdst, buffer, br, &bw); /* Write it to the destination file */
+ if (res != FR_OK)
+ break;
+ if (bw < br) {
+ res = (FRESULT) EFULL; /* disk full */
+ break;
+ }
+ }
+
+debug_cp("==== copy() res: %d, br: %d, bw: %d\n", res, br, bw);
+ f_close(&fdst);
+ if (res != FR_OK)
+ f_unlink(dst);
+ }
+ f_close(&fsrc);
+ }
+ free(buffer);
+ }
+
+ return res;
+}
+
+void ff_remove(const TCHAR *file, const TCHAR *dest UNUSED)
+{
+ FRESULT res = FR_OK;
+
+ debug_rm("==== ff_remove: '%s'\n", file);
+
+ if (!(cmd_flags & N_FLAG)) {
+ if ((res = f_unlink(file)) == FR_OK) {
+ if (cmd_flags & V_FLAG)
+ printf_P(PSTR("removed: '%s'\n"), file);
+ } else {
+ cmd_error(0, res, PSTR("cannot remove '%s'"), file);
+ }
+ } else {
+ printf_P(PSTR("not removed: '%s'\n"), file);
+ }
+
+ exit_val = (res != FR_OK);
+ return;
+}
+
+void ff_copy(const TCHAR* path_old, const TCHAR* path_new)
+{
+ FRESULT res;
+
+ debug_cp("==== ff_copy: '%s' --> '%s'\n", path_old, path_new);
+
+ if (cmd_flags & V_FLAG)
+ printf_P(PSTR("'%s' -> '%s'\n"), path_old, path_new);
+ if ((res = copy_file(path_old, path_new)) != FR_OK)
+ cmd_error(0, res, PSTR("error copying '%s' to '%s'"), path_old, path_new);
+
+ exit_val = (res != FR_OK);
+ return;
+}
+
+void ff_move(const TCHAR* path_old, const TCHAR* path_new)
+{
+ FRESULT res;
+
+ if (cmd_flags & V_FLAG)
+ printf_P(PSTR("'%s' -> '%s'\n"), path_old, path_new);
+ if ((res = f_rename(path_old, path_new)) != FR_OK)
+ cmd_error(0, res, PSTR("error copying '%s' to '%s'"), path_old, path_new);
+
+ exit_val = (res != FR_OK);
+ return;
+}
+
+void ff_iterate(fatfunc_t fatfunc, int count, char* const file[], char* dest)
+{
+ FRESULT res = 0;
+ DIR dir;
+ FILINFO finfo;
+ char srcpath[PATH_MAX], destpath[PATH_MAX];
+
+ uint8_t dest_is_dir = dest != NULL && f_stat(dest, &finfo) == FR_OK && finfo.fattrib & AM_DIR;
+ for (uint8_t i = 0; i < count; i++) {
+ char* pattern = NULL;
+ strcpy(srcpath, file[i]);
+ char* p1 = path_split(srcpath);
+
+ if (p1 != NULL) {
+ pattern = (char *) malloc(strlen(p1)+1);
+ strcpy(pattern, p1);
+ }
+ if (pattern != NULL) {
+ res = f_findfirst(&dir, &finfo, srcpath, pattern);
+ p1 = srcpath+strlen(srcpath)-1;
+ if (*p1++ != '/')
+ *(p1++) = '/';
+ } else {
+ res = f_findfirst(&dir, &finfo, ".", file[i]);
+ p1 = srcpath;
+ }
+ if ((res != FR_OK) || (finfo.fname[0] == 0)) {
+ cmd_error(0, res, PSTR("'%s': no such file or directory"), file[i]);
+ exit_val = CMD_RET_FAILURE;
+ } else {
+
+ while (res == FR_OK && finfo.fname[0] != 0) {
+ strcpy(p1, finfo.fname);
+ if (dest != NULL) {
+ strcpy(destpath, dest);
+ if (dest_is_dir) {
+ strcat_P(destpath, PSTR("/"));
+ strcat(destpath, finfo.fname);
+ }
+ }
+ fatfunc(srcpath, destpath);
+ res = f_findnext(&dir, &finfo);
+ }
+ if (res != FR_OK)
+ cmd_error(CMD_RET_FAILURE, res, PSTR("error enumerating files"));
+ }
+ f_closedir(&dir);
+ free(pattern);
+ }
+}
+
+command_ret_t do_rm(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc, char * const argv[])
+{
+ exit_val = CMD_RET_SUCCESS;
+ cmd_flags = 0;
+
+ int opt;
+ while ((opt = getopt(argc, argv, PSTR("nv"))) != -1) {
+ switch (opt) {
+ case 'n':
+ cmd_flags |= N_FLAG;
+ break;
+ case 'v':
+ cmd_flags |= V_FLAG;
+ break;
+ default:
+ return CMD_RET_USAGE;
+ break;
+ }
+ }
+ argc -= optind;
+ argv += optind;
+
+ debug_rm("==== do_rm: argc, argv[0]: %d, '%s'\n", argc, argv[0]);
+
+ if (argc < 1)
+ return CMD_RET_USAGE;
+
+ ff_iterate(ff_remove, argc, argv, NULL);
+
+ return exit_val;
+}
+
+command_ret_t do_cp_or_mv(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc, char * const argv[])
+{
+ exit_val = CMD_RET_SUCCESS;
+ cmd_flags = 0;
+ fatfunc_t func = (argv[0][0] == 'c') ? ff_copy : ff_move;
+
+ int opt;
+ while ((opt = getopt(argc, argv, PSTR("v"))) != -1) {
+ switch (opt) {
+ case 'v':
+ cmd_flags |= V_FLAG;
+ break;
+ default:
+ return CMD_RET_USAGE;
+ break;
+ }