sort is good to go.
[oweals/busybox.git] / mv.c
diff --git a/mv.c b/mv.c
index 10a08221014eac582b1b7b53106fb45ad089a91a..2be3961ca5b06f85e50b0eea35c5b23d31a48f2c 100644 (file)
--- a/mv.c
+++ b/mv.c
@@ -1,7 +1,9 @@
 /*
  * Mini mv implementation for busybox
  *
- * Copyright (C) 1998 by Erik Andersen <andersee@debian.org>
+ *
+ * Copyright (C) 1999 by Lineo, inc.
+ * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 
 #include "internal.h"
 #include <stdio.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
+#include <time.h>
 #include <utime.h>
-#include <errno.h>
+#include <dirent.h>
+
 
-const char mv_usage[] = "source-file [source-file ...] destination-file\n"
-    "\n" "\tMove the source files to the destination.\n" "\n";
+static const char mv_usage[] = "mv SOURCE DEST\n"
+"   or: mv SOURCE... DIRECTORY\n\n"
+"Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.\n";
 
 
+static const char *srcName;
+static const char *destName;
+static const char *skipName;
+static int dirFlag = FALSE;
 
-extern int mv_main (int argc, char **argv)
+
+extern int mv_main(int argc, char **argv)
 {
-    const char *srcName;
-    const char *destName;
-    const char *lastArg;
-    int dirFlag;
+    char newdestName[NAME_MAX];
 
     if (argc < 3) {
-       fprintf (stderr, "Usage: %s %s", *argv, mv_usage);
-       exit (FALSE);
+       usage (mv_usage);
     }
-    lastArg = argv[argc - 1];
+    argc--;
+    argv++;
 
-    dirFlag = isDirectory (lastArg);
+    destName = argv[argc - 1];
+    dirFlag = isDirectory(destName);
 
-    if ((argc > 3) && !dirFlag) {
-       fprintf (stderr, "%s: not a directory\n", lastArg);
+    if ((argc > 3) && dirFlag==FALSE) {
+       fprintf(stderr, "%s: not a directory\n", destName);
        exit (FALSE);
     }
-
-    while (argc-- > 2) {
-       srcName = *(++argv);
-
-       if (access (srcName, 0) < 0) {
-           perror (srcName);
-           continue;
+    
+    while (argc-- > 1) {
+       srcName = *(argv++);
+       skipName = strrchr(srcName, '/');
+       if (skipName) 
+           skipName++;
+       strcpy(newdestName, destName);
+       if (dirFlag==TRUE) {
+           strcat(newdestName, "/");
+           if ( skipName != NULL)
+               strcat(newdestName, strstr(srcName, skipName));
+           else
+               strcat(newdestName, srcName);
+           fprintf(stderr, "srcName='%s'\n", srcName);
+           fprintf(stderr, "skipName='%s'\n", skipName);
+           fprintf(stderr, "newdestName='%s'\n", newdestName);
        }
-
-       destName = lastArg;
-
-       if (dirFlag==TRUE)
-           destName = buildName (destName, srcName);
-
-       if (rename (srcName, destName) >= 0)
-           continue;
-
-       if (errno != EXDEV) {
-           perror (destName);
-           continue;
+       if (copyFile(srcName, newdestName, FALSE, FALSE)  == FALSE) {
+           exit( FALSE);
        }
-
-       if (!copyFile (srcName, destName, TRUE, FALSE))
-           continue;
-
-       if (unlink (srcName) < 0)
+       if (unlink (srcName) < 0) {
            perror (srcName);
+           exit( FALSE);
+       }
     }
-    exit (TRUE);
+    exitTRUE);
 }