Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Mini mv implementation for busybox |
| 3 | * |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 1999 by Lineo, inc. |
| 6 | * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org> |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 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 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 24 | #include "internal.h" |
| 25 | #include <stdio.h> |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 26 | #include <time.h> |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 27 | #include <utime.h> |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 28 | #include <dirent.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 29 | |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 30 | static const char mv_usage[] = "mv SOURCE DEST\n" |
Eric Andersen | d73dc5b | 1999-11-10 23:13:02 +0000 | [diff] [blame] | 31 | " or: mv SOURCE... DIRECTORY\n\n" |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 32 | "Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.\n"; |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 33 | |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 34 | |
| 35 | static const char *srcName; |
| 36 | static const char *destName; |
Erik Andersen | f664c00 | 1999-12-29 02:36:29 +0000 | [diff] [blame] | 37 | static int destDirFlag = FALSE; |
| 38 | static int srcDirFlag = FALSE; |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 39 | |
Erik Andersen | 2fe08c7 | 1999-12-29 02:10:35 +0000 | [diff] [blame] | 40 | static int fileAction(const char *fileName, struct stat* statbuf) |
| 41 | { |
| 42 | char newdestName[NAME_MAX]; |
Erik Andersen | 1dbc17f | 2000-01-08 21:16:29 +0000 | [diff] [blame] | 43 | char* newsrcName = NULL; |
Erik Andersen | 2fe08c7 | 1999-12-29 02:10:35 +0000 | [diff] [blame] | 44 | |
Erik Andersen | 2fe08c7 | 1999-12-29 02:10:35 +0000 | [diff] [blame] | 45 | strcpy(newdestName, destName); |
Erik Andersen | f664c00 | 1999-12-29 02:36:29 +0000 | [diff] [blame] | 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 | } |
Erik Andersen | 1dbc17f | 2000-01-08 21:16:29 +0000 | [diff] [blame] | 54 | newsrcName = strrchr(srcName, '/'); |
| 55 | if (newsrcName && *newsrcName != '\0') |
| 56 | strcat(newdestName, newsrcName); |
| 57 | else |
| 58 | strcat(newdestName, srcName); |
Erik Andersen | f664c00 | 1999-12-29 02:36:29 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Erik Andersen | 2fe08c7 | 1999-12-29 02:10:35 +0000 | [diff] [blame] | 61 | return (copyFile(fileName, newdestName, TRUE, TRUE)); |
| 62 | } |
| 63 | |
Erik Andersen | f664c00 | 1999-12-29 02:36:29 +0000 | [diff] [blame] | 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 | |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 82 | |
| 83 | extern int mv_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 84 | { |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 85 | if (argc < 3) { |
Eric Andersen | b0e9a70 | 1999-10-18 22:28:26 +0000 | [diff] [blame] | 86 | usage (mv_usage); |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 87 | } |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 88 | argc--; |
| 89 | argv++; |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 90 | |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 91 | destName = argv[argc - 1]; |
Erik Andersen | f664c00 | 1999-12-29 02:36:29 +0000 | [diff] [blame] | 92 | destDirFlag = isDirectory(destName); |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 93 | |
Erik Andersen | f664c00 | 1999-12-29 02:36:29 +0000 | [diff] [blame] | 94 | if ((argc > 3) && destDirFlag==FALSE) { |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 95 | fprintf(stderr, "%s: not a directory\n", destName); |
Eric Andersen | 9b58718 | 1999-10-17 05:43:39 +0000 | [diff] [blame] | 96 | exit (FALSE); |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 97 | } |
Erik Andersen | f664c00 | 1999-12-29 02:36:29 +0000 | [diff] [blame] | 98 | |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 99 | while (argc-- > 1) { |
| 100 | srcName = *(argv++); |
Erik Andersen | f664c00 | 1999-12-29 02:36:29 +0000 | [diff] [blame] | 101 | srcDirFlag = isDirectory(srcName); |
| 102 | if (recursiveAction(srcName, TRUE, TRUE, FALSE, |
| 103 | fileAction, fileAction) == FALSE) { |
| 104 | exit( FALSE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 105 | } |
Erik Andersen | f664c00 | 1999-12-29 02:36:29 +0000 | [diff] [blame] | 106 | if (recursiveAction(srcName, TRUE, TRUE, TRUE, |
| 107 | rmfileAction, rmdirAction) == FALSE) { |
| 108 | exit( FALSE); |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 109 | } |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 110 | } |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 111 | exit( TRUE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 112 | } |