blob: 2be3961ca5b06f85e50b0eea35c5b23d31a48f2c [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 Andersen596e5461999-10-07 08:30:23 +000030
Eric Andersenbed30e91999-10-18 19:02:32 +000031static const char mv_usage[] = "mv SOURCE DEST\n"
Eric Andersend73dc5b1999-11-10 23:13:02 +000032" or: mv SOURCE... DIRECTORY\n\n"
Eric Andersenbed30e91999-10-18 19:02:32 +000033"Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.\n";
Eric Andersen596e5461999-10-07 08:30:23 +000034
Eric Andersenbed30e91999-10-18 19:02:32 +000035
36static const char *srcName;
37static const char *destName;
38static const char *skipName;
39static int dirFlag = FALSE;
40
41
42extern int mv_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000043{
Eric Andersenbed30e91999-10-18 19:02:32 +000044 char newdestName[NAME_MAX];
Eric Andersencc8ed391999-10-05 16:24:54 +000045
Eric Andersen596e5461999-10-07 08:30:23 +000046 if (argc < 3) {
Eric Andersenb0e9a701999-10-18 22:28:26 +000047 usage (mv_usage);
Eric Andersen596e5461999-10-07 08:30:23 +000048 }
Eric Andersenbed30e91999-10-18 19:02:32 +000049 argc--;
50 argv++;
Eric Andersen596e5461999-10-07 08:30:23 +000051
Eric Andersenbed30e91999-10-18 19:02:32 +000052 destName = argv[argc - 1];
53 dirFlag = isDirectory(destName);
Eric Andersen596e5461999-10-07 08:30:23 +000054
Eric Andersenbed30e91999-10-18 19:02:32 +000055 if ((argc > 3) && dirFlag==FALSE) {
56 fprintf(stderr, "%s: not a directory\n", destName);
Eric Andersen9b587181999-10-17 05:43:39 +000057 exit (FALSE);
Eric Andersen596e5461999-10-07 08:30:23 +000058 }
Eric Andersend80e8511999-11-16 00:46:00 +000059
Eric Andersenbed30e91999-10-18 19:02:32 +000060 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 Andersend80e8511999-11-16 00:46:00 +000070 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 Andersencc8ed391999-10-05 16:24:54 +000075 }
Eric Andersenbed30e91999-10-18 19:02:32 +000076 if (copyFile(srcName, newdestName, FALSE, FALSE) == FALSE) {
77 exit( FALSE);
Eric Andersencc8ed391999-10-05 16:24:54 +000078 }
Eric Andersenbed30e91999-10-18 19:02:32 +000079 if (unlink (srcName) < 0) {
Eric Andersen596e5461999-10-07 08:30:23 +000080 perror (srcName);
Eric Andersenbed30e91999-10-18 19:02:32 +000081 exit( FALSE);
82 }
Eric Andersen596e5461999-10-07 08:30:23 +000083 }
Eric Andersenbed30e91999-10-18 19:02:32 +000084 exit( TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +000085}