#include "env.h"
#include "getopt-min.h"
+
+#define DEBUG_CP 0 /* set to 1 to debug */
+#define DEBUG_LS 0 /* set to 1 to debug */
+
+#define debug_cp(fmt, args...) \
+ debug_cond(DEBUG_CP, fmt, ##args)
+#define debug_ls(fmt, args...) \
+ debug_cond(DEBUG_LS, fmt, ##args)
+
+
/* TODO: use memory size test function (detect_ramsize() in cmd_loadihex.c) */
/* TODO: detect_ramsize() should be moved to z80-if.c */
#define MAX_MEMORY CONFIG_SYS_RAMSIZE_MAX
#define BUFFER_SIZE 512
-#define MAXBSIZE 512 /* TODO */
/*
pattern = strdup("*");
strip_trailing_slash_relpath(path);
-//printf_P(PSTR("*: |%s| |%s|\n"), path ? path : "<NULL>", pattern ? pattern : "<NULL>");
+debug_ls("==== path: '%s', pattern: '%s'\n", path ? path : "<NULL>", pattern ? pattern : "<NULL>");
p1 = s1 = s2 = 0;
res = f_findfirst(&Dir, &Finfo, path, pattern); /* Start to search for files */
char * buf = (char *) malloc(BUFFER_SIZE);
if (buf == NULL) {
- printf_P(PSTR("pwd: Out of Memory!\n"));
+ printf_P(PSTR("tst: Out of Memory!\n"));
free(buf);
return CMD_RET_FAILURE;
}
res = f_getcwd(buf, BUFFER_SIZE); /* Get current directory path */
if (!res) {
- printf_P(PSTR("cwd: |%s|\n"), buf);
+ printf_P(PSTR("cwd: '%s'\n"), buf);
}
free(buf);
if (res) {
else
path = "";
- printf_P(PSTR("arg: |%s|\n"), path);
+ printf_P(PSTR("arg: '%s'\n"), path);
printf_P(PSTR("==== f_stat: "));
res = f_stat(path, &Finfo);
put_rc(res);
static void strip_trailing_slash(PATH_T *p)
{
- while (p->p_end > p->p_path && p->p_end[-1] == '/')
- *--p->p_end = '\0';
+ char *beg = p->p_path;
+ char *end = p->p_end;
+ if ((beg + 2) < end && (*beg & 0x38) == '0' && *(beg+1) == ':')
+ beg += 2;
+ if (beg < end && *beg == '/')
+ ++beg;
+ while (end > beg && end[-1] == '/')
+ *--end = '\0';
+
+ p->p_end =end;
}
/*
#define V_FLAG (1<<5) // explain what is being done
static void
-setfile(FILINFO *fs, FIL *fd)
+setfile(FILINFO *fs)
{
- (void) fs;(void) fd;
-#if 0 /* TODO: */
- static struct timeval tv[2];
+ FRESULT fr;
- fs->st_mode &= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
+ fr = f_utime(to->p_path, fs);
+ if (fr != FR_OK)
+ err(PSTR("f_utime: %s: %S"), to->p_path, rctostr(fr));
+ fr = f_chmod(to->p_path, fs->fattrib, AM_RDO|AM_ARC|AM_SYS|AM_HID);
+ if (fr != FR_OK)
+ err(PSTR("f_chmod: %s: %S"), to->p_path, rctostr(fr));
- tv[0].tv_sec = fs->st_atime;
- tv[1].tv_sec = fs->st_mtime;
- if (utimes(to->p_path, tv))
- err(PSTR("utimes: %s: %s"), to->p_path, strerror(errno));
-#endif
}
void copy_file(FILINFO *fs, uint_fast8_t dne)
{
FIL from_fd, to_fd;
UINT rcount, wcount;
- //FILINFO to_stat;
- //char *p;
FRESULT fr;
+ BYTE open_mode;
if (blockbuf == NULL) {
blockbuf_size = get_freemem() / 512 * 512;
}
}
-debug("==== copy_file(): dne: %u, blockbuf_size: %d\n", dne, blockbuf_size);
-debug(" from:'%s' to:'%s'\n", from->p_path, to->p_path);
+debug_cp("==== copy_file(): dne: %u, blockbuf_size: %d\n", dne, blockbuf_size);
+debug_cp(" from:'%s' to:'%s'\n", from->p_path, to->p_path);
if ((fr = f_open(&from_fd, from->p_path, FA_READ)) != FR_OK) {
/*
* If the file exists and we're interactive, verify with the user.
- * If the file DNE, set the mode to be the from file, minus setuid
- * bits, modified by the umask; arguably wrong, but it makes copying
- * executables work right and it's been that way forever. (The
- * other choice is 666 or'ed with the execute bits on the from file
- * modified by the umask.)
*/
if (!dne) {
- if (flags & I_FLAG) {
+ if (flags & N_FLAG) {
+ if (flags & V_FLAG)
+ printf_P(PSTR("%s not overwritten\n"), to->p_path);
+ f_close(&from_fd);
+ return;
+ } if (flags & I_FLAG) {
printf_P(PSTR("overwrite '%s'? "), to->p_path);
if (!confirm_yes()) {
f_close(&from_fd);
return;
}
}
- fr = f_open(&to_fd, to->p_path, FA_WRITE|FA_CREATE_ALWAYS);
- } else
- fr = f_open(&to_fd, to->p_path, FA_WRITE|FA_CREATE_NEW);
+ if (flags & F_FLAG) {
+ /* Remove existing destination file name create a new file. */
+ f_chmod(to->p_path,0, AM_RDO);
+ f_unlink(to->p_path);
+ open_mode = FA_WRITE|FA_CREATE_NEW;
+ } else {
+ /* Overwrite existing destination file name. */
+ open_mode = FA_WRITE|FA_CREATE_ALWAYS;
+ }
+ } else {
+ open_mode = FA_WRITE|FA_CREATE_NEW;
+ }
+ fr = f_open(&to_fd, to->p_path, open_mode);
if (fr != FR_OK) {
err(PSTR("%s: %S"), to->p_path, rctostr(fr));
if (fr != FR_OK)
err(PSTR("%s: S"), from->p_path, rctostr(fr));
- if (flags & P_FLAG)
- setfile(fs, &to_fd);
-
f_close(&from_fd);
if ((fr = f_close(&to_fd)) != FR_OK)
err(PSTR("%s: %S"), to->p_path, rctostr(fr));
+ if (flags & P_FLAG)
+ setfile(fs);
}
#if 1
static void copy_dir(void)
{
-debug("==== copy_dir()");
-debug(" from:'%s' to:'%s'\n", from->p_path, to->p_path);
+debug_cp("==== copy_dir()");
+debug_cp(" from:'%s' to:'%s'\n", from->p_path, to->p_path);
printf_P(PSTR("directory copy not supported, ommitting dir '%s'\n"),
from->p_path);
int dir_cnt, i;
char *old_from, *old_to;
-debug("==== copy_file(): dne: %u\n", dne);
-debug(" from:'%s' to:'%s'\n", from->p_path, to->p_path);
+debug_cp("==== copy_file(): dne: %u\n", dne);
+debug_cp(" from:'%s' to:'%s'\n", from->p_path, to->p_path);
dir_cnt = scandir(from->p_path, &dir_list, NULL, NULL);
if (dir_cnt == -1) {
uint_fast8_t dne;
FRESULT fr;
-debug("==== copy()\n");
-debug(" from:'%s' to:'%s'\n", from->p_path, to->p_path);
+debug_cp("==== copy()\n");
+debug_cp(" from:'%s' to:'%s'\n", from->p_path, to->p_path);
fr = f_stat(from->p_path, &from_stat);
if (fr != FR_OK) {
}
copy_dir();
if (flags & P_FLAG)
- setfile(&from_stat, 0);
+ setfile(&from_stat);
return;
}
optind = 0;
int opt;
-// while ((opt = getopt(argc, argv, PSTR("fiprv"))) != -1) {
- while ((opt = getopt(argc, argv, PSTR("fiprv"))) != -1) {
+ while ((opt = getopt(argc, argv, PSTR("finprv"))) != -1) {
switch (opt) {
case 'f':
tflags &= ~(I_FLAG | N_FLAG);
from->p_end = from->p_path; *from->p_path = '\0';
to->p_end = to->p_path; *to->p_path = '\0';
- /* consume last argument first. */
- if (!path_set(to, argv[--argc])) {
- command_ret = CMD_RET_FAILURE;
+ /* last argument is destination */
+ if (!path_set(to, argv[--argc]))
goto cleanup;
- }
/*
* Cp has two distinct cases:
*/
fr = f_stat(to->p_path, &to_stat);
-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);
+debug_cp("==== main, stat to: fr: %d, attr: %02x, flags:%02x\n", fr, to_stat.fattrib, flags);
+debug_cp(" from:'%s' to:'%s'\n", from->p_path, to->p_path);
if (fr != FR_OK && fr != FR_NO_FILE && fr != FR_NO_PATH) {
err(PSTR("Test1: %s: %S"), to->p_path, rctostr(fr));