+#endif
+
+/* 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 (FF_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(PATH_MAX);
+ if (buf == NULL)
+ cmd_error(CMD_RET_FAILURE, ENOMEM, NULL);
+
+ 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 FF_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) {
+ statp.AccSize = statp.AccFiles = statp.AccDirs = 0;
+ strcpy(buf, path);
+
+ my_puts_P(PSTR("\nCounting... "));
+ res = scan_files(buf, &statp);
+ putchar('\r');
+ }
+ if (!res) {
+ printf_P(PSTR("%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)
+ cmd_error(CMD_RET_FAILURE, res, NULL);
+
+ return CMD_RET_SUCCESS;
+}