diff --git a/src/unix/core.c b/src/unix/core.c index 3997d09d..5a8987e7 100644 --- a/src/unix/core.c +++ b/src/unix/core.c @@ -427,6 +427,22 @@ int uv__socket(int domain, int type, int protocol) { return sockfd; } +/* get a file pointer to a file in read-only and close-on-exec mode */ +FILE* uv__open_file(const char* path) { + int fd; + FILE* fp; + + fd = uv__open_cloexec(path, O_RDONLY); + if (fd == -1) + return NULL; + + fp = fdopen(fd, "r"); + if (fp == NULL) + uv__close(fd); + + return fp; +} + int uv__accept(int sockfd) { int peerfd; diff --git a/src/unix/internal.h b/src/unix/internal.h index bc5ea4dd..79dab40e 100644 --- a/src/unix/internal.h +++ b/src/unix/internal.h @@ -28,6 +28,7 @@ #include /* abort */ #include /* strrchr */ #include /* O_CLOEXEC, may be */ +#include #if defined(__STRICT_ANSI__) # define inline __inline @@ -246,6 +247,8 @@ void uv__timer_close(uv_timer_t* handle); void uv__udp_close(uv_udp_t* handle); void uv__udp_finish_close(uv_udp_t* handle); uv_handle_type uv__handle_type(int fd); +FILE* uv__open_file(const char* path); + #if defined(__APPLE__) int uv___stream_fd(const uv_stream_t* handle);