Fix up some silly macros and use xmalloc and friends exclusively.
[oweals/busybox.git] / cp_mv.c
diff --git a/cp_mv.c b/cp_mv.c
index f4104e57356e60c325c635132f20e061ed051ed4..fb48d3c5cecc5c4698e97a1102d7aa7de48977cf 100644 (file)
--- a/cp_mv.c
+++ b/cp_mv.c
@@ -3,7 +3,7 @@
  * Mini `cp' and `mv' implementation for BusyBox.
  *
  *
- * Copyright (C) 1999 by Lineo, inc.
+ * Copyright (C) 1999,2000,2001 by Lineo, inc.
  * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
  *
  * Copyright (C) 2000 by BitterSweet Enterprises, LLC. (GPL)
  *
  */
 
-#include "internal.h"
-#define BB_DECLARE_EXTERN
-#define bb_need_name_too_long
-#define bb_need_omitting_directory
-#define bb_need_not_a_directory
-#include "messages.c"
-
 #include <stdio.h>
 #include <time.h>
 #include <utime.h>
 #include <string.h>
 #include <unistd.h>
 #include <errno.h>
+#include <getopt.h>
+#include <stdlib.h>
+#include "busybox.h"
+#define BB_DECLARE_EXTERN
+#define bb_need_name_too_long
+#define bb_need_omitting_directory
+#define bb_need_not_a_directory
+#include "messages.c"
+
 
-#define is_cp 0
-#define is_mv 1
+static const int is_cp = 0;
+static const int is_mv = 1;
 static int         dz_i;               /* index into cp_mv_usage */
-static const char *dz;                 /* dollar zero, .bss */
-static const char *cp_mv_usage[] =     /* .rodata */
-{
-       "cp [OPTION]... SOURCE DEST\n"
-               "   or: cp [OPTION]... SOURCE... DIRECTORY\n"
-#ifndef BB_FEATURE_TRIVIAL_HELP
-               "\nCopies SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.\n"
-               "\n"
-               "\t-a\tSame as -dpR\n"
-               "\t-d\tPreserves links\n"
-               "\t-p\tPreserves file attributes if possible\n"
-               "\t-R\tCopies directories recursively\n"
-#endif
-               ,
-       "mv SOURCE DEST\n"
-               "   or: mv SOURCE... DIRECTORY\n"
-#ifndef BB_FEATURE_TRIVIAL_HELP
-               "\nRename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.\n"
-#endif
-};
+
 
 static int recursiveFlag;
 static int followLinks;
 static int preserveFlag;
+static int forceFlag;
 
 static const char *baseSrcName;
 static int                srcDirFlag;
 static struct stat srcStatBuf;
 
-static char               baseDestName[PATH_MAX + 1];
+static char               *pBaseDestName;
 static size_t     baseDestLen;
 static int                destDirFlag;
 static struct stat destStatBuf;
@@ -87,8 +71,7 @@ static void name_too_long__exit (void) __attribute__((noreturn));
 static
 void name_too_long__exit (void)
 {
-       fprintf(stderr, name_too_long, dz);
-       exit FALSE;
+       error_msg_and_die(name_too_long);
 }
 
 static void
