Fix COPYING.EXCEPTION license notices
[gnulib.git] / m4 / getdelim.m4
blob61139039554a7d833fd639e8c7aaa7668776d1d4
1 # getdelim.m4
2 # serial 19
4 dnl Copyright (C) 2005-2007, 2009-2024 Free Software Foundation, Inc.
5 dnl
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.
10 AC_PREREQ([2.59])
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
24     HAVE_GETDELIM=1
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],
28       [case "$host_os" in
29          darwin*)
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 ;;
37          *)
38            echo fooNbarN | tr -d '\012' | tr N '\012' > conftest.data
39            AC_RUN_IFELSE([AC_LANG_SOURCE([[
40 #    include <stdio.h>
41 #    include <stdlib.h>
42 #    include <string.h>
43     int main ()
44     {
45       FILE *in = fopen ("./conftest.data", "r");
46       if (!in)
47         return 1;
48       {
49         /* Test result for a NULL buffer and a zero size.
50            Based on a test program from Karl Heuer.  */
51         char *line = NULL;
52         size_t siz = 0;
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; }
56         free (line);
57       }
58       {
59         /* Test result for a NULL buffer and a non-zero size.
60            This crashes on FreeBSD 8.0.  */
61         char *line = NULL;
62         size_t siz = (size_t)(~0) / 4;
63         if (getdelim (&line, &siz, '\n', in) == -1)
64           { fclose (in); return 3; }
65         free (line);
66       }
67       fclose (in);
68       return 0;
69     }
70     ]])],
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],
76                 [
77 #include <features.h>
78 #ifdef __GNU_LIBRARY__
79  #if (__GLIBC__ >= 2) && !defined __UCLIBC__
80   Lucky GNU user
81  #endif
82 #endif
83                 ],
84                 [gl_cv_func_working_getdelim="guessing yes"],
85                 [case "$host_os" in
86                    *-musl* | midipix*) gl_cv_func_working_getdelim="guessing yes" ;;
87                    *)                  gl_cv_func_working_getdelim="$gl_cross_guess_normal" ;;
88                  esac
89                 ])
90              ])
91            ;;
92        esac
93       ])
94     case "$gl_cv_func_working_getdelim" in
95       *yes) ;;
96       *) REPLACE_GETDELIM=1 ;;
97     esac
98   else
99     HAVE_GETDELIM=0
100     case "$gl_cv_onwards_func_getdelim" in
101       future*) REPLACE_GETDELIM=1 ;;
102     esac
103   fi
105   if test $ac_cv_have_decl_getdelim = no; then
106     HAVE_DECL_GETDELIM=0
107   fi
110 # Prerequisites of lib/getdelim.c.
111 AC_DEFUN([gl_PREREQ_GETDELIM],
113   AC_CHECK_FUNCS([flockfile funlockfile])
114   AC_CHECK_DECLS([getc_unlocked])