4 dnl Copyright (C) 2005-2007, 2009-2024 Free Software Foundation, Inc.
6 dnl This file is free software; the Free Software Foundation
7 dnl gives unlimited permission to copy and/or distribute it,
8 dnl with or without modifications, as long as this notice is preserved.
12 AC_DEFUN([gl_FUNC_GETDELIM],
14 AC_REQUIRE([gl_STDIO_H_DEFAULTS])
15 AC_REQUIRE([AC_CANONICAL_HOST])
17 dnl Persuade glibc <stdio.h> to declare getdelim().
18 AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
20 AC_CHECK_DECLS_ONCE([getdelim])
22 gl_CHECK_FUNCS_ANDROID([getdelim], [[#include <stdio.h>]])
23 if test $ac_cv_func_getdelim = yes; then
25 dnl Found it in some library. Verify that it works.
26 AC_CACHE_CHECK([for working getdelim function],
27 [gl_cv_func_working_getdelim],
30 dnl On macOS 10.13, valgrind detected an out-of-bounds read during
31 dnl the GNU sed test suite:
32 dnl Invalid read of size 16
33 dnl at 0x100EE6A05: _platform_memchr$VARIANT$Base (in /usr/lib/system/libsystem_platform.dylib)
34 dnl by 0x100B7B0BD: getdelim (in /usr/lib/system/libsystem_c.dylib)
35 dnl by 0x10000B0BE: ck_getdelim (utils.c:254)
36 gl_cv_func_working_getdelim=no ;;
38 echo fooNbarN | tr -d '\012' | tr N '\012' > conftest.data
39 AC_RUN_IFELSE([AC_LANG_SOURCE([[
45 FILE *in = fopen ("./conftest.data", "r");
49 /* Test result for a NULL buffer and a zero size.
50 Based on a test program from Karl Heuer. */
53 int len = getdelim (&line, &siz, '\n', in);
54 if (!(len == 4 && line && strcmp (line, "foo\n") == 0))
55 { free (line); fclose (in); return 2; }
59 /* Test result for a NULL buffer and a non-zero size.
60 This crashes on FreeBSD 8.0. */
62 size_t siz = (size_t)(~0) / 4;
63 if (getdelim (&line, &siz, '\n', in) == -1)
64 { fclose (in); return 3; }
71 [gl_cv_func_working_getdelim=yes],
72 [gl_cv_func_working_getdelim=no],
73 [dnl We're cross compiling.
74 dnl Guess it works on glibc2 systems and musl systems.
75 AC_EGREP_CPP([Lucky GNU user],
78 #ifdef __GNU_LIBRARY__
79 #if (__GLIBC__ >= 2) && !defined __UCLIBC__
84 [gl_cv_func_working_getdelim="guessing yes"],
86 *-musl* | midipix*) gl_cv_func_working_getdelim="guessing yes" ;;
87 *) gl_cv_func_working_getdelim="$gl_cross_guess_normal" ;;
94 case "$gl_cv_func_working_getdelim" in
96 *) REPLACE_GETDELIM=1 ;;
100 case "$gl_cv_onwards_func_getdelim" in
101 future*) REPLACE_GETDELIM=1 ;;
105 if test $ac_cv_have_decl_getdelim = no; then
110 # Prerequisites of lib/getdelim.c.
111 AC_DEFUN([gl_PREREQ_GETDELIM],
113 AC_CHECK_FUNCS([flockfile funlockfile])
114 AC_CHECK_DECLS([getc_unlocked])