summaryrefslogtreecommitdiff
path: root/doc/slurp.c
diff options
context:
space:
mode:
Diffstat (limited to 'doc/slurp.c')
-rw-r--r--doc/slurp.c38
1 files changed, 38 insertions, 0 deletions
diff --git a/doc/slurp.c b/doc/slurp.c
new file mode 100644
index 0000000..f0b047d
--- /dev/null
+++ b/doc/slurp.c
@@ -0,0 +1,38 @@
+static void *p_slurp(const char *file, size_t *outsize)
+{
+ struct stat sb;
+ int ret = 0, fd = open(file, O_RDONLY | O_BINARY);
+ void *buf = NULL;
+ ssize_t rdret;
+
+ if (fd < 0) {
+ fprintf(stderr, "ERROR: Slurping %s failed: %s\n",
+ file, strerror(errno));
+ return NULL;
+ }
+ if (fstat(fd, &buf) < 0) {
+ ret = errno;
+ perror("fstat");
+ goto out;
+ }
+ *outsize = sb.st_size; /* truncate if need be */
+ buf = malloc(*outsize);
+ if (buf == NULL) {
+ ret = errno;
+ perror("malloc");
+ goto out;
+ }
+ rdret = read(fd, buf, *outsize);
+ if (rdret < 0) {
+ ret = errno;
+ perror("read");
+ free(buf);
+ } else {
+ *outsize = rdret;
+ }
+ out:
+ close(fd);
+ errno = ret;
+ return buf;
+}
+