Skip to content

Commit d8ce312

Browse files
committed
Move code to function
1 parent 5f80f9d commit d8ce312

File tree

1 file changed

+35
-35
lines changed

1 file changed

+35
-35
lines changed

src/file_operations.c

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1620,6 +1620,38 @@ print_cp_mv_summary_msg(const char *c, const size_t n, const int cwd)
16201620
print_reload_msg(SET_SUCCESS_PTR, xs_cb, _("%zu file(s) copied\n"), n);
16211621
}
16221622

1623+
static char *
1624+
get_rename_dest_filename(char *name, int *status)
1625+
{
1626+
if (!name || !*name) {
1627+
*status = EINVAL;
1628+
return (char *)NULL;
1629+
}
1630+
1631+
/* Check source file existence. */
1632+
struct stat a;
1633+
char *p = unescape_str(name, 0);
1634+
const int ret = lstat(p ? p : name, &a);
1635+
free(p);
1636+
1637+
if (ret == -1) {
1638+
*status = errno;
1639+
alt_prompt = 0;
1640+
xerror("m: '%s': %s\n", name, strerror(errno));
1641+
return (char *)NULL;
1642+
}
1643+
1644+
/* Get destination filename. */
1645+
char *new_name = get_new_filename(name);
1646+
if (!new_name) {
1647+
*status = FUNC_SUCCESS;
1648+
return (char *)NULL;
1649+
}
1650+
1651+
return new_name;
1652+
}
1653+
1654+
16231655
/* Run CMD (either cp(1) or mv(1)) via execv().
16241656
* skip_force is true (1) when the -f,--force parameter has been provided to
16251657
* either 'c' or 'm' commands: it intructs cp/mv to run non-interactivelly
@@ -1632,41 +1664,9 @@ run_cp_mv_cmd(char **cmd, const int skip_force, const size_t files_num)
16321664

16331665
char *new_name = (char *)NULL;
16341666
if (alt_prompt == FILES_PROMPT) { /* 'm' command (interactive rename) */
1635-
if (!cmd[1])
1636-
return EINVAL;
1637-
1638-
/* If we have a number, either it was not expanded by parse_input_str(),
1639-
* in which case it is an invalid ELN, or it was expanded to a file
1640-
* named as a number. Let's check if we have such filename in the
1641-
* file list. */
1642-
if (is_number(cmd[1])) {
1643-
filesn_t i = files;
1644-
while (--i >= 0) {
1645-
if (*cmd[1] != *file_info[i].name)
1646-
continue;
1647-
if (strcmp(cmd[1], file_info[i].name) == 0)
1648-
break;
1649-
}
1650-
if (i == -1) {
1651-
xerror(_("%s: %s: No such ELN\n"), PROGRAM_NAME, cmd[1]);
1652-
alt_prompt = 0;
1653-
return ENOENT;
1654-
}
1655-
} else {
1656-
char *p = unescape_str(cmd[1], 0);
1657-
struct stat a;
1658-
const int ret = lstat(p ? p : cmd[1], &a);
1659-
free(p);
1660-
if (ret == -1) {
1661-
alt_prompt = 0;
1662-
xerror("m: %s: %s\n", cmd[1], strerror(errno));
1663-
return errno;
1664-
}
1665-
}
1666-
1667-
new_name = get_new_filename(cmd[1]);
1668-
if (!new_name)
1669-
return FUNC_SUCCESS;
1667+
int status = 0;
1668+
if (!(new_name = get_rename_dest_filename(cmd[1], &status)))
1669+
return status;
16701670
}
16711671

16721672
char **tcmd = xnmalloc(3 + args_n + 2, sizeof(char *));

0 commit comments

Comments
 (0)