Fixed mv so it now does the right thing (same method used in cp). Removed
[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
44     strcpy(newdestName, destName);
45     if ( srcDirFlag == TRUE ) {
46         strcat(newdestName, strstr(fileName, srcName) + strlen(srcName));
47     } 
48     
49     if (destDirFlag==TRUE && srcDirFlag == FALSE) {
50         if (newdestName[strlen(newdestName)-1] != '/' ) {
51             strcat(newdestName, "/");
52         }
53         strcat(newdestName, srcName);
54     }
55     
56     return (copyFile(fileName, newdestName, TRUE, TRUE));
57 }
58
59 static int rmfileAction(const char *fileName, struct stat* statbuf)
60 {
61     if (unlink( fileName) < 0 ) {
62         perror( fileName);
63         return ( FALSE);
64     }
65     return ( TRUE);
66 }
67
68 static int rmdirAction(const char *fileName, struct stat* statbuf)
69 {
70     if (rmdir( fileName) < 0 ) {
71         perror( fileName);
72         return ( FALSE);
73     }
74     return ( TRUE);
75 }
76
77
78 extern int mv_main(int argc, char **argv)
79 {
80     if (argc < 3) {
81         usage (mv_usage);
82     }
83     argc--;
84     argv++;
85
86     destName = argv[argc - 1];
87     destDirFlag = isDirectory(destName);
88
89     if ((argc > 3) && destDirFlag==FALSE) {
90         fprintf(stderr, "%s: not a directory\n", destName);
91         exit (FALSE);
92     }
93
94     while (argc-- > 1) {
95         srcName = *(argv++);
96         srcDirFlag = isDirectory(srcName);
97         if (recursiveAction(srcName, TRUE, TRUE, FALSE,
98                                fileAction, fileAction) == FALSE) {
99             exit( FALSE);
100         }
101         if (recursiveAction(srcName, TRUE, TRUE, TRUE,
102                                rmfileAction, rmdirAction) == FALSE) {
103             exit( FALSE);
104         }
105     }
106     exit( TRUE);
107 }