diff options
author | Jannis Hoffmann <jannis@fehcom.de> | 2024-07-09 13:58:20 +0200 |
---|---|---|
committer | Jannis Hoffmann <jannis@fehcom.de> | 2024-07-09 13:58:20 +0200 |
commit | 249866e3d1e11dc72eaa1305f4bb479ded92ef38 (patch) | |
tree | 7118c5f58e29fe61c100e4d067bb90ba8d52589e /src/readclose.c | |
parent | 96cf8dffe4f7b0b910f790066ae622dc429eb522 (diff) |
reorganized file structure
Moved c files into src/.
Corrected VERSION file.
Removed BUILD and FILES.
Diffstat (limited to 'src/readclose.c')
-rw-r--r-- | src/readclose.c | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/src/readclose.c b/src/readclose.c new file mode 100644 index 0000000..b0bce7e --- /dev/null +++ b/src/readclose.c @@ -0,0 +1,43 @@ +#include <unistd.h> +#include "open.h" +#include "error.h" +#include "readclose.h" + +/** + @file readclose.c + @author kp + @source qlibs + @brief This is the successor of the older 'slurpclose.c' file. The function + 'slurpclose' is now called 'readclose_append'. The other function + 'readclose' was introduced here initial. +*/ + +int readclose_append(int fd,stralloc *sa,unsigned int bufsize) +{ + int r; + for (;;) { + if (!stralloc_readyplus(sa,bufsize)) { close(fd); return -1; } + r = read(fd,sa->s + sa->len,bufsize); + if (r == -1) if (errno == EINTR) continue; + if (r <= 0) { close(fd); return r; } + sa->len += r; + } +} + +int readclose(int fd,stralloc *sa,unsigned int bufsize) +{ + if (!stralloc_copys(sa,"")) { close(fd); return -1; } + return readclose_append(fd,sa,bufsize); +} + +int openreadclose(const char *fn,stralloc *sa,unsigned int bufsize) +{ + int fd; + fd = open_read((char *) fn); + if (fd == -1) { + if (errno == ENOENT) return 0; + return -1; + } + if (readclose(fd,sa,bufsize) == -1) return -1; + return 1; +} |