From dbceb52424b0bcadb7775fa68e587203683b8364 Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Sat, 15 May 2021 21:04:25 -0400 Subject: copy source permission bits --- cp.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/cp.c b/cp.c index c852daa..f444e0e 100644 --- a/cp.c +++ b/cp.c @@ -18,6 +18,7 @@ */ #define _XOPEN_SOURCE 700 +#include #include #include #include @@ -70,9 +71,21 @@ static int cp(char *src, const char *target, enum flags flags) } } - FILE *out = fopen(targetpath, "wb"); + if (fstat(fileno(in), &st) != 0) { + perror("fstat"); + return 1; + } + + int ofd = open(targetpath, O_WRONLY | O_CREAT, st.st_mode); + if (ofd == -1) { + perror("open"); + return 1; + } + + FILE *out = fdopen(ofd, "wb"); if (out == NULL) { - fprintf(stderr, "cp: Couldn't open %s: %s\n", targetpath, strerror(errno)); + fprintf(stderr, "cp: Couldn't open %s: %s\n", targetpath, + strerror(errno)); return 1; } @@ -83,6 +96,7 @@ static int cp(char *src, const char *target, enum flags flags) fclose(in); fclose(out); + close(ofd); return 0; } -- cgit v1.2.1