aboutsummaryrefslogtreecommitdiff
path: root/mv.c
diff options
context:
space:
mode:
Diffstat (limited to 'mv.c')
-rw-r--r--mv.c76
1 files changed, 36 insertions, 40 deletions
diff --git a/mv.c b/mv.c
index 10a082210..2a7c8c1f3 100644
--- a/mv.c
+++ b/mv.c
@@ -21,63 +21,59 @@
21 21
22#include "internal.h" 22#include "internal.h"
23#include <stdio.h> 23#include <stdio.h>
24#include <sys/types.h> 24#include <time.h>
25#include <sys/stat.h>
26#include <fcntl.h>
27#include <utime.h> 25#include <utime.h>
28#include <errno.h> 26#include <dirent.h>
29 27
30const char mv_usage[] = "source-file [source-file ...] destination-file\n"
31 "\n" "\tMove the source files to the destination.\n" "\n";
32 28
29static const char mv_usage[] = "mv SOURCE DEST\n"
30" or: mv SOURCE... DIRECTORY\n"
31"Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.\n";
33 32
34 33
35extern int mv_main (int argc, char **argv) 34static const char *srcName;
35static const char *destName;
36static const char *skipName;
37static int dirFlag = FALSE;
38
39
40extern int mv_main(int argc, char **argv)
36{ 41{
37 const char *srcName; 42 char newdestName[NAME_MAX];
38 const char *destName;
39 const char *lastArg;
40 int dirFlag;
41 43
42 if (argc < 3) { 44 if (argc < 3) {
43 fprintf (stderr, "Usage: %s %s", *argv, mv_usage); 45 fprintf(stderr, "Usage: %s", mv_usage);
44 exit (FALSE); 46 exit (FALSE);
45 } 47 }
46 lastArg = argv[argc - 1]; 48 argc--;
49 argv++;
47 50
48 dirFlag = isDirectory (lastArg); 51 destName = argv[argc - 1];
52 dirFlag = isDirectory(destName);
49 53
50 if ((argc > 3) && !dirFlag) { 54 if ((argc > 3) && dirFlag==FALSE) {
51 fprintf (stderr, "%s: not a directory\n", lastArg); 55 fprintf(stderr, "%s: not a directory\n", destName);
52 exit (FALSE); 56 exit (FALSE);
53 } 57 }
54 58
55 while (argc-- > 2) { 59 while (argc-- > 1) {
56 srcName = *(++argv); 60 srcName = *(argv++);
57 61 skipName = strrchr(srcName, '/');
58 if (access (srcName, 0) < 0) { 62 if (skipName)
59 perror (srcName); 63 skipName++;
60 continue; 64 strcpy(newdestName, destName);
65 if (dirFlag==TRUE) {
66 strcat(newdestName, "/");
67 if ( skipName != NULL)
68 strcat(newdestName, strstr(srcName, skipName));
61 } 69 }
62 70 if (copyFile(srcName, newdestName, FALSE, FALSE) == FALSE) {
63 destName = lastArg; 71 exit( FALSE);
64
65 if (dirFlag==TRUE)
66 destName = buildName (destName, srcName);
67
68 if (rename (srcName, destName) >= 0)
69 continue;
70
71 if (errno != EXDEV) {
72 perror (destName);
73 continue;
74 } 72 }
75 73 if (unlink (srcName) < 0) {
76 if (!copyFile (srcName, destName, TRUE, FALSE))
77 continue;
78
79 if (unlink (srcName) < 0)
80 perror (srcName); 74 perror (srcName);
75 exit( FALSE);
76 }
81 } 77 }
82 exit (TRUE); 78 exit( TRUE);
83} 79}