diff -r 455eaa569767 -r d675c5ac6d07 librazor/util.c --- a/librazor/util.c Sun Jun 29 10:55:17 2008 +0100 +++ b/librazor/util.c Thu Jan 08 15:08:08 2009 +0000 @@ -1,6 +1,7 @@ /* * Copyright (C) 2008 Kristian Høgsberg * Copyright (C) 2008 Red Hat, Inc + * Copyright (C) 2009 J. Ali Harlow * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -17,16 +18,27 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ +#include "config.h" + #include #include +#include #include #include #include #include #include +#include +#if HAVE_SYS_MMAN_H +#include +#endif #include "razor-internal.h" +#ifndef O_BINARY +#define O_BINARY 0 +#endif + int razor_create_dir(const char *root, const char *path) { @@ -92,6 +104,65 @@ return 0; } +void * +razor_file_get_contents(const char *filename, size_t *length) +{ + int fd; + struct stat st; + void *addr; +#if !HAVE_SYS_MMAN_H + size_t nb; + ssize_t res; +#endif + + fd = open(filename, O_RDONLY | O_BINARY); + if (fd < 0) + return NULL; + + if (fstat(fd, &st) < 0) { + close(fd); + return NULL; + } + + *length = st.st_size; +#if HAVE_SYS_MMAN_H + addr = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); +#else + addr = malloc(st.st_size); + if (addr) { + nb = 0; + while(nb < st.st_size) { + res = read(fd, addr + nb, st.st_size - nb); + if (res <= 0) { + free(addr); + addr = NULL; + break; + } + nb += res; + } + } +#endif + close(fd); + +#if HAVE_SYS_MMAN_H + if (addr == MAP_FAILED) + addr = NULL; +#endif + + return addr; +} + +int +razor_file_free_contents(void *addr, size_t length) +{ +#if HAVE_SYS_MMAN_H + return munmap(addr, length); +#else + free(addr); + return 0; +#endif +} + struct qsort_context { size_t size; razor_compare_with_data_func_t compare;