diff options
author | Jakob Kaivo <jkk@ung.org> | 2021-05-15 21:04:25 -0400 |
---|---|---|
committer | Jakob Kaivo <jkk@ung.org> | 2021-05-15 21:04:25 -0400 |
commit | dbceb52424b0bcadb7775fa68e587203683b8364 (patch) | |
tree | e6e71a3347ad1e6fd6d3a249e6577e8d33cb17d8 | |
parent | 2dc7b09f9837f88d74e826288ac225dd0a1cca07 (diff) |
-rw-r--r-- | cp.c | 18 |
1 files changed, 16 insertions, 2 deletions
@@ -18,6 +18,7 @@ */ #define _XOPEN_SOURCE 700 +#include <fcntl.h> #include <errno.h> #include <stdbool.h> #include <stdio.h> @@ -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; } |