Fix the pwd and group functions. The bb_ stuff was a leftover from
[oweals/busybox.git] / cp_mv.c
diff --git a/cp_mv.c b/cp_mv.c
index 580c92046ed539b35113746986b5cf4af59b3909..dbd2fddf56dae3e7aa5f844996afcab872125f20 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 <unistd.h>
 #include <errno.h>
 #include <getopt.h>
+#include <stdlib.h>
 
-#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 *cp_mv_usage[] =     /* .rodata */
@@ -62,7 +63,7 @@ static const char *baseSrcName;
 static int                srcDirFlag;
 static struct stat srcStatBuf;
 
-static char               baseDestName[BUFSIZ + 1];
+static char               *pBaseDestName;
 static size_t     baseDestLen;
 static int                destDirFlag;
 static struct stat destStatBuf;
@@ -75,7 +76,7 @@ static void name_too_long__exit (void) __attribute__((noreturn));
 static
 void name_too_long__exit (void)
 {
-       fatalError(name_too_long);
+       error_msg_and_die(name_too_long);
 }
 
 static void
@@ -104,19 +105,19 @@ cp_mv_Action(const char *fileName, struct stat *statbuf, void* junk)
        const char *srcBasename;
        char       *name;
 
-       strcpy(destName, baseDestName);
+       strcpy(destName, pBaseDestName);
        destLen = strlen(destName);
 
        if (srcDirFlag == TRUE) {
                if (recursiveFlag == FALSE) {
-                       errorMsg(omitting_directory, baseSrcName);
+                       error_msg(omitting_directory, baseSrcName);
                        return TRUE;
                }
                srcBasename = (strstr(fileName, baseSrcName)
                                           + strlen(baseSrcName));
 
                if (destLen + strlen(srcBasename) > BUFSIZ) {
-                       errorMsg(name_too_long);
+                       error_msg(name_too_long);
                        return FALSE;
                }
                strcat(destName, srcBasename);
@@ -130,8 +131,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) {
-                       errorMsg("rename(%s, %s): %s\n", fileName, destName, 
-                                       strerror(errno));
+                       perror_msg("rename(%s, %s)", fileName, destName);
                        goto do_copyFile;       /* Try anyway... */
                }
                else if (errno == EXDEV)
@@ -143,7 +143,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) {
-                               errorMsg("link(%s, %s): %s\n", name, destName, strerror(errno));
+                               perror_msg("link(%s, %s)", name, destName);
                                return FALSE;
                        }
                        return TRUE;
@@ -152,7 +152,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, forceFlag);
+       return copy_file(fileName, destName, preserveFlag, followLinks, forceFlag);
 }
 
 static int
@@ -162,11 +162,11 @@ rm_Action(const char *fileName, struct stat *statbuf, void* junk)
 
        if (S_ISDIR(statbuf->st_mode)) {
                if (rmdir(fileName) < 0) {
-                       errorMsg("rmdir(%s): %s\n", fileName, strerror(errno));
+                       perror_msg("rmdir(%s)", fileName);
                        status = FALSE;
                }
        } else if (unlink(fileName) < 0) {
-               errorMsg("unlink(%s): %s\n", fileName, strerror(errno));
+               perror_msg("unlink(%s)", fileName);
                status = FALSE;
        }
        return status;
@@ -176,6 +176,8 @@ extern int cp_mv_main(int argc, char **argv)
 {
        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;
@@ -224,7 +226,7 @@ extern int cp_mv_main(int argc, char **argv)
        
 
        if (strlen(argv[argc - 1]) > BUFSIZ) {
-               errorMsg(name_too_long);
+               error_msg(name_too_long);
                goto exit_false;
        }
        strcpy(baseDestName, argv[argc - 1]);
@@ -232,9 +234,9 @@ extern int cp_mv_main(int argc, char **argv)
        if (baseDestLen == 0)
                goto exit_false;
 
-       destDirFlag = isDirectory(baseDestName, TRUE, &destStatBuf);
+       destDirFlag = is_directory(baseDestName, TRUE, &destStatBuf);
        if (argc - optind > 2 && destDirFlag == FALSE) {
-               errorMsg(not_a_directory, baseDestName);
+               error_msg(not_a_directory, baseDestName);
                goto exit_false;
        }
 
@@ -250,7 +252,7 @@ extern int cp_mv_main(int argc, char **argv)
 
                if (srcLen == 0) continue; /* "" */
 
-               srcDirFlag = isDirectory(baseSrcName, followLinks, &srcStatBuf);
+               srcDirFlag = is_directory(baseSrcName, followLinks, &srcStatBuf);
 
                if ((flags_memo = (recursiveFlag == TRUE &&
                                                   srcDirFlag == TRUE && destDirFlag == TRUE))) {
@@ -260,26 +262,26 @@ extern int cp_mv_main(int argc, char **argv)
                        char            *pushd, *d, *p;
 
                        if ((pushd = getcwd(NULL, BUFSIZ + 1)) == NULL) {
-                               errorMsg("getcwd(): %s\n", strerror(errno));
+                               perror_msg("getcwd()");
                                continue;
                        }
                        if (chdir(baseDestName) < 0) {
-                               errorMsg("chdir(%s): %s\n", baseSrcName, strerror(errno));
+                               perror_msg("chdir(%s)", baseSrcName);
                                continue;
                        }
                        if ((d = getcwd(NULL, BUFSIZ + 1)) == NULL) {
-                               errorMsg("getcwd(): %s\n", strerror(errno));
+                               perror_msg("getcwd()");
                                continue;
                        }
                        while (!state && *d != '\0') {
                                if (stat(d, &sb) < 0) { /* stat not lstat - always dereference targets */
-                                       errorMsg("stat(%s): %s\n", d, strerror(errno));
+                                       perror_msg("stat(%s)", d);
                                        state = -1;
                                        continue;
                                }
                                if ((sb.st_ino == srcStatBuf.st_ino) &&
                                        (sb.st_dev == srcStatBuf.st_dev)) {
-                                       errorMsg("Cannot %s `%s' into a subdirectory of itself, "
+                                       error_msg("Cannot %s `%s' into a subdirectory of itself, "
                                                        "`%s/%s'\n", applet_name, baseSrcName,
                                                        baseDestName, baseSrcName);
                                        state = -1;
@@ -290,7 +292,7 @@ extern int cp_mv_main(int argc, char **argv)
                                }
                        }
                        if (chdir(pushd) < 0) {
-                               errorMsg("chdir(%s): %s\n", pushd, strerror(errno));
+                               perror_msg("chdir(%s)", pushd);
                                free(pushd);
                                free(d);
                                continue;
@@ -305,11 +307,11 @@ 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;
                }