add uc_read_fd function

This commit is contained in:
Nils O. Selåsdal
2014-03-13 23:16:50 +01:00
parent c2fe3e5dc7
commit 5f06950fd0
2 changed files with 21 additions and 5 deletions
+8
View File
@@ -21,6 +21,14 @@ extern "C" {
char * char *
uc_read_file(const char *file_name, size_t *length, size_t max); uc_read_file(const char *file_name, size_t *length, size_t max);
/** Read the content of a file descriptor as a stream.
* @see uc_read_file
* This is similar but reads from a file descriptor,
* of any type as long as it supports the read() call.
*/
char *
uc_read_fd(int fd, size_t *length, size_t max);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
+13 -5
View File
@@ -10,9 +10,8 @@
#define CHUNK_SZ 1024 #define CHUNK_SZ 1024
char * char *
uc_read_file(const char *f, size_t *length, size_t max) uc_read_fd(int fd, size_t *length, size_t max)
{ {
int fd;
char *s = NULL; char *s = NULL;
char *p; char *p;
struct stat statb; struct stat statb;
@@ -20,9 +19,6 @@ uc_read_file(const char *f, size_t *length, size_t max)
*length = 0; *length = 0;
if ((fd = open(f, O_RDONLY)) == -1)
return NULL;
if (fstat(fd, &statb) == -1) { if (fstat(fd, &statb) == -1) {
goto out_close; goto out_close;
} }
@@ -85,3 +81,15 @@ out_close:
return s; return s;
} }
char *
uc_read_file(const char *f, size_t *length, size_t max)
{
int fd;
if ((fd = open(f, O_RDONLY)) == -1) {
*length = 0;
return NULL;
}
return uc_read_fd(fd, length, max);
}