summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cp.c18
1 files 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 <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;
}