diff options
author | Laurent Bercot <ska-skaware@skarnet.org> | 2014-09-18 18:55:44 +0000 |
---|---|---|
committer | Laurent Bercot <ska-skaware@skarnet.org> | 2014-09-18 18:55:44 +0000 |
commit | 3534b428629be185e096be99e3bd5fdfe32d5544 (patch) | |
tree | 210ef3198ed66bc7f7b7bf6a85e4579f455e5a36 /src/libstddjb/openreadfileclose.c | |
download | skalibs-3534b428629be185e096be99e3bd5fdfe32d5544.tar.xz |
initial commit with rc for skalibs-2.0.0.0
Diffstat (limited to 'src/libstddjb/openreadfileclose.c')
-rw-r--r-- | src/libstddjb/openreadfileclose.c | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/libstddjb/openreadfileclose.c b/src/libstddjb/openreadfileclose.c new file mode 100644 index 0000000..90e6b47 --- /dev/null +++ b/src/libstddjb/openreadfileclose.c @@ -0,0 +1,37 @@ +/* ISC license. */ + +#include <sys/types.h> +#include <sys/stat.h> +#include <errno.h> +#include <skalibs/allreadwrite.h> +#include <skalibs/stralloc.h> +#include <skalibs/djbunix.h> + +int openreadfileclose (char const *file, stralloc *sa, unsigned int limit) +{ + unsigned int n ; + int fd = open_readb(file) ; + if (fd < 0) return 0 ; + { + struct stat st ; + if (fstat(fd, &st) < 0) goto err ; + n = st.st_size ; + } + if (limit && (limit < n)) n = limit ; + if (!stralloc_ready_tuned(sa, sa->len + n, 0, 0, 1)) goto err ; + { + register unsigned int r = allread(fd, sa->s + sa->len, n) ; + sa->len += r ; + if (r < n) goto err ; + } + fd_close(fd) ; + return 1 ; + +err: + { + register int e = errno ; + fd_close(fd) ; + errno = e ; + } + return 0 ; +} |