@@ -97,12 +80,12 @@ fill_baseDest_buf(char *_buf, size_t * _buflen) {
        if ((srcBasename = strrchr(baseSrcName, '/')) == NULL) {
                srcBasename = baseSrcName;
                if (_buf[*_buflen - 1] != '/') {
-                       if (++(*_buflen) > PATH_MAX)
+                       if (++(*_buflen) > BUFSIZ)
                                name_too_long__exit();
                        strcat(_buf, "/");
                }
        }
-       if (*_buflen + strlen(srcBasename) > PATH_MAX)
+       if (*_buflen + strlen(srcBasename) > BUFSIZ)
                name_too_long__exit();
        strcat(_buf, srcBasename);
        return;
@@ -112,24 +95,24 @@ fill_baseDest_buf(char *_buf, size_t * _buflen) {
 static int
 cp_mv_Action(const char *fileName, struct stat *statbuf, void* junk)
 {
-       char            destName[PATH_MAX + 1];
+       char            destName[BUFSIZ + 1];
        size_t          destLen;
        const char *srcBasename;
        char       *name;
 
-       strcpy(destName, baseDestName);
+       strcpy(destName, pBaseDestName);
        destLen = strlen(destName);
 
        if (srcDirFlag == TRUE) {
                if (recursiveFlag == FALSE) {
-                       fprintf(stderr, omitting_directory, dz, baseSrcName);
+                       error_msg(omitting_directory, baseSrcName);
                        return TRUE;
                }
                srcBasename = (strstr(fileName, baseSrcName)
                                           + strlen(baseSrcName));
 
-               if (destLen + strlen(srcBasename) > PATH_MAX) {
-                       fprintf(stderr, name_too_long, dz);
+               if (destLen + strlen(srcBasename) > BUFSIZ) {
+                       error_msg(name_too_long);
                        return FALSE;
                }
                strcat(destName, srcBasename);
@@ -143,8 +126,7 @@ cp_mv_Action(const char *fileName, struct stat *statbuf, void* junk)
        if (mv_Action_first_time && (dz_i == is_mv)) {
                mv_Action_first_time = errno = 0;
                if (rename(fileName, destName) < 0 && errno != EXDEV) {
-                       fprintf(stderr, "%s: rename(%s, %s): %s\n",
-                                       dz, fileName, destName, strerror(errno));
+                       perror_msg("rename(%s, %s)", fileName, destName);
                        goto do_copyFile;       /* Try anyway... */
                }
                else if (errno == EXDEV)
@@ -156,8 +138,7 @@ cp_mv_Action(const char *fileName, struct stat *statbuf, void* junk)
        if (preserveFlag == TRUE && statbuf->st_nlink > 1) {
                if (is_in_ino_dev_hashtable(statbuf, &name)) {
                        if (link(name, destName) < 0) {
-                               fprintf(stderr, "%s: link(%s, %s): %s\n",
-                                               dz, name, destName, strerror(errno));
+                               perror_msg("link(%s, %s)", name, destName);
                                return FALSE;
                        }
                        return TRUE;
@@ -166,7 +147,7 @@ cp_mv_Action(const char *fileName, struct stat *statbuf, void* junk)
                        add_to_ino_dev_hashtable(statbuf, destName);
                }
        }
-       return copyFile(fileName, destName, preserveFlag, followLinks);
+       return copy_file(fileName, destName, preserveFlag, followLinks, forceFlag);
 }
 
 static int
@@ -176,11 +157,11 @@ rm_Action(const char *fileName, struct stat *statbuf, void* junk)
 
        if (S_ISDIR(statbuf->st_mode)) {
                if (rmdir(fileName) < 0) {
-                       fprintf(stderr, "%s: rmdir(%s): %s\n", dz, fileName, strerror(errno));
+                       perror_msg("rmdir(%s)", fileName);
                        status = FALSE;
                }
        } else if (unlink(fileName) < 0) {
-               fprintf(stderr, "%s: unlink(%s): %s\n", dz, fileName, strerror(errno));
+               perror_msg("unlink(%s)", fileName);
                status = FALSE;
        }
        return status;
@@ -188,22 +169,23 @@ rm_Action(const char *fileName, struct stat *statbuf, void* junk)
 
 extern int cp_mv_main(int argc, char **argv)
 {
-       dz = *argv;                                     /* already basename'd by busybox.c:main */
-       if (*dz == 'c' && *(dz + 1) == 'p')
+       volatile int i;
+       int c;
+       RESERVE_BB_BUFFER(baseDestName,BUFSIZ + 1);
+       pBaseDestName = baseDestName; /* available globally */
+
+       if (*applet_name == 'c' && *(applet_name + 1) == 'p')
                dz_i = is_cp;
        else
                dz_i = is_mv;
        if (argc < 3)
-               usage(cp_mv_usage[dz_i]);
-       argc--;
-       argv++;
+               show_usage();
 
        if (dz_i == is_cp) {
-               recursiveFlag = preserveFlag = FALSE;
+               recursiveFlag = preserveFlag = forceFlag = FALSE;
                followLinks = TRUE;
-               while (**argv == '-') {
-                       while (*++(*argv)) {
-                               switch (**argv) {
+               while ((c = getopt(argc, argv, "adpRf")) != EOF) {
+                               switch (c) {
                                case 'a':
                                        followLinks = FALSE;
                                        preserveFlag = TRUE;
@@ -218,20 +200,28 @@ extern int cp_mv_main(int argc, char **argv)
                                case 'R':
                                        recursiveFlag = TRUE;
                                        break;
+                               case 'f':
+                                       forceFlag = TRUE;
+                                       break;
                                default:
-                                       usage(cp_mv_usage[is_cp]);
+                                       show_usage();
                                }
-                       }
-                       argc--;
-                       argv++;
+               }
+               if ((argc - optind) < 2) {
+                       show_usage();
                }
        } else {                                        /* (dz_i == is_mv) */
+               /* Initialize optind to 1, since in libc5 optind
+                * is not initialized until getopt() is called
+                * (or until sneaky programmers force it...). */
+               optind = 1;
                recursiveFlag = preserveFlag = TRUE;
                followLinks = FALSE;
        }
+       
 
-       if (strlen(argv[argc - 1]) > PATH_MAX) {
-               fprintf(stderr, name_too_long, "cp");
+       if (strlen(argv[argc - 1]) > BUFSIZ) {
+               error_msg(name_too_long);
                goto exit_false;
        }
        strcpy(baseDestName, argv[argc - 1]);
@@ -239,25 +229,25 @@ extern int cp_mv_main(int argc, char **argv)
        if (baseDestLen == 0)
                goto exit_false;
 
-       destDirFlag = isDirectory(baseDestName, TRUE, &destStatBuf);
-       if ((argc > 3) && destDirFlag == FALSE) {
-               fprintf(stderr, not_a_directory, "cp", baseDestName);
+       destDirFlag = is_directory(baseDestName, TRUE, &destStatBuf);
+       if (argc - optind > 2 && destDirFlag == FALSE) {
+               error_msg(not_a_directory, baseDestName);
                goto exit_false;
        }
 
-       while (argc-- > 1) {
+       for (i = optind; i < (argc-1); i++) {
                size_t srcLen;
                volatile int flags_memo;
                int        status;
 
-               baseSrcName = *(argv++);
+               baseSrcName=argv[i];
 
-               if ((srcLen = strlen(baseSrcName)) > PATH_MAX)
+               if ((srcLen = strlen(baseSrcName)) > BUFSIZ)
                        name_too_long__exit();
 
                if (srcLen == 0) continue; /* "" */
 
-               srcDirFlag = isDirectory(baseSrcName, followLinks, &srcStatBuf);
+               srcDirFlag = is_directory(baseSrcName, followLinks, &srcStatBuf);
 
                if ((flags_memo = (recursiveFlag == TRUE &&
                                                   srcDirFlag == TRUE && destDirFlag == TRUE))) {
@@ -266,30 +256,29 @@ extern int cp_mv_main(int argc, char **argv)
                        int                     state = 0;
                        char            *pushd, *d, *p;
 
-                       if ((pushd = getcwd(NULL, PATH_MAX + 1)) == NULL) {
-                               fprintf(stderr, "%s: getcwd(): %s\n", dz, strerror(errno));
+                       if ((pushd = getcwd(NULL, BUFSIZ + 1)) == NULL) {
+                               perror_msg("getcwd()");
                                continue;
                        }
                        if (chdir(baseDestName) < 0) {
-                               fprintf(stderr, "%s: chdir(%s): %s\n", dz, baseSrcName, strerror(errno));
+                               perror_msg("chdir(%s)", baseSrcName);
                                continue;
                        }
-                       if ((d = getcwd(NULL, PATH_MAX + 1)) == NULL) {
-                               fprintf(stderr, "%s: getcwd(): %s\n", dz, strerror(errno));
+                       if ((d = getcwd(NULL, BUFSIZ + 1)) == NULL) {
+                               perror_msg("getcwd()");
                                continue;
                        }
                        while (!state && *d != '\0') {
                                if (stat(d, &sb) < 0) { /* stat not lstat - always dereference targets */
-                                       fprintf(stderr, "%s: stat(%s) :%s\n", dz, d, strerror(errno));
+                                       perror_msg("stat(%s)", d);
                                        state = -1;
                                        continue;
                                }
                                if ((sb.st_ino == srcStatBuf.st_ino) &&
                                        (sb.st_dev == srcStatBuf.st_dev)) {
-                                       fprintf(stderr,
-                                                       "%s: Cannot %s `%s' "
-                                                       "into a subdirectory of itself, `%s/%s'\n",
-                                                       dz, dz, baseSrcName, baseDestName, baseSrcName);
+                                       error_msg("Cannot %s `%s' into a subdirectory of itself, "
+                                                       "`%s/%s'", applet_name, baseSrcName,
+                                                       baseDestName, baseSrcName);
                                        state = -1;
                                        continue;
                                }
@@ -298,7 +287,7 @@ extern int cp_mv_main(int argc, char **argv)
                                }
                        }
                        if (chdir(pushd) < 0) {
-                               fprintf(stderr, "%s: chdir(%s): %s\n", dz, pushd, strerror(errno));
+                               perror_msg("chdir(%s)", pushd);
                                free(pushd);
                                free(d);
                                continue;
@@ -313,21 +302,20 @@ extern int cp_mv_main(int argc, char **argv)
                status = setjmp(catch);
                if (status == 0) {
                        mv_Action_first_time = 1;
-                       if (recursiveAction(baseSrcName,
+                       if (recursive_action(baseSrcName,
                                                                recursiveFlag, followLinks, FALSE,
                                                                cp_mv_Action, cp_mv_Action, NULL) == FALSE) goto exit_false;
                        if (dz_i == is_mv &&
-                               recursiveAction(baseSrcName,
+                               recursive_action(baseSrcName,
                                                                recursiveFlag, followLinks, TRUE,
                                                                rm_Action, rm_Action, NULL) == FALSE) goto exit_false;
                }               
                if (flags_memo)
                        *(baseDestName + baseDestLen) = '\0';
        }
-// exit_true:
-       exit TRUE;
+       return EXIT_SUCCESS;
  exit_false:
-       exit FALSE;
+       return EXIT_FAILURE;
 }
 
 /*