This is used in GnuTLS, and I need it in Shishi now, so I thought that it should be a module. What do you think?
Possibly, there should be a xstrfile too, that uses xrealloc... Thanks! Index: modules/strfile =================================================================== RCS file: modules/strfile diff -N modules/strfile --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ modules/strfile 27 May 2006 10:11:38 -0000 @@ -0,0 +1,23 @@ +Description: +strfile() function: read the contents of a file into a string + +Files: +lib/strfile.h +lib/strfile.c +m4/strfile.m4 + +Depends-on: + +configure.ac: +gl_FUNC_STRFILE + +Makefile.am: + +Include: +"strfile.h" + +License: +GPL + +Maintainer: +Simon Josefsson Index: m4/strfile.m4 =================================================================== RCS file: m4/strfile.m4 diff -N m4/strfile.m4 --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ m4/strfile.m4 27 May 2006 10:11:38 -0000 @@ -0,0 +1,14 @@ +# strfile.m4 serial 1 +dnl Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc. +dnl This file is free software; the Free Software Foundation +dnl gives unlimited permission to copy and/or distribute it, +dnl with or without modifications, as long as this notice is preserved. + +AC_DEFUN([gl_FUNC_STRFILE], +[ + AC_LIBSOURCES([strfile.c, strfile.h]) + gl_PREREQ_STRFILE +]) + +# Prerequisites of lib/strfile.c. +AC_DEFUN([gl_PREREQ_STRFILE], [:]) Index: lib/strfile.h =================================================================== RCS file: lib/strfile.h diff -N lib/strfile.h --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ lib/strfile.h 27 May 2006 10:11:38 -0000 @@ -0,0 +1,26 @@ +/* strfile.h -- read file contents into a string + Copyright (C) 2006 Free Software Foundation, Inc. + + 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 + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + +#ifndef STRFILE_H +#define STRFILE_H + +/* Get size_t. */ +#include <stddef.h> + +extern char *strfile (const char *filename, size_t *length); + +#endif /* STRFILE_H */ Index: lib/strfile.c =================================================================== RCS file: lib/strfile.c diff -N lib/strfile.c --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ lib/strfile.c 27 May 2006 10:11:38 -0000 @@ -0,0 +1,69 @@ +/* strfile.c -- read file contents into a string + Copyright (C) 2006 Free Software Foundation, Inc. + + 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 + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include "strfile.h" + +#include <stdio.h> + +/* Open and read the contents of FILENAME's, and return a newly + allocated string with the content, and set LENGTH to the length of + the string. On errors, return NULL and sets errno. */ +char * +strfile (const char *filename, size_t *length) +{ + FILE *fh; + char *out = NULL; + size_t pos = 0; + + fh = fopen (filename, "r"); + if (!fh) + return NULL; + + do { + size_t nread; + char *tmp; + + tmp = realloc (out, pos + BUFSIZ); + if (!tmp) + { + int save_errno = errno; + free (out); + errno = save_errno; + return NULL; + } + out = tmp; + + nread = fread (out + pos, 1, BUFSIZ, fh); + pos += nread; + } while (!feof (fh) && !ferror (fh)); + + if (!(feof (fh) && fclose (fh))) + { + int save_errno = errno; + free (out); + errno = save_errno; + return NULL; + } + + *length = pos; + + return out; +}