Commit 42392246 authored by Niels Möller's avatar Niels Möller
Browse files

(io_read_file_raw): New function, for reading an entire

file into a string. Uses string_buffer.

Rev: src/io.c:1.155
Rev: src/io.h:1.85
parent cc19ca74
......@@ -24,6 +24,7 @@
#include "io.h"
#include "format.h"
#include "string_buffer.h"
#include "werror.h"
#include "xalloc.h"
......@@ -852,6 +853,43 @@ read_raw(int fd, UINT32 length, UINT8 *data)
return NULL;
}
struct lsh_string *
io_read_file_raw(int fd, UINT32 guess)
{
struct string_buffer buffer;
string_buffer_init(&buffer, guess);
for (;;)
{
int res;
if (!buffer.left)
/* Roughly double the size of the buffer */
string_buffer_grow(&buffer,
buffer.partial->length + buffer.total + 100);
res = read(fd, buffer.current, buffer.left);
if (res < 0)
{
if (errno == EINTR)
continue;
string_buffer_clear(&buffer);
return NULL;
}
else if (!res)
{
/* EOF */
return string_buffer_final(&buffer, buffer.left);
}
assert( (unsigned) res <= buffer.left);
buffer.current += res;
buffer.left -= res;
}
}
/* Network utility functions */
/* Converts a string port number or service name to a port number.
......
......@@ -268,6 +268,9 @@ write_raw_with_poll(int fd, UINT32 length, const UINT8 *data);
const struct exception *
read_raw(int fd, UINT32 length, UINT8 *data);
struct lsh_string *
io_read_file_raw(int fd, UINT32 guess);
void io_set_nonblocking(int fd);
void io_set_blocking(int fd);
void io_set_close_on_exec(int fd);
......
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