@@ -1608,7 +1608,7 @@ cwd_has_sel_files(void)
1608
1608
#define IS_MVCMD (s ) (*(s) == 'm' || (*(s) == 'a' \
1609
1609
&& strncmp((s), "advmv", 5) == 0))
1610
1610
1611
- static void
1611
+ static int
1612
1612
print_cp_mv_summary_msg (const char * c , const size_t n , const int cwd )
1613
1613
{
1614
1614
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)
1618
1618
print_reload_msg (SET_SUCCESS_PTR , xs_cb , _ ("%zu file(s) moved\n" ), n );
1619
1619
else
1620
1620
print_reload_msg (SET_SUCCESS_PTR , xs_cb , _ ("%zu file(s) copied\n" ), n );
1621
+
1622
+ return FUNC_SUCCESS ;
1621
1623
}
1622
1624
1623
1625
static char *
@@ -1651,26 +1653,12 @@ get_rename_dest_filename(char *name, int *status)
1651
1653
return new_name ;
1652
1654
}
1653
1655
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 )
1661
1658
{
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 * ));
1673
1659
size_t n = 0 ;
1660
+ char * * tcmd = xnmalloc (3 + args_n + 2 , sizeof (char * ));
1661
+
1674
1662
char * p = strchr (cmd [0 ], ' ' );
1675
1663
if (p && p [1 ]) {
1676
1664
* p = '\0' ;
@@ -1684,62 +1672,39 @@ run_cp_mv_cmd(char **cmd, const int skip_force, const size_t files_num)
1684
1672
}
1685
1673
1686
1674
/* 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 ) {
1688
1676
tcmd [n ] = savestring ("--" , 2 );
1689
1677
n ++ ;
1690
1678
}
1691
1679
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 )))
1702
1686
continue ;
1703
1687
tcmd [n ] = savestring (p , strlen (p ));
1704
1688
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 ]);
1707
1691
n ++ ;
1708
1692
}
1709
1693
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 ]);
1714
1703
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
- }
1722
1704
}
1723
1705
1724
1706
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 ;
1743
1708
}
1744
1709
1745
1710
static int
@@ -1849,13 +1814,21 @@ cp_mv_file(char **args, const int copy_and_rename, const int force)
1849
1814
&& (ret = validate_vv_dest_dir (args [args_n ])) != FUNC_SUCCESS )
1850
1815
return ret == -1 ? FUNC_SUCCESS : FUNC_FAILURE ;
1851
1816
1817
+ /* m command */
1818
+ char * new_name = (char * )NULL ;
1852
1819
if (IS_MVCMD (args [0 ]) && args [1 ]) {
1853
1820
const size_t len = strlen (args [1 ]);
1854
1821
if (len > 0 && args [1 ][len - 1 ] == '/' )
1855
1822
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
+ }
1856
1829
}
1857
1830
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. */
1859
1832
if (* args [0 ] == 'r' && args [1 ])
1860
1833
remove_dirslash_from_source (args );
1861
1834
@@ -1866,51 +1839,14 @@ cp_mv_file(char **args, const int copy_and_rename, const int force)
1866
1839
const size_t files_num =
1867
1840
args_n - (args_n > 1 && sel_is_last == 0 ) - skipped - force_param ;
1868
1841
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
-
1892
1842
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 ;
1911
1846
1912
1847
ret = launch_execv (tcmd , FOREGROUND , E_NOFLAG );
1913
1848
1849
+ size_t i ;
1914
1850
for (i = 0 ; tcmd [i ]; i ++ )
1915
1851
free (tcmd [i ]);
1916
1852
free (tcmd );
@@ -1921,14 +1857,17 @@ cp_mv_file(char **args, const int copy_and_rename, const int force)
1921
1857
if (copy_and_rename == 1 ) /* vv command */
1922
1858
return vv_rename_files (args , files_num );
1923
1859
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
+ }
1930
1869
1931
- return FUNC_SUCCESS ;
1870
+ return print_cp_mv_summary_msg ( args [ 0 ], files_num , cwd ) ;
1932
1871
}
1933
1872
#undef IS_MVCMD
1934
1873
0 commit comments