]> cloudbase.mooo.com Git - z180-stamp.git/blobdiff - avr/cmd_fat.c
fat cp: flags
[z180-stamp.git] / avr / cmd_fat.c
index b0c889c767f1671f1aa38e6855f88a9388e391e6..29d96dec13becbd934fb999ae0525d3a7ef5bb24 100644 (file)
@@ -522,16 +522,16 @@ char *path_basename(PATH_T *p)
 }
 
 
-uint8_t flags = 0;
+static uint8_t flags;
 PATH_T *from;
 PATH_T *to;
 
-#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)
+#define R_FLAG (1<<0)  // copy directories recursively
+#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)  // if an existing destination file cannot be opened, remove it and try again (this option is ignored when the -n option is also used)
+#define P_FLAG (1<<4)  // preserve attributes and timestamps
+#define V_FLAG (1<<5)  // explain what is being done
 
 static void
 setfile(FILINFO *fs, FIL *fd)
@@ -576,13 +576,8 @@ debug("     from:'%s'  to:'%s'\n", from->p_path, to->p_path);
         */
        if (!dne) {
                if (flags & I_FLAG) {
-                       int checkch, ch;
-
                        printf_P(PSTR("overwrite %s? "), to->p_path);
-                       checkch = ch = getchar();
-                       while (ch != '\n' && ch != EOF)
-                               ch = getchar();
-                       if (checkch != 'y') {
+                       if (!confirm_yes()) {
                                f_close(&from_fd);
                                return;
                        }
@@ -784,36 +779,38 @@ command_ret_t do_cp(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc,
     char *old_to;
 
 
+       uint8_t tflags = 0;
        command_ret = CMD_RET_SUCCESS;
 
        /* reset getopt() */
        optind = 0;
 
        int opt;
-       while ((opt = getopt(argc, argv, PSTR("Rrfip"))) != -1) {
+       while ((opt = getopt(argc, argv, PSTR("rfipv"))) != -1) {
                switch (opt) {
                        case 'f':
-                               flags &= I_FLAG;
+                               tflags &= ~I_FLAG;
+                               tflags |=  F_FLAG;
                                break;
                        case 'i':
-                               flags |= I_FLAG;
-                               flags &= F_FLAG;
+                               tflags &= ~F_FLAG;
+                               tflags |=  I_FLAG;
                                break;
                        case 'p':
-                               flags |= P_FLAG;
+                               tflags |= P_FLAG;
                                break;
-                       case 'R':
                        case 'r':
-                               flags |= R_FLAG;
+                               tflags |= R_FLAG;
                                break;
                        case 'v':
-                               flags |= V_FLAG;
+                               tflags |= V_FLAG;
                                break;
                        default:
                                return CMD_RET_USAGE;
                                break;
                }
        }
+       flags = tflags;
        argc -= optind;
        argv += optind;
 
@@ -852,7 +849,7 @@ command_ret_t do_cp(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc,
         */
 
        fr = f_stat(to->p_path, &to_stat);
-debug("==== main, stat to: fr: %d, attr: %02x\n", fr, to_stat.fattrib);
+debug("==== main, stat to: fr: %d, attr: %02x, flags:%02x\n", fr, to_stat.fattrib, flags);
 debug("     from:'%s'  to:'%s'\n", from->p_path, to->p_path);
 
        if (fr != FR_OK && fr != FR_NO_FILE && fr != FR_NO_PATH) {
@@ -1206,8 +1203,15 @@ CMD_TBL_ITEM(
 CMD_TBL_ITEM(
        cp,     CONFIG_SYS_MAXARGS,     CTBL_DBG,       do_cp,
        "copy files",
-       "[-R] [-f | -i | -n] [-aprv] source_file target_file\n"
-       "    - \n"
+       "[-f | -i | -n] [-prv] source_file target_file\n"
+       "cp [-f | -i | -n] [-prv] source_file ... target_dir\n"
+       "    -f overwrite existing file ignoring write protection\n"
+       "       this option is ignored when the -n option is also used\n"
+       "    -i prompt before overwrite (overrides a previous -n option)\n"
+       "    -n do not overwrite an existing file (overrides a previous -i option)\n"
+       "    -p preserve attributes and timestamps\n"
+       "    -r copy directories recursively\n"
+       "    -v explain what is being done\n"
 ),
 
 CMD_TBL_ITEM(