#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
+#include <unistd.h>
+
+#include <sys/stat.h>
#include "libvarnish.h"
}
/* not reached */
}
+
+char *
+vreadfile(int fd)
+{
+ struct stat st;
+ char *f;
+ int i;
+
+ assert(0 == fstat(fd, &st));
+ if (!S_ISREG(st.st_mode))
+ return (NULL);
+ f = malloc(st.st_size + 1);
+ assert(f != NULL);
+ i = read(fd, f, st.st_size);
+ assert(i == st.st_size);
+ f[i] = '\0';
+ return (f);
+}
vcc_file_source(struct vsb *sb, const char *fn, int fd)
{
char *f;
- int i;
- struct stat st;
struct source *sp;
if (fd < 0) {
return (NULL);
}
}
- assert(0 == fstat(fd, &st));
- if (! S_ISREG(st.st_mode)) {
- vsb_printf(sb, "File '%s' is not a regular file\n", fn);
- AZ(close(fd));
- return (NULL);
- }
- f = malloc(st.st_size + 1);
- assert(f != NULL);
- i = read(fd, f, st.st_size);
- assert(i == st.st_size);
+ f = vreadfile(fd);
+ AN(f);
AZ(close(fd));
- f[i] = '\0';
- sp = vcc_new_source(f, f + i, fn);
+ sp = vcc_new_source(f, NULL, fn);
sp->freeit = f;
return (sp);
}