diff options
author | Simon Josefsson <simon@josefsson.org> | 2006-11-16 09:04:54 +0000 |
---|---|---|
committer | Simon Josefsson <simon@josefsson.org> | 2006-11-16 09:04:54 +0000 |
commit | 7fb4b71c5c3f55e7686209d9993726dfaa219b27 (patch) | |
tree | 7511d256eb7c9560bbdb92f9604176e02e24d27c /gl | |
parent | 6cfa79db487699e3ba7116f56df26223cc798f30 (diff) | |
download | libtasn1-7fb4b71c5c3f55e7686209d9993726dfaa219b27.tar.gz libtasn1-7fb4b71c5c3f55e7686209d9993726dfaa219b27.tar.bz2 libtasn1-7fb4b71c5c3f55e7686209d9993726dfaa219b27.zip |
Use read_binary_file to read data, for Windows. Add self-test to test regressions.
Diffstat (limited to 'gl')
-rw-r--r-- | gl/Makefile.am | 20 | ||||
-rw-r--r-- | gl/getopt_.h | 9 | ||||
-rw-r--r-- | gl/gettext.h | 2 | ||||
-rw-r--r-- | gl/m4/gnulib-cache.m4 | 4 | ||||
-rw-r--r-- | gl/m4/gnulib-comp.m4 | 11 | ||||
-rw-r--r-- | gl/m4/read-file.m4 | 14 | ||||
-rw-r--r-- | gl/progname.h | 3 | ||||
-rw-r--r-- | gl/read-file.c | 136 | ||||
-rw-r--r-- | gl/read-file.h | 34 |
9 files changed, 214 insertions, 19 deletions
diff --git a/gl/Makefile.am b/gl/Makefile.am index 1074d81..74fcbf1 100644 --- a/gl/Makefile.am +++ b/gl/Makefile.am @@ -9,7 +9,7 @@ # the same distribution terms as the rest of that program. # # Generated by gnulib-tool. -# Reproduce by: gnulib-tool --import --dir=. --local-dir=gl/override --lib=libgnu --source-base=gl --m4-base=gl/m4 --doc-base=doc --aux-dir=. --libtool --macro-prefix=gl gendocs getopt maintainer-makefile memmove progname strdup version-etc-fsf +# Reproduce by: gnulib-tool --import --dir=. --local-dir=gl/override --lib=libgnu --source-base=gl --m4-base=gl/m4 --doc-base=doc --aux-dir=. --libtool --macro-prefix=gl gendocs getopt maintainer-makefile memmove progname read-file strdup version-etc-fsf AUTOMAKE_OPTIONS = 1.5 gnits @@ -67,6 +67,13 @@ libgnu_la_SOURCES += progname.h progname.c ## end gnulib module progname +## begin gnulib module read-file + + +EXTRA_DIST += read-file.c read-file.h + +## end gnulib module read-file + ## begin gnulib module strdup @@ -102,9 +109,8 @@ libgnu_la_SOURCES += version-etc-fsf.c mostlyclean-local: mostlyclean-generic - @test -z "$(MOSTLYCLEANDIRS)" || \ - for dir in $(MOSTLYCLEANDIRS); do \ - if test -d $$dir; then \ - echo "rmdir $$dir"; rmdir $$dir; \ - fi; \ - done + @for dir in '' $(MOSTLYCLEANDIRS); do \ + if test -n "$$dir" && test -d $$dir; then \ + echo "rmdir $$dir"; rmdir $$dir; \ + fi; \ + done diff --git a/gl/getopt_.h b/gl/getopt_.h index 3c406e5..27fce3d 100644 --- a/gl/getopt_.h +++ b/gl/getopt_.h @@ -1,5 +1,5 @@ /* Declarations for getopt. - Copyright (C) 1989-1994,1996-1999,2001,2003,2004,2005 + Copyright (C) 1989-1994,1996-1999,2001,2003,2004,2005,2006 Free Software Foundation, Inc. This file is part of the GNU C Library. @@ -196,9 +196,10 @@ struct option scanning, explicitly telling `getopt' that there are no more options. - If OPTS begins with `--', then non-option arguments are treated as - arguments to the option '\0'. This behavior is specific to the GNU - `getopt'. */ + If OPTS begins with `-', then non-option arguments are treated as + arguments to the option '\1'. This behavior is specific to the GNU + `getopt'. If OPTS begins with `+', or POSIXLY_CORRECT is set in + the environment, then do not permute arguments. */ extern int getopt (int ___argc, char *const *___argv, const char *__shortopts) __THROW; diff --git a/gl/gettext.h b/gl/gettext.h index 70e2c1c..f15eb6a 100644 --- a/gl/gettext.h +++ b/gl/gettext.h @@ -164,7 +164,7 @@ npgettext_aux (const char *domain, #include <string.h> #define _LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS \ - (__GNUC__ >= 3 || defined __cplusplus) + (__GNUC__ >= 3 || __GNUG__ >= 2 /* || __STDC_VERSION__ >= 199901L */ ) #if !_LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS #include <stdlib.h> diff --git a/gl/m4/gnulib-cache.m4 b/gl/m4/gnulib-cache.m4 index 43f6520..cfdca07 100644 --- a/gl/m4/gnulib-cache.m4 +++ b/gl/m4/gnulib-cache.m4 @@ -15,11 +15,11 @@ # Specification in the form of a command-line invocation: -# gnulib-tool --import --dir=. --local-dir=gl/override --lib=libgnu --source-base=gl --m4-base=gl/m4 --doc-base=doc --aux-dir=. --libtool --macro-prefix=gl gendocs getopt maintainer-makefile memmove progname strdup version-etc-fsf +# gnulib-tool --import --dir=. --local-dir=gl/override --lib=libgnu --source-base=gl --m4-base=gl/m4 --doc-base=doc --aux-dir=. --libtool --macro-prefix=gl gendocs getopt maintainer-makefile memmove progname read-file strdup version-etc-fsf # Specification in the form of a few gnulib-tool.m4 macro invocations: gl_LOCAL_DIR([gl/override]) -gl_MODULES([gendocs getopt maintainer-makefile memmove progname strdup version-etc-fsf]) +gl_MODULES([gendocs getopt maintainer-makefile memmove progname read-file strdup version-etc-fsf]) gl_AVOID([]) gl_SOURCE_BASE([gl]) gl_M4_BASE([gl/m4]) diff --git a/gl/m4/gnulib-comp.m4 b/gl/m4/gnulib-comp.m4 index 5f0a73e..1140f4f 100644 --- a/gl/m4/gnulib-comp.m4 +++ b/gl/m4/gnulib-comp.m4 @@ -38,6 +38,7 @@ AC_DEFUN([gl_INIT], gl_source_base='gl' gl_GETOPT gl_FUNC_MEMMOVE + gl_FUNC_READ_FILE gl_STDARG_H gl_FUNC_STRDUP gl_HEADER_UNISTD @@ -47,10 +48,9 @@ AC_DEFUN([gl_INIT], gl_libobjs= gl_ltlibobjs= if test -n "$gl_LIBOBJS"; then - for i in $gl_LIBOBJS; do - # Remove the extension. - sed_drop_objext='s/\.o$//;s/\.obj$//' - i=`echo "$i" | sed "$sed_drop_objext"` + # Remove the extension. + sed_drop_objext='s/\.o$//;s/\.obj$//' + for i in `for i in $gl_LIBOBJS; do echo "$i"; done | sed "$sed_drop_objext" | sort | uniq`; do gl_libobjs="$gl_libobjs $i.$ac_objext" gl_ltlibobjs="$gl_ltlibobjs $i.lo" done @@ -85,6 +85,8 @@ AC_DEFUN([gl_FILE_LIST], [ lib/memmove.c lib/progname.c lib/progname.h + lib/read-file.c + lib/read-file.h lib/strdup.c lib/strdup.h lib/version-etc-fsf.c @@ -92,6 +94,7 @@ AC_DEFUN([gl_FILE_LIST], [ lib/version-etc.h m4/getopt.m4 m4/memmove.m4 + m4/read-file.m4 m4/stdarg.m4 m4/strdup.m4 m4/unistd_h.m4 diff --git a/gl/m4/read-file.m4 b/gl/m4/read-file.m4 new file mode 100644 index 0000000..8d1b8f3 --- /dev/null +++ b/gl/m4/read-file.m4 @@ -0,0 +1,14 @@ +# read-file.m4 serial 2 +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_READ_FILE], +[ + AC_LIBOBJ([read-file]) + gl_PREREQ_READ_FILE +]) + +# Prerequisites of lib/read-file.c. +AC_DEFUN([gl_PREREQ_READ_FILE], [:]) diff --git a/gl/progname.h b/gl/progname.h index a5d9a04..206c2cc 100644 --- a/gl/progname.h +++ b/gl/progname.h @@ -1,5 +1,5 @@ /* Program name management. - Copyright (C) 2001-2004 Free Software Foundation, Inc. + Copyright (C) 2001-2004, 2006 Free Software Foundation, Inc. Written by Bruno Haible <haible@clisp.cons.org>, 2001. This program is free software; you can redistribute it and/or modify @@ -42,6 +42,7 @@ extern void set_program_name (const char *argv0); extern void set_program_name_and_installdir (const char *argv0, const char *orig_installprefix, const char *orig_installdir); +#undef set_program_name #define set_program_name(ARG0) \ set_program_name_and_installdir (ARG0, INSTALLPREFIX, INSTALLDIR) diff --git a/gl/read-file.c b/gl/read-file.c new file mode 100644 index 0000000..1045b49 --- /dev/null +++ b/gl/read-file.c @@ -0,0 +1,136 @@ +/* read-file.c -- read file contents into a string + Copyright (C) 2006 Free Software Foundation, Inc. + Written by Simon Josefsson and Bruno Haible. + + 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. */ + +#include <config.h> + +#include "read-file.h" + +/* Get realloc, free. */ +#include <stdlib.h> + +/* Get errno. */ +#include <errno.h> + +/* Read a STREAM and return a newly allocated string with the content, + and set *LENGTH to the length of the string. The string is + zero-terminated, but the terminating zero byte is not counted in + *LENGTH. On errors, *LENGTH is undefined, errno preserves the + values set by system functions (if any), and NULL is returned. */ +char * +fread_file (FILE * stream, size_t * length) +{ + char *buf = NULL; + size_t alloc = 0; + size_t size = 0; + int save_errno; + + for (;;) + { + size_t count; + size_t requested; + + if (size + BUFSIZ + 1 > alloc) + { + char *new_buf; + + alloc += alloc / 2; + if (alloc < size + BUFSIZ + 1) + alloc = size + BUFSIZ + 1; + + new_buf = realloc (buf, alloc); + if (!new_buf) + { + save_errno = errno; + break; + } + + buf = new_buf; + } + + requested = alloc - size - 1; + count = fread (buf + size, 1, requested, stream); + size += count; + + if (count != requested) + { + save_errno = errno; + if (ferror (stream)) + break; + buf[size] = '\0'; + *length = size; + return buf; + } + } + + free (buf); + errno = save_errno; + return NULL; +} + +static char * +internal_read_file (const char *filename, size_t * length, const char *mode) +{ + FILE *stream = fopen (filename, mode); + char *out; + int save_errno; + + if (!stream) + return NULL; + + out = fread_file (stream, length); + + save_errno = errno; + + if (fclose (stream) != 0) + { + if (out) + { + save_errno = errno; + free (out); + } + errno = save_errno; + return NULL; + } + + return out; +} + +/* 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. The string is zero-terminated, but the terminating + zero byte is not counted in *LENGTH. On errors, *LENGTH is + undefined, errno preserves the values set by system functions (if + any), and NULL is returned. */ +char * +read_file (const char *filename, size_t * length) +{ + return internal_read_file (filename, length, "r"); +} + +/* Open (on non-POSIX systems, in binary mode) and read the contents + of FILENAME, and return a newly allocated string with the content, + and set LENGTH to the length of the string. The string is + zero-terminated, but the terminating zero byte is not counted in + the LENGTH variable. On errors, *LENGTH is undefined, errno + preserves the values set by system functions (if any), and NULL is + returned. */ +char * +read_binary_file (const char *filename, size_t * length) +{ + return internal_read_file (filename, length, "rb"); +} diff --git a/gl/read-file.h b/gl/read-file.h new file mode 100644 index 0000000..f8c712e --- /dev/null +++ b/gl/read-file.h @@ -0,0 +1,34 @@ +/* read-file.h -- read file contents into a string + Copyright (C) 2006 Free Software Foundation, Inc. + Written by Simon Josefsson. + + 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 READ_FILE_H +#define READ_FILE_H + +/* Get size_t. */ +#include <stddef.h> + +/* Get FILE. */ +#include <stdio.h> + +extern char *fread_file (FILE * stream, size_t * length); + +extern char *read_file (const char *filename, size_t * length); + +extern char *read_binary_file (const char *filename, size_t * length); + +#endif /* READ_FILE_H */ |