blob: 467a360dea59121875842a39fbfbc88b07c868ab [file] [log] [blame]
Eric Andersen596e5461999-10-07 08:30:23 +00001/*
2 * Mini mv implementation for busybox
3 *
Eric Andersenc4996011999-10-20 22:08:37 +00004 *
5 * Copyright (C) 1999 by Lineo, inc.
6 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
Eric Andersen596e5461999-10-07 08:30:23 +00007 *
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
Eric Andersencc8ed391999-10-05 16:24:54 +000024#include "internal.h"
25#include <stdio.h>
Eric Andersenbed30e91999-10-18 19:02:32 +000026#include <time.h>
Eric Andersen596e5461999-10-07 08:30:23 +000027#include <utime.h>
Eric Andersenbed30e91999-10-18 19:02:32 +000028#include <dirent.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000029
Eric Andersenbed30e91999-10-18 19:02:32 +000030static const char mv_usage[] = "mv SOURCE DEST\n"
Eric Andersend73dc5b1999-11-10 23:13:02 +000031" or: mv SOURCE... DIRECTORY\n\n"
Eric Andersenbed30e91999-10-18 19:02:32 +000032"Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.\n";
Eric Andersen596e5461999-10-07 08:30:23 +000033
Eric Andersenbed30e91999-10-18 19:02:32 +000034
35static const char *srcName;
36static const char *destName;
Erik Andersenf664c001999-12-29 02:36:29 +000037static int destDirFlag = FALSE;
38static int srcDirFlag = FALSE;
Eric Andersenbed30e91999-10-18 19:02:32 +000039
Erik Andersen2fe08c71999-12-29 02:10:35 +000040static int fileAction(const char *fileName, struct stat* statbuf)
41{
42 char newdestName[NAME_MAX];
Erik Andersen1dbc17f2000-01-08 21:16:29 +000043 char* newsrcName = NULL;
Erik Andersen2fe08c71999-12-29 02:10:35 +000044
Erik Andersen2fe08c71999-12-29 02:10:35 +000045 strcpy(newdestName, destName);
Erik Andersenf664c001999-12-29 02:36:29 +000046 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 }
Erik Andersen1dbc17f2000-01-08 21:16:29 +000054 newsrcName = strrchr(srcName, '/');
55 if (newsrcName && *newsrcName != '\0')
56 strcat(newdestName, newsrcName);
57 else
58 strcat(newdestName, srcName);
Erik Andersenf664c001999-12-29 02:36:29 +000059 }
60
Erik Andersen2fe08c71999-12-29 02:10:35 +000061 return (copyFile(fileName, newdestName, TRUE, TRUE));
62}
63
Erik Andersenf664c001999-12-29 02:36:29 +000064static 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
73static 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
Eric Andersenbed30e91999-10-18 19:02:32 +000082
83extern int mv_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000084{
Eric Andersen596e5461999-10-07 08:30:23 +000085 if (argc < 3) {
Eric Andersenb0e9a701999-10-18 22:28:26 +000086 usage (mv_usage);
Eric Andersen596e5461999-10-07 08:30:23 +000087 }
Eric Andersenbed30e91999-10-18 19:02:32 +000088 argc--;
89 argv++;
Eric Andersen596e5461999-10-07 08:30:23 +000090
Eric Andersenbed30e91999-10-18 19:02:32 +000091 destName = argv[argc - 1];
Erik Andersenf664c001999-12-29 02:36:29 +000092 destDirFlag = isDirectory(destName);
Eric Andersen596e5461999-10-07 08:30:23 +000093
Erik Andersenf664c001999-12-29 02:36:29 +000094 if ((argc > 3) && destDirFlag==FALSE) {
Eric Andersenbed30e91999-10-18 19:02:32 +000095 fprintf(stderr, "%s: not a directory\n", destName);
Eric Andersen9b587181999-10-17 05:43:39 +000096 exit (FALSE);
Eric Andersen596e5461999-10-07 08:30:23 +000097 }
Erik Andersenf664c001999-12-29 02:36:29 +000098
Eric Andersenbed30e91999-10-18 19:02:32 +000099 while (argc-- > 1) {
100 srcName = *(argv++);
Erik Andersenf664c001999-12-29 02:36:29 +0000101 srcDirFlag = isDirectory(srcName);
102 if (recursiveAction(srcName, TRUE, TRUE, FALSE,
103 fileAction, fileAction) == FALSE) {
104 exit( FALSE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000105 }
Erik Andersenf664c001999-12-29 02:36:29 +0000106 if (recursiveAction(srcName, TRUE, TRUE, TRUE,
107 rmfileAction, rmdirAction) == FALSE) {
108 exit( FALSE);
Eric Andersenbed30e91999-10-18 19:02:32 +0000109 }
Eric Andersen596e5461999-10-07 08:30:23 +0000110 }
Eric Andersenbed30e91999-10-18 19:02:32 +0000111 exit( TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000112}