]> cloudbase.mooo.com Git - z180-stamp.git/commitdiff
fat ls: works again
authorLeo C <leo@lenti.loc>
Sat, 21 Apr 2018 11:50:39 +0000 (13:50 +0200)
committerLeo C <leo@lenti.loc>
Sat, 21 Apr 2018 11:50:39 +0000 (13:50 +0200)
avr/cmd_fat.c

index e7e3c396428dc2698b168bb5d8f28a24eede60d7..0fcc72c51e9ed4bed7325587c381c172a2080799 100644 (file)
@@ -267,10 +267,17 @@ path_restore(PATH_T *p, char *old)
  */
 char *path_basename(PATH_T *p)
 {
-       char *basename;
+       char *basename = strrchr(p->p_path, '/');
 
-       basename = strrchr(p->p_path, '/');
-       return(basename ? basename + 1 : p->p_path);
+       if (basename) {
+               ++basename;
+       } else {
+               basename = p->p_path;
+               if ((basename[0] & 0x38) == '0' &&  basename[1] == ':')
+                       basename += 2;
+       }
+
+       return basename;
 }
 
 
@@ -408,31 +415,21 @@ command_ret_t do_ls(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc,
                if (!path_set(&from, argv[1])) {
                        /* TODO: error out*/
                }
-#if 0
-       char *p = strrchr(path, '/');
-       if (p)
-               p++;
-       else {
-               p = path;
-               char *q = p;
-               if ((*q++ & 0x38) == '0' &&  *q++ == ':')
-                       p = q;
-       }
-#endif
+
        char *p = path_basename(&from);
-       char *pattern;
        if (strpbrk_P(p, PSTR("*?")) ||
                        (f_stat(from.p_path, &Finfo) == FR_OK && !(Finfo.fattrib & AM_DIR))) {
-               pattern = strdup(p);
+               path_set(&to, p);
                *p = '\0';
-       } else
-               pattern = strdup("*");
-       //strip_trailing_slash_relpath(path);
+       } else {
+               to.p_path[0] = '*';
+               to.p_path[1] = '\0';
+       }
 
-debug_ls("==== path: '%s', pattern: '%s'\n", from.p_path ? from.p_path : "<NULL>", pattern ? pattern : "<NULL>");
+debug_ls("==== path: '%s', pattern: '%s'\n", from.p_path ? from.p_path : "<NULL>", to.p_path ? to.p_path : "<NULL>");
 
        p1 = s1 = s2 = 0;
-       res = f_findfirst(&Dir, &Finfo, from.p_path, pattern);  /* Start to search for files */
+       res = f_findfirst(&Dir, &Finfo, from.p_path, to.p_path);  /* Start to search for files */
        while (res == FR_OK && Finfo.fname[0]) {
                if (Finfo.fattrib & AM_DIR) {
                        s2++;
@@ -445,7 +442,6 @@ debug_ls("==== path: '%s', pattern: '%s'\n", from.p_path ? from.p_path : "<NULL>
                res = f_findnext(&Dir, &Finfo);
        }
        f_closedir(&Dir);
-       free(pattern);
 
        if (res == FR_OK) {
                printf_P(PSTR("%4u File(s),%10lu bytes total\n%4u Dir(s)"), s1, p1, s2);
@@ -520,35 +516,6 @@ command_ret_t do_tst(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc
        return CMD_RET_SUCCESS;
 }
 
-#if 0
-static
-FRESULT mkpath(TCHAR *path)
-{
-       /* TODO: */
-       (void) path;
-       FILINFO fd
-       TCHAR *p, *q;
-       FRESULT ret;
-
-       res = f_stat (path, &fd)
-
-       p = strchr(path, ':');
-       if (p == NULL || *++p == '\0' || *p++ != '/')
-               return FR_OK;
-
-       while ((q = strchr(p, '/')) != NULL) {
-               *q = '\0';
-               ret = f_mkdir(path);
-               *q = '/';
-                       if (ret != FR_OK && ret != FR_EXIST)
-                               return ret;
-               p = q + 1;
-       }
-
-       return FR_OK;
-}
-#endif
-
 /******************************************************************************/
 
 static uint8_t flags;
@@ -900,15 +867,6 @@ command_ret_t do_cp(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc,
        if (argc < 2)
                return CMD_RET_USAGE;
 
-#if 0
-       from = (PATH_T *) malloc(sizeof(PATH_T));
-       to   = (PATH_T *) malloc(sizeof(PATH_T));
-       if (from == NULL || to == NULL) {
-               printf_P(PSTR("cp: Out of Memory!\n"));
-               command_ret = CMD_RET_FAILURE;
-               goto cleanup;
-       }
-#endif
        path_init();
 
        /* last argument is destination */