diff options
author | Matt Kraai <kraai@debian.org> | 2001-12-11 19:43:48 +0300 |
---|---|---|
committer | Matt Kraai <kraai@debian.org> | 2001-12-11 19:43:48 +0300 |
commit | 14b7c5d12b9a492d148b744cf8300f47b53aafd0 (patch) | |
tree | a526d078b760f343038f2f67eda043e3e9198613 /libbb/copy_file.c | |
parent | 741f40b58edf3644c6bc8e6863ee9ad681b21562 (diff) |
Open the source before creating the destination.
Diffstat (limited to 'libbb/copy_file.c')
-rw-r--r-- | libbb/copy_file.c | 19 |
1 files changed, 11 insertions, 8 deletions
diff --git a/libbb/copy_file.c b/libbb/copy_file.c index a80e30b50..29778f2a4 100644 --- a/libbb/copy_file.c +++ b/libbb/copy_file.c @@ -132,21 +132,30 @@ int copy_file(const char *source, const char *dest, int flags) } else if (S_ISREG(source_stat.st_mode)) { FILE *sfp, *dfp=NULL; + if ((sfp = fopen(source, "r")) == NULL) { + perror_msg("unable to open `%s'", source); + return -1; + } + if (dest_exists) { if (flags & FILEUTILS_INTERACTIVE) { fprintf(stderr, "%s: overwrite `%s'? ", applet_name, dest); - if (!ask_confirmation()) + if (!ask_confirmation()) { + fclose (sfp); return 0; + } } if ((dfp = fopen(dest, "w")) == NULL) { if (!(flags & FILEUTILS_FORCE)) { perror_msg("unable to open `%s'", dest); + fclose (sfp); return -1; } if (unlink(dest) < 0) { perror_msg("unable to remove `%s'", dest); + fclose (sfp); return -1; } @@ -162,17 +171,11 @@ int copy_file(const char *source, const char *dest, int flags) if (fd >= 0) close(fd); perror_msg("unable to open `%s'", dest); + fclose (sfp); return -1; } } - if ((sfp = fopen(source, "r")) == NULL) { - fclose(dfp); - perror_msg("unable to open `%s'", source); - status = -1; - goto end; - } - if (copy_file_chunk(sfp, dfp, -1) < 0) status = -1; |