mirror of
https://github.com/troglobit/libite.git
synced 2026-11-14 19:03:05 +00:00
Preserve binary data when copying file streams
Copy the bytes returned by fread instead of treating each input chunk as a NUL-terminated string. Preserve the public stream positions and report read and write failures rather than returning success after a partial copy. Add binary boundary, current-position, native pipe error and full-device regression coverage.
This commit is contained in:
1 parent
b69d8a6013
commit
2dfc02e67d
4 files changed
+110
-5
No files matched your search
+16
-5
@@ -286,6 +286,8 @@ int movefile(const char *src, const char *dst)
|
||||
int fcopyfile(FILE *src, FILE *dst)
|
||||
{
|
||||
char *buf;
|
||||
size_t len;
|
||||
int rc, saved_errno;
|
||||
|
||||
if (!src || !dst) {
|
||||
errno = EINVAL;
|
||||
@@ -296,12 +298,21 @@ int fcopyfile(FILE *src, FILE *dst)
|
||||
if (!buf)
|
||||
return -1;
|
||||
|
||||
while (fgets(buf, BUFSIZ, src))
|
||||
fputs(buf, dst);
|
||||
|
||||
free(buf);
|
||||
rc = 0;
|
||||
while ((len = fread(buf, 1, BUFSIZ, src)) > 0) {
|
||||
if (fwrite(buf, 1, len, dst) != len) {
|
||||
rc = -1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (ferror(src))
|
||||
rc = -1;
|
||||
|
||||
return 0;
|
||||
saved_errno = errno;
|
||||
free(buf);
|
||||
errno = saved_errno;
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user