1# getdelim.m4 serial 15
2
3dnl Copyright (C) 2005-2007, 2009-2020 Free Software Foundation, Inc.
4dnl
5dnl This file is free software; the Free Software Foundation
6dnl gives unlimited permission to copy and/or distribute it,
7dnl with or without modifications, as long as this notice is preserved.
8
9AC_PREREQ([2.59])
10
11AC_DEFUN([gl_FUNC_GETDELIM],
12[
13  AC_REQUIRE([gl_STDIO_H_DEFAULTS])
14  AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
15
16  dnl Persuade glibc <stdio.h> to declare getdelim().
17  AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
18
19  AC_CHECK_DECLS_ONCE([getdelim])
20
21  AC_CHECK_FUNCS_ONCE([getdelim])
22  if test $ac_cv_func_getdelim = yes; then
23    HAVE_GETDELIM=1
24    dnl Found it in some library.  Verify that it works.
25    AC_CACHE_CHECK([for working getdelim function],
26      [gl_cv_func_working_getdelim],
27      [echo fooNbarN | tr -d '\012' | tr N '\012' > conftest.data
28       AC_RUN_IFELSE([AC_LANG_SOURCE([[
29#    include <stdio.h>
30#    include <stdlib.h>
31#    include <string.h>
32    int main ()
33    {
34      FILE *in = fopen ("./conftest.data", "r");
35      if (!in)
36        return 1;
37      {
38        /* Test result for a NULL buffer and a zero size.
39           Based on a test program from Karl Heuer.  */
40        char *line = NULL;
41        size_t siz = 0;
42        int len = getdelim (&line, &siz, '\n', in);
43        if (!(len == 4 && line && strcmp (line, "foo\n") == 0))
44          { free (line); fclose (in); return 2; }
45        free (line);
46      }
47      {
48        /* Test result for a NULL buffer and a non-zero size.
49           This crashes on FreeBSD 8.0.  */
50        char *line = NULL;
51        size_t siz = (size_t)(~0) / 4;
52        if (getdelim (&line, &siz, '\n', in) == -1)
53          { fclose (in); return 3; }
54        free (line);
55      }
56      fclose (in);
57      return 0;
58    }
59    ]])],
60         [gl_cv_func_working_getdelim=yes],
61         [gl_cv_func_working_getdelim=no],
62         [dnl We're cross compiling.
63          dnl Guess it works on glibc2 systems and musl systems.
64          AC_EGREP_CPP([Lucky GNU user],
65            [
66#include <features.h>
67#ifdef __GNU_LIBRARY__
68 #if (__GLIBC__ >= 2) && !defined __UCLIBC__
69  Lucky GNU user
70 #endif
71#endif
72            ],
73            [gl_cv_func_working_getdelim="guessing yes"],
74            [case "$host_os" in
75               *-musl*) gl_cv_func_working_getdelim="guessing yes" ;;
76               *)       gl_cv_func_working_getdelim="$gl_cross_guess_normal" ;;
77             esac
78            ])
79         ])
80      ])
81    case "$gl_cv_func_working_getdelim" in
82      *yes) ;;
83      *) REPLACE_GETDELIM=1 ;;
84    esac
85  else
86    HAVE_GETDELIM=0
87  fi
88
89  if test $ac_cv_have_decl_getdelim = no; then
90    HAVE_DECL_GETDELIM=0
91  fi
92])
93
94# Prerequisites of lib/getdelim.c.
95AC_DEFUN([gl_PREREQ_GETDELIM],
96[
97  AC_CHECK_FUNCS([flockfile funlockfile])
98  AC_CHECK_DECLS([getc_unlocked])
99])
100