Skip to content

Commit d35a289

Browse files
committed
Expand tilde in get_rename_dest_filename() itself
1 parent d8ce312 commit d35a289

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

src/file_operations.c

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ get_dup_file_dest_dir(void)
274274
char *name = file_info[n - 1].name;
275275
dir = savestring(name, strlen(name));
276276
}
277-
} else if (*dir == '~') { // Expand tilde
277+
} else if (*dir == '~') { /* Expand tilde */
278278
char *tmp = tilde_expand(dir);
279279
if (tmp) {
280280
free(dir);
@@ -1643,23 +1643,36 @@ get_rename_dest_filename(char *name, int *status)
16431643

16441644
/* Get destination filename. */
16451645
char *new_name = get_new_filename(name);
1646-
if (!new_name) {
1646+
if (!new_name) { /* The user pressed Ctrl+d */
16471647
*status = FUNC_SUCCESS;
16481648
return (char *)NULL;
16491649
}
16501650

1651+
if (*new_name == '~') {
1652+
p = tilde_expand(new_name);
1653+
if (!p) {
1654+
*status = FUNC_FAILURE;
1655+
xerror(_("m: '%s': Error expanding tilde\n"), new_name);
1656+
free(new_name);
1657+
return (char *)NULL;
1658+
} else {
1659+
free(new_name);
1660+
new_name = p;
1661+
}
1662+
}
1663+
16511664
return new_name;
16521665
}
16531666

16541667

16551668
/* Run CMD (either cp(1) or mv(1)) via execv().
16561669
* 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-interactivelly
1670+
* either 'c' or 'm' commands: it intructs cp/mv to run non-interactively
16581671
* (no -i). */
16591672
static int
16601673
run_cp_mv_cmd(char **cmd, const int skip_force, const size_t files_num)
16611674
{
1662-
if (!cmd)
1675+
if (!cmd || !cmd[0])
16631676
return FUNC_FAILURE;
16641677

16651678
char *new_name = (char *)NULL;
@@ -1714,13 +1727,7 @@ run_cp_mv_cmd(char **cmd, const int skip_force, const size_t files_num)
17141727
n++;
17151728
} else {
17161729
if (new_name) {
1717-
p = (char *)NULL;
1718-
if (*new_name == '~')
1719-
p = tilde_expand(new_name);
1720-
char *q = p ? p : new_name;
1721-
tcmd[n] = savestring(q, strlen(q));
1722-
free(new_name);
1723-
free(p);
1730+
tcmd[n] = new_name;
17241731
if (cwd == 0)
17251732
cwd = is_file_in_cwd(tcmd[n]);
17261733
n++;

0 commit comments

Comments
 (0)