]> cloudbase.mooo.com Git - z180-stamp.git/commitdiff
do_ls() (WIP)
authorLeo C <erbl259-lmu@yahoo.de>
Sat, 26 May 2018 11:11:19 +0000 (13:11 +0200)
committerLeo C <erbl259-lmu@yahoo.de>
Sat, 26 May 2018 11:11:19 +0000 (13:11 +0200)
avr/cmd_fat.c

index b33e27edd66886c68992898ffa3d3e2bfbc270cf..55d6ceb8a51eee13bdc68f60c999fb67e37a0e3f 100644 (file)
@@ -303,6 +303,7 @@ char *path_basename(PATH_T *p)
        return basename;
 }
 
+#if 0
 char *path_basename_pattern(PATH_T *p)
 {
        char *pattern = path_basename(p);
@@ -317,13 +318,14 @@ char *path_basename_pattern(PATH_T *p)
        }
        return pattern;
 }
+#endif
 
 /*
  * Split path
  * Return basename/pattern of path.
  */
 
-static char *path_split_pattern(PATH_T *p)
+char *path_split_pattern(PATH_T *p)
 {
        char *pp = path_skip_heading(p->p_path);
        char *pattern = strrchr(pp, '/');
@@ -342,7 +344,7 @@ static char *path_split_pattern(PATH_T *p)
        return pattern;
 }
 
-static void path_fix(PATH_T *p)
+void path_fix(PATH_T *p)
 {
        char *pp = path_skip_heading(p->p_path);
 
@@ -352,7 +354,7 @@ static void path_fix(PATH_T *p)
        }
 }
 
-static void path_unfix(PATH_T *p)
+void path_unfix(PATH_T *p)
 {
        char *pp = path_skip_heading(p->p_path);
 
@@ -427,27 +429,7 @@ command_ret_t do_cd(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc,
        return command_ret;
 }
 
-#if 0
-/*
- * Remove trailing slashes,
- * but keep a leading slash (absolute path)
- */
-void strip_trailing_slash_relpath(char *p)
-{
-       int n = strlen(p);
 
-       if (n >= 2 && (p[0] & 0x38) == '0' &&  p[1] == ':') {
-               p += 2;
-               n -= 2;
-       }
-       if (n >= 1 &&  p[0] == '/') {
-               p++;
-               n--;
-       }
-       while (n-- != 0  && p[n] == '/')
-                       p[n] = '\0';
-}
-#endif
 
 command_ret_t do_rm(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc, char * const argv[])
 {
@@ -551,9 +533,9 @@ command_ret_t do_mkdir(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int ar
 }
 
 
-int print_dirent(FILINFO *f)
+static void print_dirent(FILINFO *f)
 {
-       return printf_P(PSTR("%c%c%c%c%c %u/%02u/%02u %02u:%02u %9lu  %s\n"),
+       printf_P(PSTR("%c%c%c%c%c %u/%02u/%02u %02u:%02u %9lu  %s\n"),
                                (f->fattrib & AM_DIR) ? 'D' : '-',
                                (f->fattrib & AM_RDO) ? 'R' : '-',
                                (f->fattrib & AM_HID) ? 'H' : '-',
@@ -578,7 +560,7 @@ command_ret_t do_ls(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc,
        FRESULT res;
 
        cmdname = argv[0];
-
+       command_ret = CMD_RET_SUCCESS;
 
        path_init();
        if (argc > 1)
@@ -586,37 +568,47 @@ command_ret_t do_ls(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc,
                        /* TODO: error out*/
                }
 
+#if 0
        char *pattern = path_basename_pattern(&from);
-
+#else
+       char *pattern = path_split_pattern(&from);
+       if (*pattern == '\0')
+               pattern = "*";
+#endif
        debug_ls("==== path: '%s', pattern: '%s'\n", from.p_path ? from.p_path : "<NULL>", pattern ? pattern : "<NULL>");
 
        p1 = s1 = s2 = 0;
        res = f_findfirst(&Dir, &Finfo, from.p_path, pattern);  /* Start to search for files */
-       while (res == FR_OK && Finfo.fname[0]) {
-               if (Finfo.fattrib & AM_DIR) {
-                       s2++;
-               } else {
-                       s1++; p1 += Finfo.fsize;
-               }
-               print_dirent(&Finfo);
-               if (check_abort())
-                       break;
-               res = f_findnext(&Dir, &Finfo);
+       if (res != FR_OK || !Finfo.fname[0]) {
+               path_fix(&from);
+               err(PSTR("'%s%s': No such file or directory"), from.p_path, pattern);
+       } else {
+               do {
+                       if (Finfo.fattrib & AM_DIR) {
+                               s2++;
+                       } else {
+                               s1++; p1 += Finfo.fsize;
+                       }
+                       print_dirent(&Finfo);
+                       if (check_abort())
+                               break;
+                       res = f_findnext(&Dir, &Finfo);
+               } while (res == FR_OK && Finfo.fname[0]);
        }
        f_closedir(&Dir);
 
-       if (res == FR_OK) {
+       if (res == FR_OK && command_ret == CMD_RET_SUCCESS) {
                printf_P(PSTR("%4u File(s),%10lu bytes total\n%4u Dir(s)"), s1, p1, s2);
                if (f_getfree(from.p_path, (DWORD*)&p1, &fs) == FR_OK)
                        printf_P(PSTR(", %10luK bytes free\n"), p1 * fs->csize / 2);
        }
 
-       if (res) {
+       if (res && command_ret == CMD_RET_SUCCESS) {
                put_rc(res);
                return CMD_RET_FAILURE;
        }
 
-       return CMD_RET_SUCCESS;
+       return command_ret;
 }
 
 /*