summaryrefslogtreecommitdiff
path: root/gl
diff options
context:
space:
mode:
authorNikos Mavrogiannopoulos <nmav@redhat.com>2014-04-09 14:39:30 +0200
committerNikos Mavrogiannopoulos <nmav@redhat.com>2014-04-09 14:39:30 +0200
commit5008cef3a6a5d40e30a356e6e5a317550d9a554f (patch)
treeab7fb45dec47ac03c23fc98bbbd24ad2df0eccc2 /gl
parent49043202dde3fb7e0f3b48b9e7288e930ef81d1c (diff)
downloadgnutls-5008cef3a6a5d40e30a356e6e5a317550d9a554f.tar.gz
Added getline() in gnulib.
Diffstat (limited to 'gl')
-rw-r--r--gl/Makefile.am20
-rw-r--r--gl/getdelim.c135
-rw-r--r--gl/getline.c27
-rw-r--r--gl/m4/getdelim.m488
-rw-r--r--gl/m4/getline.m496
-rw-r--r--gl/m4/gnulib-cache.m43
-rw-r--r--gl/m4/gnulib-comp.m422
-rw-r--r--gl/tests/Makefile.am18
-rw-r--r--gl/tests/test-getdelim.c94
-rw-r--r--gl/tests/test-getline.c94
10 files changed, 595 insertions, 2 deletions
diff --git a/gl/Makefile.am b/gl/Makefile.am
index dd42ba7f39..47d9944a17 100644
--- a/gl/Makefile.am
+++ b/gl/Makefile.am
@@ -21,7 +21,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 --tests-base=gl/tests --aux-dir=build-aux --with-tests --avoid=alignof-tests --avoid=lock-tests --avoid=lseek-tests --lgpl=2 --no-conditional-dependencies --libtool --macro-prefix=gl --no-vc-files alloca base64 byteswap c-ctype extensions func gendocs gettext gettimeofday hash-pjw-bare havelib iconv intprops lib-msvc-compat lib-symbol-versions maintainer-makefile manywarnings memmem-simple minmax netdb netinet_in pmccabe2html read-file snprintf stdint strcase strndup strtok_r strverscmp sys_socket sys_stat time_r u64 unistd valgrind-tests vasprintf vsnprintf warnings
+# Reproduce by: gnulib-tool --import --dir=. --local-dir=gl/override --lib=libgnu --source-base=gl --m4-base=gl/m4 --doc-base=doc --tests-base=gl/tests --aux-dir=build-aux --with-tests --avoid=alignof-tests --avoid=lock-tests --avoid=lseek-tests --lgpl=2 --no-conditional-dependencies --libtool --macro-prefix=gl --no-vc-files alloca base64 byteswap c-ctype extensions func gendocs getline gettext gettimeofday hash-pjw-bare havelib iconv intprops lib-msvc-compat lib-symbol-versions maintainer-makefile manywarnings memmem-simple minmax netdb netinet_in pmccabe2html read-file snprintf stdint strcase strndup strtok_r strverscmp sys_socket sys_stat time_r u64 unistd valgrind-tests vasprintf vsnprintf warnings
AUTOMAKE_OPTIONS = 1.9.6 gnits
@@ -231,6 +231,24 @@ EXTRA_DIST += $(top_srcdir)/build-aux/gendocs.sh
## end gnulib module gendocs
+## begin gnulib module getdelim
+
+
+EXTRA_DIST += getdelim.c
+
+EXTRA_libgnu_la_SOURCES += getdelim.c
+
+## end gnulib module getdelim
+
+## begin gnulib module getline
+
+
+EXTRA_DIST += getline.c
+
+EXTRA_libgnu_la_SOURCES += getline.c
+
+## end gnulib module getline
+
## begin gnulib module gettext
# If your project uses "gettextize --intl" to put a source-code
diff --git a/gl/getdelim.c b/gl/getdelim.c
new file mode 100644
index 0000000000..7762e52176
--- /dev/null
+++ b/gl/getdelim.c
@@ -0,0 +1,135 @@
+/* getdelim.c --- Implementation of replacement getdelim function.
+ Copyright (C) 1994, 1996-1998, 2001, 2003, 2005-2014 Free Software
+ Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundation; either version 2.1, 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
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with this program; if not, see <http://www.gnu.org/licenses/>. */
+
+/* Ported from glibc by Simon Josefsson. */
+
+/* Don't use __attribute__ __nonnull__ in this compilation unit. Otherwise gcc
+ optimizes away the lineptr == NULL || n == NULL || fp == NULL tests below. */
+#define _GL_ARG_NONNULL(params)
+
+#include <config.h>
+
+#include <stdio.h>
+
+#include <limits.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <errno.h>
+
+#ifndef SSIZE_MAX
+# define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))
+#endif
+
+#if USE_UNLOCKED_IO
+# include "unlocked-io.h"
+# define getc_maybe_unlocked(fp) getc(fp)
+#elif !HAVE_FLOCKFILE || !HAVE_FUNLOCKFILE || !HAVE_DECL_GETC_UNLOCKED
+# undef flockfile
+# undef funlockfile
+# define flockfile(x) ((void) 0)
+# define funlockfile(x) ((void) 0)
+# define getc_maybe_unlocked(fp) getc(fp)
+#else
+# define getc_maybe_unlocked(fp) getc_unlocked(fp)
+#endif
+
+/* Read up to (and including) a DELIMITER from FP into *LINEPTR (and
+ NUL-terminate it). *LINEPTR is a pointer returned from malloc (or
+ NULL), pointing to *N characters of space. It is realloc'ed as
+ necessary. Returns the number of characters read (not including
+ the null terminator), or -1 on error or EOF. */
+
+ssize_t
+getdelim (char **lineptr, size_t *n, int delimiter, FILE *fp)
+{
+ ssize_t result;
+ size_t cur_len = 0;
+
+ if (lineptr == NULL || n == NULL || fp == NULL)
+ {
+ errno = EINVAL;
+ return -1;
+ }
+
+ flockfile (fp);
+
+ if (*lineptr == NULL || *n == 0)
+ {
+ char *new_lineptr;
+ *n = 120;
+ new_lineptr = (char *) realloc (*lineptr, *n);
+ if (new_lineptr == NULL)
+ {
+ result = -1;
+ goto unlock_return;
+ }
+ *lineptr = new_lineptr;
+ }
+
+ for (;;)
+ {
+ int i;
+
+ i = getc_maybe_unlocked (fp);
+ if (i == EOF)
+ {
+ result = -1;
+ break;
+ }
+
+ /* Make enough space for len+1 (for final NUL) bytes. */
+ if (cur_len + 1 >= *n)
+ {
+ size_t needed_max =
+ SSIZE_MAX < SIZE_MAX ? (size_t) SSIZE_MAX + 1 : SIZE_MAX;
+ size_t needed = 2 * *n + 1; /* Be generous. */
+ char *new_lineptr;
+
+ if (needed_max < needed)
+ needed = needed_max;
+ if (cur_len + 1 >= needed)
+ {
+ result = -1;
+ errno = EOVERFLOW;
+ goto unlock_return;
+ }
+
+ new_lineptr = (char *) realloc (*lineptr, needed);
+ if (new_lineptr == NULL)
+ {
+ result = -1;
+ goto unlock_return;
+ }
+
+ *lineptr = new_lineptr;
+ *n = needed;
+ }
+
+ (*lineptr)[cur_len] = i;
+ cur_len++;
+
+ if (i == delimiter)
+ break;
+ }
+ (*lineptr)[cur_len] = '\0';
+ result = cur_len ? cur_len : result;
+
+ unlock_return:
+ funlockfile (fp); /* doesn't set errno */
+
+ return result;
+}
diff --git a/gl/getline.c b/gl/getline.c
new file mode 100644
index 0000000000..c0d652e44b
--- /dev/null
+++ b/gl/getline.c
@@ -0,0 +1,27 @@
+/* getline.c --- Implementation of replacement getline function.
+ Copyright (C) 2005-2007, 2009-2014 Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundation; either version 2.1, 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
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with this program; if not, see <http://www.gnu.org/licenses/>. */
+
+/* Written by Simon Josefsson. */
+
+#include <config.h>
+
+#include <stdio.h>
+
+ssize_t
+getline (char **lineptr, size_t *n, FILE *stream)
+{
+ return getdelim (lineptr, n, '\n', stream);
+}
diff --git a/gl/m4/getdelim.m4 b/gl/m4/getdelim.m4
new file mode 100644
index 0000000000..c763994186
--- /dev/null
+++ b/gl/m4/getdelim.m4
@@ -0,0 +1,88 @@
+# getdelim.m4 serial 10
+
+dnl Copyright (C) 2005-2007, 2009-2014 Free Software Foundation, Inc.
+dnl
+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_PREREQ([2.59])
+
+AC_DEFUN([gl_FUNC_GETDELIM],
+[
+ AC_REQUIRE([gl_STDIO_H_DEFAULTS])
+
+ dnl Persuade glibc <stdio.h> to declare getdelim().
+ AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
+
+ AC_CHECK_DECLS_ONCE([getdelim])
+
+ AC_CHECK_FUNCS_ONCE([getdelim])
+ if test $ac_cv_func_getdelim = yes; then
+ HAVE_GETDELIM=1
+ dnl Found it in some library. Verify that it works.
+ AC_CACHE_CHECK([for working getdelim function], [gl_cv_func_working_getdelim],
+ [echo fooNbarN | tr -d '\012' | tr N '\012' > conftest.data
+ AC_RUN_IFELSE([AC_LANG_SOURCE([[
+# include <stdio.h>
+# include <stdlib.h>
+# include <string.h>
+ int main ()
+ {
+ FILE *in = fopen ("./conftest.data", "r");
+ if (!in)
+ return 1;
+ {
+ /* Test result for a NULL buffer and a zero size.
+ Based on a test program from Karl Heuer. */
+ char *line = NULL;
+ size_t siz = 0;
+ int len = getdelim (&line, &siz, '\n', in);
+ if (!(len == 4 && line && strcmp (line, "foo\n") == 0))
+ return 2;
+ }
+ {
+ /* Test result for a NULL buffer and a non-zero size.
+ This crashes on FreeBSD 8.0. */
+ char *line = NULL;
+ size_t siz = (size_t)(~0) / 4;
+ if (getdelim (&line, &siz, '\n', in) == -1)
+ return 3;
+ }
+ return 0;
+ }
+ ]])], [gl_cv_func_working_getdelim=yes] dnl The library version works.
+ , [gl_cv_func_working_getdelim=no] dnl The library version does NOT work.
+ , dnl We're cross compiling. Assume it works on glibc2 systems.
+ [AC_EGREP_CPP([Lucky GNU user],
+ [
+#include <features.h>
+#ifdef __GNU_LIBRARY__
+ #if (__GLIBC__ >= 2) && !defined __UCLIBC__
+ Lucky GNU user
+ #endif
+#endif
+ ],
+ [gl_cv_func_working_getdelim="guessing yes"],
+ [gl_cv_func_working_getdelim="guessing no"])]
+ )])
+ case "$gl_cv_func_working_getdelim" in
+ *no)
+ REPLACE_GETDELIM=1
+ ;;
+ esac
+ else
+ HAVE_GETDELIM=0
+ fi
+
+ if test $ac_cv_have_decl_getdelim = no; then
+ HAVE_DECL_GETDELIM=0
+ fi
+])
+
+# Prerequisites of lib/getdelim.c.
+AC_DEFUN([gl_PREREQ_GETDELIM],
+[
+ AC_CHECK_FUNCS([flockfile funlockfile])
+ AC_CHECK_DECLS([getc_unlocked])
+])
diff --git a/gl/m4/getline.m4 b/gl/m4/getline.m4
new file mode 100644
index 0000000000..0330666bc7
--- /dev/null
+++ b/gl/m4/getline.m4
@@ -0,0 +1,96 @@
+# getline.m4 serial 26
+
+dnl Copyright (C) 1998-2003, 2005-2007, 2009-2014 Free Software Foundation,
+dnl Inc.
+dnl
+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_PREREQ([2.59])
+
+dnl See if there's a working, system-supplied version of the getline function.
+dnl We can't just do AC_REPLACE_FUNCS([getline]) because some systems
+dnl have a function by that name in -linet that doesn't have anything
+dnl to do with the function we need.
+AC_DEFUN([gl_FUNC_GETLINE],
+[
+ AC_REQUIRE([gl_STDIO_H_DEFAULTS])
+
+ dnl Persuade glibc <stdio.h> to declare getline().
+ AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
+
+ AC_CHECK_DECLS_ONCE([getline])
+
+ gl_getline_needs_run_time_check=no
+ AC_CHECK_FUNC([getline],
+ [dnl Found it in some library. Verify that it works.
+ gl_getline_needs_run_time_check=yes],
+ [am_cv_func_working_getline=no])
+ if test $gl_getline_needs_run_time_check = yes; then
+ AC_CACHE_CHECK([for working getline function], [am_cv_func_working_getline],
+ [echo fooNbarN | tr -d '\012' | tr N '\012' > conftest.data
+ AC_RUN_IFELSE([AC_LANG_SOURCE([[
+# include <stdio.h>
+# include <stdlib.h>
+# include <string.h>
+ int main ()
+ {
+ FILE *in = fopen ("./conftest.data", "r");
+ if (!in)
+ return 1;
+ {
+ /* Test result for a NULL buffer and a zero size.
+ Based on a test program from Karl Heuer. */
+ char *line = NULL;
+ size_t siz = 0;
+ int len = getline (&line, &siz, in);
+ if (!(len == 4 && line && strcmp (line, "foo\n") == 0))
+ return 2;
+ }
+ {
+ /* Test result for a NULL buffer and a non-zero size.
+ This crashes on FreeBSD 8.0. */
+ char *line = NULL;
+ size_t siz = (size_t)(~0) / 4;
+ if (getline (&line, &siz, in) == -1)
+ return 3;
+ }
+ return 0;
+ }
+ ]])], [am_cv_func_working_getline=yes] dnl The library version works.
+ , [am_cv_func_working_getline=no] dnl The library version does NOT work.
+ , dnl We're cross compiling. Assume it works on glibc2 systems.
+ [AC_EGREP_CPP([Lucky GNU user],
+ [
+#include <features.h>
+#ifdef __GNU_LIBRARY__
+ #if (__GLIBC__ >= 2) && !defined __UCLIBC__
+ Lucky GNU user
+ #endif
+#endif
+ ],
+ [am_cv_func_working_getline="guessing yes"],
+ [am_cv_func_working_getline="guessing no"])]
+ )])
+ fi
+
+ if test $ac_cv_have_decl_getline = no; then
+ HAVE_DECL_GETLINE=0
+ fi
+
+ case "$am_cv_func_working_getline" in
+ *no)
+ dnl Set REPLACE_GETLINE always: Even if we have not found the broken
+ dnl getline function among $LIBS, it may exist in libinet and the
+ dnl executable may be linked with -linet.
+ REPLACE_GETLINE=1
+ ;;
+ esac
+])
+
+# Prerequisites of lib/getline.c.
+AC_DEFUN([gl_PREREQ_GETLINE],
+[
+ :
+])
diff --git a/gl/m4/gnulib-cache.m4 b/gl/m4/gnulib-cache.m4
index b8047930e8..934e144724 100644
--- a/gl/m4/gnulib-cache.m4
+++ b/gl/m4/gnulib-cache.m4
@@ -27,7 +27,7 @@
# 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 --tests-base=gl/tests --aux-dir=build-aux --with-tests --avoid=alignof-tests --avoid=lock-tests --avoid=lseek-tests --lgpl=2 --no-conditional-dependencies --libtool --macro-prefix=gl --no-vc-files alloca base64 byteswap c-ctype extensions func gendocs gettext gettimeofday hash-pjw-bare havelib iconv intprops lib-msvc-compat lib-symbol-versions maintainer-makefile manywarnings memmem-simple minmax netdb netinet_in pmccabe2html read-file snprintf stdint strcase strndup strtok_r strverscmp sys_socket sys_stat time_r u64 unistd valgrind-tests vasprintf vsnprintf warnings
+# gnulib-tool --import --dir=. --local-dir=gl/override --lib=libgnu --source-base=gl --m4-base=gl/m4 --doc-base=doc --tests-base=gl/tests --aux-dir=build-aux --with-tests --avoid=alignof-tests --avoid=lock-tests --avoid=lseek-tests --lgpl=2 --no-conditional-dependencies --libtool --macro-prefix=gl --no-vc-files alloca base64 byteswap c-ctype extensions func gendocs getline gettext gettimeofday hash-pjw-bare havelib iconv intprops lib-msvc-compat lib-symbol-versions maintainer-makefile manywarnings memmem-simple minmax netdb netinet_in pmccabe2html read-file snprintf stdint strcase strndup strtok_r strverscmp sys_socket sys_stat time_r u64 unistd valgrind-tests vasprintf vsnprintf warnings
# Specification in the form of a few gnulib-tool.m4 macro invocations:
gl_LOCAL_DIR([gl/override])
@@ -39,6 +39,7 @@ gl_MODULES([
extensions
func
gendocs
+ getline
gettext
gettimeofday
hash-pjw-bare
diff --git a/gl/m4/gnulib-comp.m4 b/gl/m4/gnulib-comp.m4
index 64dc2b3eed..613c7dea6c 100644
--- a/gl/m4/gnulib-comp.m4
+++ b/gl/m4/gnulib-comp.m4
@@ -78,6 +78,10 @@ AC_DEFUN([gl_EARLY],
# Code from module func-tests:
# Code from module fwrite-tests:
# Code from module gendocs:
+ # Code from module getdelim:
+ # Code from module getdelim-tests:
+ # Code from module getline:
+ # Code from module getline-tests:
# Code from module getpagesize:
# Code from module gettext:
# Code from module gettext-h:
@@ -234,6 +238,18 @@ AC_SUBST([LTALLOCA])
fi
gl_STDIO_MODULE_INDICATOR([ftello])
gl_FUNC
+ gl_FUNC_GETDELIM
+ if test $HAVE_GETDELIM = 0 || test $REPLACE_GETDELIM = 1; then
+ AC_LIBOBJ([getdelim])
+ gl_PREREQ_GETDELIM
+ fi
+ gl_STDIO_MODULE_INDICATOR([getdelim])
+ gl_FUNC_GETLINE
+ if test $REPLACE_GETLINE = 1; then
+ AC_LIBOBJ([getline])
+ gl_PREREQ_GETLINE
+ fi
+ gl_STDIO_MODULE_INDICATOR([getline])
dnl you must add AM_GNU_GETTEXT([external]) or similar to configure.ac.
AM_GNU_GETTEXT_VERSION([0.18.1])
AC_SUBST([LIBINTL])
@@ -574,6 +590,8 @@ AC_DEFUN([gl_FILE_LIST], [
lib/fstat.c
lib/ftell.c
lib/ftello.c
+ lib/getdelim.c
+ lib/getline.c
lib/gettext.h
lib/gettimeofday.c
lib/hash-pjw-bare.c
@@ -657,6 +675,8 @@ AC_DEFUN([gl_FILE_LIST], [
m4/ftell.m4
m4/ftello.m4
m4/func.m4
+ m4/getdelim.m4
+ m4/getline.m4
m4/getpagesize.m4
m4/gettext.m4
m4/gettimeofday.m4
@@ -774,6 +794,8 @@ AC_DEFUN([gl_FILE_LIST], [
tests/test-ftello4.sh
tests/test-func.c
tests/test-fwrite.c
+ tests/test-getdelim.c
+ tests/test-getline.c
tests/test-gettimeofday.c
tests/test-iconv.c
tests/test-init.sh
diff --git a/gl/tests/Makefile.am b/gl/tests/Makefile.am
index 3cbf0dc3e0..8cd3036885 100644
--- a/gl/tests/Makefile.am
+++ b/gl/tests/Makefile.am
@@ -257,6 +257,24 @@ EXTRA_DIST += test-fwrite.c signature.h macros.h
## end gnulib module fwrite-tests
+## begin gnulib module getdelim-tests
+
+TESTS += test-getdelim
+check_PROGRAMS += test-getdelim
+MOSTLYCLEANFILES += test-getdelim.txt
+EXTRA_DIST += test-getdelim.c signature.h macros.h
+
+## end gnulib module getdelim-tests
+
+## begin gnulib module getline-tests
+
+TESTS += test-getline
+check_PROGRAMS += test-getline
+MOSTLYCLEANFILES += test-getline.txt
+EXTRA_DIST += test-getline.c signature.h macros.h
+
+## end gnulib module getline-tests
+
## begin gnulib module getpagesize
diff --git a/gl/tests/test-getdelim.c b/gl/tests/test-getdelim.c
new file mode 100644
index 0000000000..76aba827db
--- /dev/null
+++ b/gl/tests/test-getdelim.c
@@ -0,0 +1,94 @@
+/* Test of getdelim() function.
+ Copyright (C) 2007-2014 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 3, 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, see <http://www.gnu.org/licenses/>. */
+
+/* Written by Eric Blake <ebb9@byu.net>, 2007. */
+
+#include <config.h>
+
+#include <stdio.h>
+
+#include "signature.h"
+SIGNATURE_CHECK (getdelim, ssize_t, (char **, size_t *, int, FILE *));
+
+#include <stdlib.h>
+#include <string.h>
+
+#include "macros.h"
+
+int
+main (void)
+{
+ FILE *f;
+ char *line;
+ size_t len;
+ ssize_t result;
+
+ /* Create test file. */
+ f = fopen ("test-getdelim.txt", "wb");
+ if (!f || fwrite ("anAnbcnd\0f", 1, 10, f) != 10 || fclose (f) != 0)
+ {
+ fputs ("Failed to create sample file.\n", stderr);
+ remove ("test-getdelim.txt");
+ return 1;
+ }
+ f = fopen ("test-getdelim.txt", "rb");
+ if (!f)
+ {
+ fputs ("Failed to reopen sample file.\n", stderr);
+ remove ("test-getdelim.txt");
+ return 1;
+ }
+
+ /* Test initial allocation, which must include trailing NUL. */
+ line = NULL;
+ len = 0;
+ result = getdelim (&line, &len, 'n', f);
+ ASSERT (result == 2);
+ ASSERT (strcmp (line, "an") == 0);
+ ASSERT (2 < len);
+ free (line);
+
+ /* Test initial allocation again, with line = NULL and len != 0. */
+ line = NULL;
+ len = (size_t)(~0) / 4;
+ result = getdelim (&line, &len, 'n', f);
+ ASSERT (result == 2);
+ ASSERT (strcmp (line, "An") == 0);
+ ASSERT (2 < len);
+ free (line);
+
+ /* Test growth of buffer. */
+ line = malloc (1);
+ len = 1;
+ result = getdelim (&line, &len, 'n', f);
+ ASSERT (result == 3);
+ ASSERT (strcmp (line, "bcn") == 0);
+ ASSERT (3 < len);
+
+ /* Test embedded NULs and EOF behavior. */
+ result = getdelim (&line, &len, 'n', f);
+ ASSERT (result == 3);
+ ASSERT (memcmp (line, "d\0f", 4) == 0);
+ ASSERT (3 < len);
+
+ result = getdelim (&line, &len, 'n', f);
+ ASSERT (result == -1);
+
+ free (line);
+ fclose (f);
+ remove ("test-getdelim.txt");
+ return 0;
+}
diff --git a/gl/tests/test-getline.c b/gl/tests/test-getline.c
new file mode 100644
index 0000000000..4dbc485e87
--- /dev/null
+++ b/gl/tests/test-getline.c
@@ -0,0 +1,94 @@
+/* Test of getline() function.
+ Copyright (C) 2007-2014 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 3, 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, see <http://www.gnu.org/licenses/>. */
+
+/* Written by Eric Blake <ebb9@byu.net>, 2007. */
+
+#include <config.h>
+
+#include <stdio.h>
+
+#include "signature.h"
+SIGNATURE_CHECK (getline, ssize_t, (char **, size_t *, FILE *));
+
+#include <stdlib.h>
+#include <string.h>
+
+#include "macros.h"
+
+int
+main (void)
+{
+ FILE *f;
+ char *line;
+ size_t len;
+ ssize_t result;
+
+ /* Create test file. */
+ f = fopen ("test-getline.txt", "wb");
+ if (!f || fwrite ("a\nA\nbc\nd\0f", 1, 10, f) != 10 || fclose (f) != 0)
+ {
+ fputs ("Failed to create sample file.\n", stderr);
+ remove ("test-getline.txt");
+ return 1;
+ }
+ f = fopen ("test-getline.txt", "rb");
+ if (!f)
+ {
+ fputs ("Failed to reopen sample file.\n", stderr);
+ remove ("test-getline.txt");
+ return 1;
+ }
+
+ /* Test initial allocation, which must include trailing NUL. */
+ line = NULL;
+ len = 0;
+ result = getline (&line, &len, f);
+ ASSERT (result == 2);
+ ASSERT (strcmp (line, "a\n") == 0);
+ ASSERT (2 < len);
+ free (line);
+
+ /* Test initial allocation again, with line = NULL and len != 0. */
+ line = NULL;
+ len = (size_t)(~0) / 4;
+ result = getline (&line, &len, f);
+ ASSERT (result == 2);
+ ASSERT (strcmp (line, "A\n") == 0);
+ ASSERT (2 < len);
+ free (line);
+
+ /* Test growth of buffer, must not leak. */
+ line = malloc (1);
+ len = 0;
+ result = getline (&line, &len, f);
+ ASSERT (result == 3);
+ ASSERT (strcmp (line, "bc\n") == 0);
+ ASSERT (3 < len);
+
+ /* Test embedded NULs and EOF behavior. */
+ result = getline (&line, &len, f);
+ ASSERT (result == 3);
+ ASSERT (memcmp (line, "d\0f", 4) == 0);
+ ASSERT (3 < len);
+
+ result = getline (&line, &len, f);
+ ASSERT (result == -1);
+
+ free (line);
+ fclose (f);
+ remove ("test-getline.txt");
+ return 0;
+}