Simplify CRC table generation
[oweals/busybox.git] / libbb / copy_file.c
index 22684be74283c1bf0996637979a91c48ded60f9a..a80e30b50549ee2d2ddfdfa6299a6f0f6f4ec804 100644 (file)
@@ -2,7 +2,6 @@
 /*
  * Mini copy_file implementation for busybox
  *
- *
  * Copyright (C) 2001 by Matt Kraai <kraai@alumni.carnegiemellon.edu>
  *
  * This program is free software; you can redistribute it and/or modify
@@ -29,6 +28,7 @@
 #include <errno.h>
 #include <dirent.h>
 #include <stdlib.h>
+#include <string.h>
 
 #include "libbb.h"
 
@@ -39,9 +39,9 @@ int copy_file(const char *source, const char *dest, int flags)
        int dest_exists = 1;
        int status = 0;
 
-       if (((flags & FILEUTILS_PRESERVE_SYMLINKS) &&
+       if ((!(flags & FILEUTILS_DEREFERENCE) &&
                        lstat(source, &source_stat) < 0) ||
-                       (!(flags & FILEUTILS_PRESERVE_SYMLINKS) &&
+                       ((flags & FILEUTILS_DEREFERENCE) &&
                         stat(source, &source_stat) < 0)) {
                perror_msg("%s", source);
                return -1;
@@ -94,7 +94,7 @@ int copy_file(const char *source, const char *dest, int flags)
 
                        umask(saved_umask);
                }
-               
+
                /* Recursively copy files in SOURCE.  */
                if ((dp = opendir(source)) == NULL) {
                        perror_msg("unable to open directory `%s'", source);
@@ -116,7 +116,7 @@ int copy_file(const char *source, const char *dest, int flags)
                        free(new_source);
                        free(new_dest);
                }
-               
+
                /* ??? What if an error occurs in readdir?  */
 
                if (closedir(dp) < 0) {
@@ -130,7 +130,7 @@ int copy_file(const char *source, const char *dest, int flags)
                        status = -1;
                }
        } else if (S_ISREG(source_stat.st_mode)) {
-               FILE *sfp, *dfp;
+               FILE *sfp, *dfp=NULL;
 
                if (dest_exists) {
                        if (flags & FILEUTILS_INTERACTIVE) {
@@ -173,7 +173,8 @@ int copy_file(const char *source, const char *dest, int flags)
                        goto end;
                }
 
-               copy_file_chunk(sfp, dfp, source_stat.st_size);
+               if (copy_file_chunk(sfp, dfp, -1) < 0)
+                       status = -1;
 
                if (fclose(dfp) < 0) {
                        perror_msg("unable to close `%s'", dest);