From 8a962fd5dc8c0f233ee0848613821e52737d905a Mon Sep 17 00:00:00 2001 From: Tomas Mlcoch Date: Wed, 4 Apr 2012 10:36:50 +0200 Subject: [PATCH] Upgrade of copy_file function in misc --- src/misc.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/misc.c b/src/misc.c index 19ec93b..d2cbb16 100644 --- a/src/misc.c +++ b/src/misc.c @@ -385,7 +385,7 @@ char *get_filename(const char *filepath) } -int copy_file(const char *src, const char *dst) +int copy_file(const char *src, const char *in_dst) { size_t readed; char buf[BUFFER_SIZE]; @@ -393,6 +393,17 @@ int copy_file(const char *src, const char *dst) FILE *orig; FILE *new; + if (!src || !in_dst) { + g_debug(MODULE"%s: File name cannot be NULL", __func__); + return CR_COPY_ERR; + } + + // If destination is dir use filename from src + gchar dst = in_dst; + if (g_str_has_suffix(in_dst, "/")) { + dst = g_strconcat(in_dst, get_filename(src), NULL); + } + if ((orig = fopen(src, "r")) == NULL) { g_debug(MODULE"%s: Cannot open source file %s (%s)", __func__, src, strerror(errno)); return CR_COPY_ERR; @@ -420,6 +431,10 @@ int copy_file(const char *src, const char *dst) } } + if (dst != in_dst) { + g_free(dst); + } + fclose(new); fclose(orig); -- 2.7.4