projects
/
libjh.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (parent:
95317cd
)
io: allow slurping files with a given size
author
Jann Horn
<jann@thejh.net>
Mon, 19 Aug 2013 20:51:24 +0000
(22:51 +0200)
committer
Jann Horn
<jann@thejh.net>
Mon, 19 Aug 2013 20:51:24 +0000
(22:51 +0200)
io.c
patch
|
blob
|
history
diff --git
a/io.c
b/io.c
index
d2b3197
..
3c0a8eb
100644
(file)
--- a/
io.c
+++ b/
io.c
@@
-67,6
+67,9
@@
PUBLIC_FN ssize_t write_nointr(int fd, void *buf, size_t count, int *last_res) {
PUBLIC_CONST JH_SLURP_NO_STAT 1
PUBLIC_CONST JH_SLURP_REALLOC 2 /* realloc result block if it saves RAM */
PUBLIC_CONST JH_SLURP_8BYTE_PAD 4 /* pad buffer with eight nullbytes, not one */
PUBLIC_CONST JH_SLURP_NO_STAT 1
PUBLIC_CONST JH_SLURP_REALLOC 2 /* realloc result block if it saves RAM */
PUBLIC_CONST JH_SLURP_8BYTE_PAD 4 /* pad buffer with eight nullbytes, not one */
+PUBLIC_CONST JH_SLURP_FIXSIZE 8 /* len_out is actually input and specifies
+HEADER the expected length. if there is more data,
+HEADER that might be ignored silently. */
PUBLIC_FN void *slurp_fd(int fd, size_t *len_out, int flags) {
int errno_;
PUBLIC_FN void *slurp_fd(int fd, size_t *len_out, int flags) {
int errno_;
@@
-75,6
+78,13
@@
PUBLIC_FN void *slurp_fd(int fd, size_t *len_out, int flags) {
size_t size_guess = 1023;
bool trusted_guess = false; /* can we rely on the guess? */
size_t size_guess = 1023;
bool trusted_guess = false; /* can we rely on the guess? */
+ if (flags&JH_SLURP_FIXSIZE) {
+ size_guess = *len_out;
+ trusted_guess = true;
+ // if we want a fixed size, of course we don't want to stat
+ flags |= JH_SLURP_NO_STAT;
+ }
+
int padlen = (flags&JH_SLURP_8BYTE_PAD)?8:1;
// If we can determine the exact size, we don't have to guess. So try
int padlen = (flags&JH_SLURP_8BYTE_PAD)?8:1;
// If we can determine the exact size, we don't have to guess. So try
@@
-113,6
+123,12
@@
PUBLIC_FN void *slurp_fd(int fd, size_t *len_out, int flags) {
char *buf_ = realloc(buf, done+padlen);
if (buf_) buf = buf_;
}
char *buf_ = realloc(buf, done+padlen);
if (buf_) buf = buf_;
}
+ if (done != size_guess && (flags&JH_SLURP_FIXSIZE)) {
+ free(buf);
+ // not the most correct error message ever, but whatever.
+ errno = EFBIG;
+ return NULL;
+ }
if (len_out) *len_out = done;
return buf;
}
if (len_out) *len_out = done;
return buf;
}