Skip to content

Commit 0ec1c54

Browse files
committed
Refactor copy/move functions
1 parent ae8615b commit 0ec1c54

File tree

3 files changed

+72
-148
lines changed

3 files changed

+72
-148
lines changed

src/exec.c

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1913,43 +1913,32 @@ toggle_follow_links(const char *arg)
19131913
static int
19141914
handle_copy_move_cmds(char ***cmd)
19151915
{
1916-
int copy_and_rename = 0;
1917-
int use_force = 0;
1918-
char **args = *cmd;
1919-
1920-
if (args[0][0] != 'm') { // Either c, vv, or paste commands
1921-
if (args[1] && IS_HELP(args[1])) {
1922-
if (args[0][1] == 'v')
1923-
puts(_(VV_USAGE));
1924-
else
1925-
puts(_(WRAPPERS_USAGE));
1926-
return (-1);
1927-
}
1928-
1929-
if (args[0][1]== 'v')
1930-
copy_and_rename = 1;
1931-
1932-
use_force = is_force_param(args[1]);
1933-
set_cp_cmd(&args[0], &use_force);
1934-
1935-
} else { /* The m command */
1936-
if (args[1] && IS_HELP(args[1])) {
1937-
puts(_(WRAPPERS_USAGE));
1938-
return (-1);
1939-
}
1940-
1941-
if (sel_is_last == 0 && args[1] && !args[2])
1942-
alt_prompt = FILES_PROMPT; /* Interactive rename */
1943-
1944-
use_force = is_force_param(args[1]);
1945-
set_mv_cmd(&args[0], &use_force);
1916+
char **args = *cmd;
1917+
if (!args || !args[0])
1918+
return (-1);
1919+
1920+
int copy_and_rename = 0;
1921+
int use_force = args[1] ? is_force_param(args[1]) : 0;
1922+
1923+
if (args[1] && IS_HELP(args[1])) {
1924+
puts(args[0][1] == 'v' ? _(VV_USAGE) : _(WRAPPERS_USAGE));
1925+
return (-1);
1926+
}
1927+
1928+
if (args[0][0] != 'm') { /* Either c, vv, or paste commands. */
1929+
copy_and_rename = (args[0][1] == 'v'); /* vv command. */
1930+
set_cp_cmd(&args[0], &use_force);
1931+
} else { /* The m command. */
1932+
if (sel_is_last == 0 && args[1] && !args[2])
1933+
alt_prompt = FILES_PROMPT; /* Interactive rename. */
1934+
set_mv_cmd(&args[0], &use_force);
19461935
}
19471936

1948-
kbind_busy = 1;
1949-
exit_code = cp_mv_file(args, copy_and_rename, use_force);
1950-
kbind_busy = 0;
1937+
kbind_busy = 1;
1938+
exit_code = cp_mv_file(args, copy_and_rename, use_force);
1939+
kbind_busy = 0;
19511940

1952-
return 0;
1941+
return 0;
19531942
}
19541943

19551944
/* Take the command entered by the user, already splitted into substrings

src/file_operations.c

Lines changed: 49 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1608,7 +1608,7 @@ cwd_has_sel_files(void)
16081608
#define IS_MVCMD(s) (*(s) == 'm' || (*(s) == 'a' \
16091609
&& strncmp((s), "advmv", 5) == 0))
16101610

1611-
static void
1611+
static int
16121612
print_cp_mv_summary_msg(const char *c, const size_t n, const int cwd)
16131613
{
16141614
if (conf.autols == 1 && cwd == 1)
@@ -1618,6 +1618,8 @@ print_cp_mv_summary_msg(const char *c, const size_t n, const int cwd)
16181618
print_reload_msg(SET_SUCCESS_PTR, xs_cb, _("%zu file(s) moved\n"), n);
16191619
else
16201620
print_reload_msg(SET_SUCCESS_PTR, xs_cb, _("%zu file(s) copied\n"), n);
1621+
1622+
return FUNC_SUCCESS;
16211623
}
16221624

16231625
static char *
@@ -1651,26 +1653,12 @@ get_rename_dest_filename(char *name, int *status)
16511653
return new_name;
16521654
}
16531655

1654-
1655-
/* Run CMD (either cp(1) or mv(1)) via execv().
1656-
* skip_force is true (1) when the -f,--force parameter has been provided to
1657-
* either 'c' or 'm' commands: it intructs cp/mv to run non-interactively
1658-
* (no -i). */
1659-
static int
1660-
run_cp_mv_cmd(char **cmd, const int skip_force, const size_t files_num)
1656+
static char **
1657+
construct_cp_mv_cmd(char **cmd, char *new_name, int *cwd, const size_t force_param)
16611658
{
1662-
if (!cmd || !cmd[0])
1663-
return FUNC_FAILURE;
1664-
1665-
char *new_name = (char *)NULL;
1666-
if (alt_prompt == FILES_PROMPT) { /* 'm' command (interactive rename) */
1667-
int status = 0;
1668-
if (!(new_name = get_rename_dest_filename(cmd[1], &status)))
1669-
return status;
1670-
}
1671-
1672-
char **tcmd = xnmalloc(3 + args_n + 2, sizeof(char *));
16731659
size_t n = 0;
1660+
char **tcmd = xnmalloc(3 + args_n + 2, sizeof(char *));
1661+
16741662
char *p = strchr(cmd[0], ' ');
16751663
if (p && p[1]) {
16761664
*p = '\0';
@@ -1684,62 +1672,39 @@ run_cp_mv_cmd(char **cmd, const int skip_force, const size_t files_num)
16841672
}
16851673

16861674
/* wcp(1) does not support end of options (--). */
1687-
if (strcmp(cmd[0], "wcp") != 0) {
1675+
if (*tcmd[0] != 'w' || strcmp(tcmd[0], "wcp") != 0) {
16881676
tcmd[n] = savestring("--", 2);
16891677
n++;
16901678
}
16911679

1692-
int cwd = 0;
1693-
size_t i;
1694-
for (i = 1; cmd[i]; i++) {
1695-
/* The -f,--force parameter is internal. Skip it.
1696-
* It instructs cp/mv to run non-interactively (no -i param). */
1697-
if (!*cmd[i] || (skip_force == 1 && i == 1
1698-
&& is_force_param(cmd[i]) == 1))
1699-
continue;
1700-
p = unescape_str(cmd[i], 0);
1701-
if (!p)
1680+
/* The -f,--force parameter is internal. Skip it.
1681+
* It instructs cp/mv to skip confirmation prompts. */
1682+
size_t i = force_param == 1 ? 2 : 1;
1683+
1684+
for (; cmd[i]; i++) {
1685+
if (!*cmd[i] || !(p = unescape_str(cmd[i], 0)))
17021686
continue;
17031687
tcmd[n] = savestring(p, strlen(p));
17041688
free(p);
1705-
if (cwd == 0)
1706-
cwd = is_file_in_cwd(tcmd[n]);
1689+
if (*cwd == 0)
1690+
*cwd = is_file_in_cwd(tcmd[n]);
17071691
n++;
17081692
}
17091693

1710-
if (cmd[1] && !cmd[2] && *cmd[0] == 'c' && cmd[0][1] == 'p'
1711-
&& cmd[0][2] == ' ') {
1712-
tcmd[n][0] = '.';
1713-
tcmd[n][1] = '\0';
1694+
/* Append extra parameters as required. */
1695+
if (is_sel == 1 && sel_is_last == 1) { /* E.g., "m sel" */
1696+
tcmd[n] = savestring(".", 1);
1697+
*cwd = 1;
1698+
n++;
1699+
} else if (new_name) { /* Interactive rename: "m FILE" */
1700+
tcmd[n] = new_name;
1701+
if (*cwd == 0)
1702+
*cwd = is_file_in_cwd(tcmd[n]);
17141703
n++;
1715-
} else {
1716-
if (new_name) {
1717-
tcmd[n] = new_name;
1718-
if (cwd == 0)
1719-
cwd = is_file_in_cwd(tcmd[n]);
1720-
n++;
1721-
}
17221704
}
17231705

17241706
tcmd[n] = (char *)NULL;
1725-
const int ret = launch_execv(tcmd, FOREGROUND, E_NOFLAG);
1726-
1727-
for (i = 0; i < n; i++)
1728-
free(tcmd[i]);
1729-
free(tcmd);
1730-
1731-
if (ret != FUNC_SUCCESS)
1732-
return ret;
1733-
1734-
/* Error messages are printed by launch_execv() itself. */
1735-
1736-
if (sel_n > 0 && IS_MVCMD(cmd[0]) && cwd_has_sel_files())
1737-
/* Just in case a selected file in the current dir was renamed. */
1738-
get_sel_files();
1739-
1740-
print_cp_mv_summary_msg(cmd[0], files_num, cwd);
1741-
1742-
return FUNC_SUCCESS;
1707+
return tcmd;
17431708
}
17441709

17451710
static int
@@ -1849,13 +1814,21 @@ cp_mv_file(char **args, const int copy_and_rename, const int force)
18491814
&& (ret = validate_vv_dest_dir(args[args_n])) != FUNC_SUCCESS)
18501815
return ret == -1 ? FUNC_SUCCESS : FUNC_FAILURE;
18511816

1817+
/* m command */
1818+
char *new_name = (char *)NULL;
18521819
if (IS_MVCMD(args[0]) && args[1]) {
18531820
const size_t len = strlen(args[1]);
18541821
if (len > 0 && args[1][len - 1] == '/')
18551822
args[1][len - 1] = '\0';
1823+
1824+
if (alt_prompt == FILES_PROMPT) { /* Interactive rename. */
1825+
int status = 0;
1826+
if (!(new_name = get_rename_dest_filename(args[1], &status)))
1827+
return status;
1828+
}
18561829
}
18571830

1858-
/* rsync won't copy directories with a trailing slash. Remove it. */
1831+
/* rsync(1) won't copy directories with a trailing slash. Remove it. */
18591832
if (*args[0] == 'r' && args[1])
18601833
remove_dirslash_from_source(args);
18611834

@@ -1866,51 +1839,14 @@ cp_mv_file(char **args, const int copy_and_rename, const int force)
18661839
const size_t files_num =
18671840
args_n - (args_n > 1 && sel_is_last == 0) - skipped - force_param;
18681841

1869-
if (is_sel == 0 && copy_and_rename == 0)
1870-
return run_cp_mv_cmd(args, force, files_num);
1871-
1872-
size_t n = 0;
1873-
char **tcmd = xnmalloc(3 + args_n + 2, sizeof(char *));
1874-
char *p = strchr(args[0], ' ');
1875-
if (p && p[1]) {
1876-
*p = '\0';
1877-
p++;
1878-
tcmd[0] = savestring(args[0], strlen(args[0]));
1879-
tcmd[1] = savestring(p, strlen(p));
1880-
n += 2;
1881-
} else {
1882-
tcmd[0] = savestring(args[0], strlen(args[0]));
1883-
n++;
1884-
}
1885-
1886-
/* wcp(1) does not support end of options (--) */
1887-
if (*tcmd[0] == 'w' && strcmp(tcmd[0], "wcp") != 0) {
1888-
tcmd[n] = savestring("--" , 2);
1889-
n++;
1890-
}
1891-
18921842
int cwd = 0;
1893-
size_t i = force_param == 1 ? 2 : 1;
1894-
for (; args[i]; i++) {
1895-
if (!*args[i] || !(p = unescape_str(args[i], 0)))
1896-
continue;
1897-
tcmd[n] = savestring(p, strlen(p));
1898-
if (cwd == 0)
1899-
cwd = is_file_in_cwd(tcmd[n]);
1900-
free(p);
1901-
n++;
1902-
}
1903-
1904-
if (sel_is_last == 1) {
1905-
tcmd[n] = savestring(".", 1);
1906-
cwd = 1;
1907-
n++;
1908-
}
1909-
1910-
tcmd[n] = (char *)NULL;
1843+
char **tcmd = construct_cp_mv_cmd(args, new_name, &cwd, force_param);
1844+
if (!tcmd)
1845+
return FUNC_FAILURE;
19111846

19121847
ret = launch_execv(tcmd, FOREGROUND, E_NOFLAG);
19131848

1849+
size_t i;
19141850
for (i = 0; tcmd[i]; i++)
19151851
free(tcmd[i]);
19161852
free(tcmd);
@@ -1921,14 +1857,17 @@ cp_mv_file(char **args, const int copy_and_rename, const int force)
19211857
if (copy_and_rename == 1) /* vv command */
19221858
return vv_rename_files(args, files_num);
19231859

1924-
/* If 'mv sel' and command is successful deselect everything:
1925-
* selected files are not there anymore. */
1926-
if (is_sel == 1 && sel_n > 0 && IS_MVCMD(args[0]))
1927-
deselect_all();
1928-
1929-
print_cp_mv_summary_msg(args[0], files_num, cwd);
1860+
if (sel_n > 0 && IS_MVCMD(args[0])) {
1861+
if (is_sel == 1)
1862+
/* If 'mv sel' and command is successful deselect everything:
1863+
* selected files are not there anymore. */
1864+
deselect_all();
1865+
else if (cwd_has_sel_files())
1866+
/* Just in case a selected file in the current dir was renamed. */
1867+
get_sel_files();
1868+
}
19301869

1931-
return FUNC_SUCCESS;
1870+
return print_cp_mv_summary_msg(args[0], files_num, cwd);
19321871
}
19331872
#undef IS_MVCMD
19341873

src/settings.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -621,19 +621,15 @@ ws2=31:ws3=38;5;228:ws4=32:ws5=36:ws6=38;5;214:ws7=35:ws8=2;37:xf=1;31:xs=32:"
621621

622622
/* cp and mv commands
623623
* All options used for cp(1) and mv(1) are POSIX. No need to check. */
624-
//#define DEFAULT_CP_CMD "cp -iRp"
625624
#define DEFAULT_CP_CMD "cp -Rp"
626625
#define DEFAULT_CP_CMD_FORCE "cp -Rp"
627-
//#define DEFAULT_ADVCP_CMD "advcp -giRp"
628626
#define DEFAULT_ADVCP_CMD "advcp -gRp"
629627
#define DEFAULT_ADVCP_CMD_FORCE "advcp -gRp"
630628
#define DEFAULT_WCP_CMD "wcp"
631629
#define DEFAULT_RSYNC_CMD "rsync -avP"
632630

633-
//#define DEFAULT_MV_CMD "mv -i"
634631
#define DEFAULT_MV_CMD "mv"
635632
#define DEFAULT_MV_CMD_FORCE "mv"
636-
//#define DEFAULT_ADVMV_CMD "advmv -gi"
637633
#define DEFAULT_ADVMV_CMD "advmv -g"
638634
#define DEFAULT_ADVMV_CMD_FORCE "advmv -g"
639635

0 commit comments

Comments
 (0)