From 52c4606d2ca4ee3fe6d3baa44c943f78bac0409f Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Tue, 3 Aug 2010 23:09:16 +0200 Subject: [PATCH] Move read_file from test/util.c to util.c --- ccache.h | 1 + test/util.c | 31 ------------------------------- util.c | 31 +++++++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 31 deletions(-) diff --git a/ccache.h b/ccache.h index 45b4f98eb..a7d6a57e2 100644 --- a/ccache.h +++ b/ccache.h @@ -122,6 +122,7 @@ void *x_fmmap(const char *fname, off_t *size, const char *errstr); int x_munmap(void *addr, size_t length); int x_rename(const char *oldpath, const char *newpath); char *x_readlink(const char *path); +char *read_file(const char *path); /* ------------------------------------------------------------------------- */ /* stats.c */ diff --git a/test/util.c b/test/util.c index bc03923be..370fd4bde 100644 --- a/test/util.c +++ b/test/util.c @@ -50,34 +50,3 @@ create_file(const char *path, const char *content) fclose(f); } } - -/* Return the content of a text file, or NULL on error. Caller frees. */ -char * -read_file(const char *path) -{ - int fd, ret; - size_t pos = 0, allocated = 1024; - char *result = malloc(allocated); - - fd = open(path, O_RDONLY); - if (fd == -1) { - free(result); - return NULL; - } - ret = 0; - do { - pos += ret; - if (pos > allocated / 2) { - allocated *= 2; - result = realloc(result, allocated); - } - } while ((ret = read(fd, result + pos, allocated - pos - 1)) > 0); - close(fd); - if (ret == -1) { - free(result); - return NULL; - } - result[pos] = '\0'; - - return result; -} diff --git a/util.c b/util.c index 664c03581..42adccb41 100644 --- a/util.c +++ b/util.c @@ -1155,3 +1155,34 @@ x_readlink(const char *path) return buf; } #endif + +/* Return the content of a text file, or NULL on error. Caller frees. */ +char * +read_file(const char *path) +{ + int fd, ret; + size_t pos = 0, allocated = 1024; + char *result = malloc(allocated); + + fd = open(path, O_RDONLY); + if (fd == -1) { + free(result); + return NULL; + } + ret = 0; + do { + pos += ret; + if (pos > allocated / 2) { + allocated *= 2; + result = realloc(result, allocated); + } + } while ((ret = read(fd, result + pos, allocated - pos - 1)) > 0); + close(fd); + if (ret == -1) { + free(result); + return NULL; + } + result[pos] = '\0'; + + return result; +} -- 2.47.3