Fix cp and mv so 'cp foo/README bar' where foo and bar are directories,
[oweals/busybox.git] / coreutils / mv.c
1 /*
2  * Mini mv implementation for busybox
3  *
4  *
5  * Copyright (C) 1999 by Lineo, inc.
6  * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  *
22  */
23
24 #include "internal.h"
25 #include <stdio.h>
26 #include <time.h>
27 #include <utime.h>
28 #include <dirent.h>
29
30 static const char mv_usage[] = "mv SOURCE DEST\n"
31 "   or: mv SOURCE... DIRECTORY\n\n"
32 "Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.\n";
33
34
35 static const char *srcName;
36 static const char *destName;
37 static int destDirFlag = FALSE;
38 static int srcDirFlag = FALSE;
39
40 static int fileAction(const char *fileName, struct stat* statbuf)
41 {
42     char newdestName[NAME_MAX];
43     char* newsrcName = NULL;
44
45     strcpy(newdestName, destName);
46     if ( srcDirFlag == TRUE ) {
47         strcat(newdestName, strstr(fileName, srcName) + strlen(srcName));
48     } 
49     
50     if (destDirFlag==TRUE && srcDirFlag == FALSE) {
51         if (newdestName[strlen(newdestName)-1] != '/' ) {
52             strcat(newdestName, "/");
53         }
54         newsrcName = strrchr(srcName, '/');
55         if (newsrcName && *newsrcName != '\0')
56             strcat(newdestName, newsrcName);
57         else
58             strcat(newdestName, srcName);
59     }
60     
61     return (copyFile(fileName, newdestName, TRUE, TRUE));
62 }
63
64 static int rmfileAction(const char *fileName, struct stat* statbuf)
65 {
66     if (unlink( fileName) < 0 ) {
67         perror( fileName);
68         return ( FALSE);
69     }
70     return ( TRUE);
71 }
72
73 static int rmdirAction(const char *fileName, struct stat* statbuf)
74 {
75     if (rmdir( fileName) < 0 ) {
76         perror( fileName);
77         return ( FALSE);
78     }
79     return ( TRUE);
80 }
81
82
83 extern int mv_main(int argc, char **argv)
84 {
85     if (argc < 3) {
86         usage (mv_usage);
87     }
88     argc--;
89     argv++;
90
91     destName = argv[argc - 1];
92     destDirFlag = isDirectory(destName);
93
94     if ((argc > 3) && destDirFlag==FALSE) {
95         fprintf(stderr, "%s: not a directory\n", destName);
96         exit (FALSE);
97     }
98
99     while (argc-- > 1) {
100         srcName = *(argv++);
101         srcDirFlag = isDirectory(srcName);
102         if (recursiveAction(srcName, TRUE, TRUE, FALSE,
103                                fileAction, fileAction) == FALSE) {
104             exit( FALSE);
105         }
106         if (recursiveAction(srcName, TRUE, TRUE, TRUE,
107                                rmfileAction, rmdirAction) == FALSE) {
108             exit( FALSE);
109         }
110     }
111     exit( TRUE);
112 }