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 | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 30 | |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 31 | static const char mv_usage[] = "mv SOURCE DEST\n" |
Eric Andersen | d73dc5b | 1999-11-10 23:13:02 +0000 | [diff] [blame] | 32 | " or: mv SOURCE... DIRECTORY\n\n" |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 33 | "Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.\n"; |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 34 | |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 35 | |
| 36 | static const char *srcName; |
| 37 | static const char *destName; |
| 38 | static const char *skipName; |
| 39 | static int dirFlag = FALSE; |
| 40 | |
| 41 | |
| 42 | extern int mv_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 43 | { |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 44 | char newdestName[NAME_MAX]; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 45 | |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 46 | if (argc < 3) { |
Eric Andersen | b0e9a70 | 1999-10-18 22:28:26 +0000 | [diff] [blame] | 47 | usage (mv_usage); |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 48 | } |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 49 | argc--; |
| 50 | argv++; |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 51 | |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 52 | destName = argv[argc - 1]; |
| 53 | dirFlag = isDirectory(destName); |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 54 | |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 55 | if ((argc > 3) && dirFlag==FALSE) { |
| 56 | fprintf(stderr, "%s: not a directory\n", destName); |
Eric Andersen | 9b58718 | 1999-10-17 05:43:39 +0000 | [diff] [blame] | 57 | exit (FALSE); |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 58 | } |
Eric Andersen | d80e851 | 1999-11-16 00:46:00 +0000 | [diff] [blame] | 59 | |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 60 | while (argc-- > 1) { |
| 61 | srcName = *(argv++); |
| 62 | skipName = strrchr(srcName, '/'); |
| 63 | if (skipName) |
| 64 | skipName++; |
| 65 | strcpy(newdestName, destName); |
| 66 | if (dirFlag==TRUE) { |
| 67 | strcat(newdestName, "/"); |
| 68 | if ( skipName != NULL) |
| 69 | strcat(newdestName, strstr(srcName, skipName)); |
Eric Andersen | d80e851 | 1999-11-16 00:46:00 +0000 | [diff] [blame] | 70 | else |
| 71 | strcat(newdestName, srcName); |
| 72 | fprintf(stderr, "srcName='%s'\n", srcName); |
| 73 | fprintf(stderr, "skipName='%s'\n", skipName); |
| 74 | fprintf(stderr, "newdestName='%s'\n", newdestName); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 75 | } |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 76 | if (copyFile(srcName, newdestName, FALSE, FALSE) == FALSE) { |
| 77 | exit( FALSE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 78 | } |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 79 | if (unlink (srcName) < 0) { |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 80 | perror (srcName); |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 81 | exit( FALSE); |
| 82 | } |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 83 | } |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 84 | exit( TRUE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 85 | } |