From d84bcb38479d07767c72c1553fe82a641447e531 Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Fri, 22 Dec 2017 11:34:46 +0100 Subject: [PATCH] copyfile(),movefile(): constify A `const` string is a declaration to the user of an API that the implementation does not touch the contents. We do some `(char *)` casting in this patch, but the resulting code still does not touch the contents of the original pointer. Signed-off-by: Joachim Nilsson --- src/copyfile.c | 27 ++++++++++++++------------- src/lite.h | 4 ++-- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/copyfile.c b/src/copyfile.c index 8c5463b..720a3e3 100644 --- a/src/copyfile.c +++ b/src/copyfile.c @@ -27,7 +27,7 @@ #include "lite.h" /* Tests if dst is a directory, if so, reallocates dst and appends src filename returning 1 */ -static int adjust_target(char *src, char **dst) +static int adjust_target(const char *src, char **dst) { int isdir = 0; @@ -36,7 +36,7 @@ static int adjust_target(char *src, char **dst) char *tmp, *ptr = strrchr(src, '/'); if (!ptr) - ptr = src; + ptr = (char *)src; else ptr++; @@ -115,9 +115,9 @@ static void set_mtime(int in, int out) * may also indicate that @src was empty. If @src is a directory @errno * will be set to %EISDIR since copyfile() is not recursive. */ -ssize_t copyfile(char *src, char *dst, int len, int opt) +ssize_t copyfile(const char *src, const char *dst, int len, int opt) { - char *buffer; + char *buffer, *dest = (char *)dst; int sym = (opt & LITE_FOPT_COPYFILE_SYM) != 0; int keep_mtim = (opt & LITE_FOPT_KEEP_MTIME) != 0; int in, out, isdir = 0, saved_errno = 0; @@ -137,7 +137,7 @@ ssize_t copyfile(char *src, char *dst, int len, int opt) } /* Check if target is a directory, then append src filename. */ - isdir = adjust_target(src, &dst); + isdir = adjust_target(src, &dest); /* Check if the source file is a symlink ... */ if (stat(src, &st)) { @@ -154,7 +154,7 @@ ssize_t copyfile(char *src, char *dst, int len, int opt) size = -1; } else { buffer[size] = 0; - size = !symlink(buffer, dst); + size = !symlink(buffer, dest); } } @@ -175,7 +175,7 @@ ssize_t copyfile(char *src, char *dst, int len, int opt) goto exit; } - out = open(dst, O_WRONLY | O_CREAT | O_TRUNC, st.st_mode); + out = open(dest, O_WRONLY | O_CREAT | O_TRUNC, st.st_mode); if (out < 0) { close(in); saved_errno = errno; @@ -194,7 +194,7 @@ ssize_t copyfile(char *src, char *dst, int len, int opt) exit: free(buffer); if (isdir) - free(dst); + free(dest); errno = saved_errno; return size; @@ -216,17 +216,18 @@ exit: * Returns: * POSIX OK(0), or non-zero with errno set. */ -int movefile(char *src, char *dst) +int movefile(const char *src, const char *dst) { + char *dest = (char *)dst; int isdir, result = 0; /* Check if target is a directory, then append the src base filename. */ - isdir = adjust_target(src, &dst); + isdir = adjust_target(src, &dest); - if (rename(src, dst)) { + if (rename(src, dest)) { if (errno == EXDEV) { errno = 0; - copyfile(src, dst, 0, 1); + copyfile(src, dest, 0, 1); if (errno) result = 1; else @@ -237,7 +238,7 @@ int movefile(char *src, char *dst) } if (isdir) - free(dst); + free(dest); return result; } diff --git a/src/lite.h b/src/lite.h index 6dbd221..73ea91f 100644 --- a/src/lite.h +++ b/src/lite.h @@ -60,8 +60,8 @@ int fexist (const char *file); int fisdir (const char *file); FILE *tempfile (void); -ssize_t copyfile (char *src, char *dst, int len, int opt); -int movefile (char *src, char *dst); +ssize_t copyfile (const char *src, const char *dst, int len, int opt); +int movefile (const char *src, const char *dst); int fcopyfile (FILE *src, FILE *dst); ssize_t fsendfile (FILE *src, FILE *dst, size_t len);