Commit 7418a736 authored by Poul-Henning Kamp's avatar Poul-Henning Kamp

Add a vreadfile() utility function, which reads a file into malloc'ed

memory



git-svn-id: http://www.varnish-cache.org/svn/trunk/varnish-cache@3414 d4fa192b-c00b-0410-8231-f00ffab90ce4
parent e6d94b8b
......@@ -86,6 +86,7 @@ void varnish_version(const char *);
/* from libvarnish/vtmpfile.c */
int vtmpfile(char *);
char *vreadfile(int fd);
/*
* assert(), AN() and AZ() are static checks that should not happen.
......
......@@ -35,6 +35,9 @@
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include "libvarnish.h"
......@@ -74,3 +77,21 @@ vtmpfile(char *template)
}
/* 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);
}
......@@ -402,8 +402,6 @@ static struct source *
vcc_file_source(struct vsb *sb, const char *fn, int fd)
{
char *f;
int i;
struct stat st;
struct source *sp;
if (fd < 0) {
......@@ -414,19 +412,10 @@ vcc_file_source(struct vsb *sb, const char *fn, int fd)
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);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment