]> cloudbase.mooo.com Git - z180-stamp.git/commitdiff
fat cp: flags
authorLeo C <erbl259-lmu@yahoo.de>
Wed, 11 Apr 2018 21:35:46 +0000 (23:35 +0200)
committerLeo C <erbl259-lmu@yahoo.de>
Wed, 11 Apr 2018 23:52:12 +0000 (01:52 +0200)
avr/cmd_fat.c
avr/con-utils.c
include/con-utils.h

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(
index 4a96771040c536123388f92657a0d1e6fe54c982..410822108b971a710c8fde7b7c29c76690a39a78 100644 (file)
@@ -80,13 +80,29 @@ uint_fast8_t ctrlc(void)
 /* Reads user's confirmation.
    Returns 1 if user's input is "y", "Y", "yes" or "YES"
 */
+uint_fast8_t confirm_yes(void)
+{
+       uint_fast8_t checkch, ch;
+
+       checkch = ch = my_getchar(1);
+       putchar(ch);
+       while (ch != '\r') {
+               ch = my_getchar(1);
+               putchar(ch);
+       }
+       putchar('\n');
+
+       return (checkch  == 'y');
+}
+
+#if 0
 uint_fast8_t confirm_yesno(void)
 {
        unsigned int i;
        char str_input[5];
 
        /* Flush input */
-       while (serial_getc())
+       while (serial_getc() < 0)
                ;
        i = 0;
        while (i < sizeof(str_input)) {
@@ -104,6 +120,7 @@ uint_fast8_t confirm_yesno(void)
                return 1;
        return 0;
 }
+#endif
 
 /* pass 1 to disable ctrlc() checking, 0 to enable.
  * returns previous state
index 86c0df02369c1234b385da33abb1d2694eb5fa01..a71fdf9f3c12a49a42651b9e63b7ffb4b591a685 100644 (file)
@@ -15,16 +15,16 @@ uint_fast8_t tstc(void);
 
 int my_getchar(uint_fast8_t waitforchar);
 
-/* test if ctrl-c was pressed */
+/* Test if ctrl-c was pressed */
 uint_fast8_t ctrlc(void);
 
-
-/* pass 1 to disable ctrlc() checking, 0 to enable.
- * returns previous state
- */
+/* Pass 1 to disable ctrlc() checking, 0 to enable. Returns previous state. */
 uint_fast8_t disable_ctrlc(uint_fast8_t disable);
 
 uint_fast8_t had_ctrlc (void);
 void clear_ctrlc(void);
 
+/* Reads user's confirmation. Returns 1 if user's input is "y", "Y", "yes" or "YES" */
+uint_fast8_t confirm_yes(void);
+
 #endif /* CON_UTILS_H */