blob: 10a08221014eac582b1b7b53106fb45ad089a91a [file] [log] [blame]
Eric Andersen596e5461999-10-07 08:30:23 +00001/*
2 * Mini mv implementation for busybox
3 *
4 * Copyright (C) 1998 by Erik Andersen <andersee@debian.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
Eric Andersencc8ed391999-10-05 16:24:54 +000022#include "internal.h"
23#include <stdio.h>
Eric Andersen596e5461999-10-07 08:30:23 +000024#include <sys/types.h>
25#include <sys/stat.h>
26#include <fcntl.h>
27#include <utime.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000028#include <errno.h>
29
Eric Andersen596e5461999-10-07 08:30:23 +000030const char mv_usage[] = "source-file [source-file ...] destination-file\n"
31 "\n" "\tMove the source files to the destination.\n" "\n";
Eric Andersencc8ed391999-10-05 16:24:54 +000032
Eric Andersen596e5461999-10-07 08:30:23 +000033
34
35extern int mv_main (int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000036{
Eric Andersen596e5461999-10-07 08:30:23 +000037 const char *srcName;
38 const char *destName;
39 const char *lastArg;
Eric Andersenf811e071999-10-09 00:25:00 +000040 int dirFlag;
Eric Andersencc8ed391999-10-05 16:24:54 +000041
Eric Andersen596e5461999-10-07 08:30:23 +000042 if (argc < 3) {
43 fprintf (stderr, "Usage: %s %s", *argv, mv_usage);
Eric Andersen9b587181999-10-17 05:43:39 +000044 exit (FALSE);
Eric Andersen596e5461999-10-07 08:30:23 +000045 }
46 lastArg = argv[argc - 1];
47
48 dirFlag = isDirectory (lastArg);
49
50 if ((argc > 3) && !dirFlag) {
51 fprintf (stderr, "%s: not a directory\n", lastArg);
Eric Andersen9b587181999-10-17 05:43:39 +000052 exit (FALSE);
Eric Andersen596e5461999-10-07 08:30:23 +000053 }
54
55 while (argc-- > 2) {
56 srcName = *(++argv);
57
58 if (access (srcName, 0) < 0) {
59 perror (srcName);
60 continue;
Eric Andersencc8ed391999-10-05 16:24:54 +000061 }
Eric Andersen596e5461999-10-07 08:30:23 +000062
63 destName = lastArg;
64
Eric Andersenf811e071999-10-09 00:25:00 +000065 if (dirFlag==TRUE)
Eric Andersen596e5461999-10-07 08:30:23 +000066 destName = buildName (destName, srcName);
67
68 if (rename (srcName, destName) >= 0)
69 continue;
70
71 if (errno != EXDEV) {
72 perror (destName);
73 continue;
Eric Andersencc8ed391999-10-05 16:24:54 +000074 }
Eric Andersen596e5461999-10-07 08:30:23 +000075
Eric Andersenf811e071999-10-09 00:25:00 +000076 if (!copyFile (srcName, destName, TRUE, FALSE))
Eric Andersen596e5461999-10-07 08:30:23 +000077 continue;
78
79 if (unlink (srcName) < 0)
80 perror (srcName);
81 }
Eric Andersen9b587181999-10-17 05:43:39 +000082 exit (TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +000083}