чување b946976470f901577be916c062a6bec846dd266c
родитељ ce9da14e665f65a9f307980f60c797c860565416
Аутор: Страхиња Радић <sr@strahinja.org>
Датум: Tue, 12 Aug 2025 22:09:28 +0200
sled.c(cp): Pass third argument to open with O_CREAT
Diffstat:
измењених датотека: 1, додавања: 8(+), брисања: 5(-)
diff --git a/sled.c b/sled.c
@@ -97,18 +97,20 @@ cp(const char* from, const char* to)
int saved_errno;
assert((from != NULL) && (to != NULL));
+
+ tofd = 0;
fromfd = open(from, O_RDONLY);
if (fromfd < 0)
return 1;
- tofd = open(to, O_WRONLY | O_CREAT | O_TRUNC);
- if (tofd < 0)
- return 1;
-
if (fstat(fromfd, &st) < 0)
{
result = 1;
goto cp_cleanup;
}
+
+ tofd = open(to, O_WRONLY | O_CREAT | O_TRUNC, st.st_mode);
+ if (tofd < 0)
+ return 1;
if (fchmod(tofd, st.st_mode) < 0)
{
result = 1;
@@ -140,7 +142,8 @@ cp(const char* from, const char* to)
cp_cleanup:
saved_errno = errno;
- close(tofd);
+ if (tofd)
+ close(tofd);
close(fromfd);
errno = saved_errno;
return result;