bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: strfile: new module


From: Simon Josefsson
Subject: Re: strfile: new module
Date: Sat, 27 May 2006 12:35:21 +0200
User-agent: Gnus/5.110006 (No Gnus v0.6) Emacs/22.0.50 (gnu/linux)

Ralf Wildenhues <address@hidden> writes:

> Hi Simon,
>
> * Simon Josefsson wrote on Sat, May 27, 2006 at 12:12:59PM CEST:
>> +    int save_errno = errno;
>
> You are using errno but not including <errno.h>.

Hi Ralf!  Thanks.  I hadn't actually even compiled the module..
Improved patch below.

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:33:30 -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:
+LGPL
+
+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:33:30 -0000
@@ -0,0 +1,15 @@
+# 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])
+  AC_LIBOBJ([strfile])
+  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:33:30 -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:33:30 -0000
@@ -0,0 +1,71 @@
+/* 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>
+#include <stdlib.h>
+#include <errno.h>
+
+/* Open and read the contents of FILENAME, 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 = realloc (out, pos + BUFSIZ);
+
+    if (!tmp)
+      {
+       int save_errno = errno;
+       if (out)
+         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;
+}




reply via email to

[Prev in Thread] Current Thread [Next in Thread]