mirror of
https://github.com/cuberite/libevent.git
synced 2025-09-19 01:04:58 -04:00
Refactor code from evdns into a new internal "read a file" function.
This commit is contained in:
parent
eaaf27f1f7
commit
0f7144fd8b
62
evutil.c
62
evutil.c
@ -67,6 +67,7 @@
|
||||
#include <sys/timeb.h>
|
||||
#include <time.h>
|
||||
#endif
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include "event2/util.h"
|
||||
#include "util-internal.h"
|
||||
@ -76,6 +77,67 @@
|
||||
#include "strlcpy-internal.h"
|
||||
#include "ipv6-internal.h"
|
||||
|
||||
/**
|
||||
Read the contents of 'filename' into a newly allocated NUL-terminated
|
||||
string. Set *content_out to hold this string, and *len_out to hold its
|
||||
length (not including the appended NUL). If 'is_binary', open the file in
|
||||
binary mode.
|
||||
|
||||
Returns 0 on success, -1 if the open fails, and -2 for all other failures.
|
||||
|
||||
Used internally only; may go away in a future version.
|
||||
*/
|
||||
int
|
||||
evutil_read_file(const char *filename, char **content_out, size_t *len_out,
|
||||
int is_binary)
|
||||
{
|
||||
int fd, r;
|
||||
struct stat st;
|
||||
char *mem;
|
||||
size_t read_so_far=0;
|
||||
int mode = O_RDONLY;
|
||||
|
||||
EVUTIL_ASSERT(content_out);
|
||||
EVUTIL_ASSERT(len_out);
|
||||
*content_out = NULL;
|
||||
*len_out = 0;
|
||||
|
||||
#ifdef O_BINARY
|
||||
if (is_binary)
|
||||
mode |= O_BINARY;
|
||||
#endif
|
||||
|
||||
fd = open(filename, mode);
|
||||
if (fd < 0)
|
||||
return -1;
|
||||
if (fstat(fd, &st)) {
|
||||
close(fd);
|
||||
return -2;
|
||||
}
|
||||
mem = mm_malloc(st.st_size + 1);
|
||||
if (!mem) {
|
||||
close(fd);
|
||||
return -2;
|
||||
}
|
||||
read_so_far = 0;
|
||||
while ((r = read(fd, mem+read_so_far, st.st_size - read_so_far)) > 0) {
|
||||
read_so_far += r;
|
||||
if (read_so_far >= st.st_size)
|
||||
break;
|
||||
EVUTIL_ASSERT(read_so_far < st.st_size);
|
||||
}
|
||||
if (r < 0) {
|
||||
close(fd);
|
||||
mm_free(mem);
|
||||
return -2;
|
||||
}
|
||||
mem[read_so_far] = 0;
|
||||
|
||||
*len_out = read_so_far;
|
||||
*content_out = mem;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
evutil_socketpair(int family, int type, int protocol, evutil_socket_t fd[2])
|
||||
{
|
||||
|
@ -136,6 +136,8 @@ extern const char EVUTIL_TOLOWER_TABLE[];
|
||||
#define EVUTIL_UPCAST(ptr, type, field) \
|
||||
((type *)(((char*)(ptr)) - evutil_offsetof(type, field)))
|
||||
|
||||
int evutil_read_file(const char *filename, char **content_out, size_t *len_out,
|
||||
int is_binary);
|
||||
|
||||
int evutil_socket_connect(evutil_socket_t *fd_ptr, struct sockaddr *sa, int socklen);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user