+#endif
+
+/******************************************************************************/
+
+
+typedef struct {
+ char *p_end; /* pointer to NULL at end of path */
+ char p_path[MAX_PATHLEN + 1]; /* pointer to the start of a path */
+} PATH_T;
+
+static inline void strip_trailing_slash(PATH_T *p)
+{
+ while (p->p_end > p->p_path && p->p_end[-1] == '/')
+ *--p->p_end = 0;
+}
+
+/*
+ * Move specified string into path. Convert "" to "." to handle BSD
+ * semantics for a null path. Strip trailing slashes.
+ */
+int
+path_set(PATH_T *p, char *string)
+{
+ if (strlen(string) > MAX_PATHLEN) {
+ //err("%s: name too long", string);
+ return(0);
+ }
+
+ (void)strcpy(p->p_path, string);
+ p->p_end = p->p_path + strlen(p->p_path);
+
+ if (p->p_path == p->p_end) {
+ *p->p_end++ = '.';
+ *p->p_end = 0;
+ }
+
+ strip_trailing_slash(p);
+ return(1);
+}
+
+#define R_FLAG (1<<0)
+#define I_FLAG (1<<1)
+#define N_FLAG (1<<2)
+#define F_FLAG (1<<3)
+#define P_FLAG (1<<4)
+#define V_FLAG (1<<5)
+
+command_ret_t do_cp(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc, char * const argv[])
+{
+
+ FRESULT fr; /* Return value */
+ DIR dj; /* Directory search object */
+ FILINFO fno; /* File information */
+
+ uint8_t flags = 0;
+
+
+ /* reset getopt() */
+ optind = 0;
+
+ int opt;
+ while ((opt = getopt(argc, argv, PSTR("Rrfip"))) != -1) {
+ switch (opt) {
+ case 'f':
+ flags &= I_FLAG;
+ break;
+ case 'i':
+ flags |= I_FLAG;
+ flags &= F_FLAG;
+ break;
+ case 'p':
+ flags |= P_FLAG;
+ break;
+ case 'R':
+ case 'r':
+ flags |= R_FLAG;
+ break;
+ case 'v':
+ flags |= V_FLAG;
+ break;
+ default:
+ return CMD_RET_USAGE;
+ break;
+ }
+ }
+ argc -= optind;
+ argv += optind;
+
+ if (argc < 2)
+ return CMD_RET_USAGE;
+
+#if 0
+ /* consume last argument first. */
+ if (!path_set(&to, argv[--argc]))
+ exit(1);
+
+
+ if (flags & V_FLAG)
+ printf_P((PSTR("%s %s -> %s\n", badcp ? : "ERR:" : " ", curr->fts_path, to.p_path)));
+
+#endif
+
+/* Search a directory for objects and display it */
+
+
+ fr = f_findfirst(&dj, &fno, argv[1], argv[2]); /* Start to search for files */
+
+ while (fr == FR_OK && fno.fname[0]) { /* Repeat while an item is found */
+ printf_P(PSTR("%c%c%c%c%c %u/%02u/%02u %02u:%02u %9lu %s\n"),
+ (fno.fattrib & AM_DIR) ? 'D' : '-',
+ (fno.fattrib & AM_RDO) ? 'R' : '-',
+ (fno.fattrib & AM_HID) ? 'H' : '-',
+ (fno.fattrib & AM_SYS) ? 'S' : '-',
+ (fno.fattrib & AM_ARC) ? 'A' : '-',
+ (fno.fdate >> 9) + 1980, (fno.fdate >> 5) & 15, fno.fdate & 31,
+ (fno.ftime >> 11), (fno.ftime >> 5) & 63,
+ fno.fsize, fno.fname);
+ fr = f_findnext(&dj, &fno); /* Search for next item */
+ }
+
+ f_closedir(&dj);
+
+ return CMD_RET_SUCCESS;
+}
+
+/******************************************************************************/
+
+/* Work register for fs command */
+struct stat_dat_s {
+ DWORD AccSize;
+ WORD AccFiles, AccDirs;
+ FILINFO Finfo;
+};
+
+static
+FRESULT scan_files (
+ char *path, /* Pointer to the working buffer with start path */
+ struct stat_dat_s *statp
+)
+{
+ DIR dirs;
+ FRESULT res;
+ int i;
+ char *fn;
+
+ res = f_opendir(&dirs, path);
+ swirl();
+ if (res == FR_OK) {
+ i = strlen(path);
+ while (((res = f_readdir(&dirs, &statp->Finfo)) == FR_OK) &&
+ statp->Finfo.fname[0]) {
+ if (_FS_RPATH && statp->Finfo.fname[0] == '.')
+ continue;
+ fn = statp->Finfo.fname;
+ if (statp->Finfo.fattrib & AM_DIR) {
+ statp->AccDirs++;
+ path[i] = '/';
+ strcpy(path+i+1, fn);
+ res = scan_files(path, statp);
+ path[i] = '\0';
+ if (res != FR_OK)
+ break;
+ } else {
+ //printf_P(PSTR("%s/%s\n"), path, fn);
+ statp->AccFiles++;
+ statp->AccSize += statp->Finfo.fsize;
+ }
+ if (check_abort()) {
+ res = 255;
+ break;
+ }
+ }
+ }
+
+ return res;
+}
+
+
+/*
+ * fatstat path - Show logical drive status
+ *
+ */
+command_ret_t do_stat(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc, char * const argv[])
+{
+ FATFS *fs;
+ DWORD nfreeclst;
+ FRESULT res;
+ char *buf;
+ char *path = "";
+ struct stat_dat_s statp;
+
+ buf = (char *) malloc(BUFFER_SIZE);
+ if (buf == NULL) {
+ printf_P(PSTR("fat stat: Out of Memory!\n"));
+ return CMD_RET_FAILURE;
+ }
+
+ if (argc > 1)
+ path = argv[1];
+ res = f_getfree(path, &nfreeclst, &fs);
+ if (!res) {
+ printf_P(PSTR(
+ "FAT type: %u\n"
+ "Bytes/Cluster: %lu\n"
+ "Number of FATs: %u\n"
+ "Root DIR entries: %u\n"
+ "Sectors/FAT: %lu\n"
+ "Number of clusters: %lu\n"
+ "FAT start (lba): %lu\n"
+ "DIR start (lba,cluster): %lu\n"
+ "Data start (lba): %lu\n"),
+ fs->fs_type, (DWORD)fs->csize * 512, fs->n_fats,
+ fs->n_rootdir, fs->fsize, fs->n_fatent - 2,
+ fs->fatbase, fs->dirbase, fs->database);
+
+#if _USE_LABEL
+ DWORD serial;
+ res = f_getlabel(path, buf, &serial);
+ if (!res) {
+ printf_P(PSTR(
+ "Volume name: %s\n"
+ "Volume S/N: %04X-%04X\n"),
+ buf, (WORD)(serial >> 16), (WORD)(serial & 0xFFFF));
+ }
+#endif
+ if (!res) {
+ my_puts_P(PSTR("\nCounting... "));
+ statp.AccSize = statp.AccFiles = statp.AccDirs = 0;
+ strcpy(buf, path);
+
+ res = scan_files(buf, &statp);
+ }
+ if (!res) {
+ printf_P(PSTR("\r%u files, %lu bytes.\n%u folders.\n"
+ "%lu KB total disk space.\n%lu KB available.\n"),
+ statp.AccFiles, statp.AccSize, statp.AccDirs,
+ (fs->n_fatent - 2) * (fs->csize / 2), nfreeclst * (fs->csize / 2)
+ );
+ }
+ }
+
+ free(buf);
+ if (res) {
+ put_rc(res);
+ return CMD_RET_FAILURE;
+ }
+ return CMD_RET_SUCCESS;
+}