1# getline.m4 serial 27
2
3dnl Copyright (C) 1998-2003, 2005-2007, 2009-2016 Free Software Foundation,
4dnl Inc.
5dnl
6dnl This file is free software; the Free Software Foundation
7dnl gives unlimited permission to copy and/or distribute it,
8dnl with or without modifications, as long as this notice is preserved.
9
10AC_PREREQ([2.59])
11
12dnl See if there's a working, system-supplied version of the getline function.
13dnl We can't just do AC_REPLACE_FUNCS([getline]) because some systems
14dnl have a function by that name in -linet that doesn't have anything
15dnl to do with the function we need.
16AC_DEFUN([gl_FUNC_GETLINE],
17[
18  AC_REQUIRE([gl_STDIO_H_DEFAULTS])
19
20  dnl Persuade glibc <stdio.h> to declare getline().
21  AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
22
23  AC_CHECK_DECLS_ONCE([getline])
24
25  gl_getline_needs_run_time_check=no
26  AC_CHECK_FUNC([getline],
27                [dnl Found it in some library.  Verify that it works.
28                 gl_getline_needs_run_time_check=yes],
29                [am_cv_func_working_getline=no])
30  if test $gl_getline_needs_run_time_check = yes; then
31    AC_CACHE_CHECK([for working getline function], [am_cv_func_working_getline],
32    [echo fooNbarN | tr -d '\012' | tr N '\012' > conftest.data
33    AC_RUN_IFELSE([AC_LANG_SOURCE([[
34#    include <stdio.h>
35#    include <stdlib.h>
36#    include <string.h>
37    int main ()
38    {
39      FILE *in = fopen ("./conftest.data", "r");
40      if (!in)
41        return 1;
42      {
43        /* Test result for a NULL buffer and a zero size.
44           Based on a test program from Karl Heuer.  */
45        char *line = NULL;
46        size_t siz = 0;
47        int len = getline (&line, &siz, in);
48        if (!(len == 4 && line && strcmp (line, "foo\n") == 0))
49          return 2;
50        free (line);
51      }
52      {
53        /* Test result for a NULL buffer and a non-zero size.
54           This crashes on FreeBSD 8.0.  */
55        char *line = NULL;
56        size_t siz = (size_t)(~0) / 4;
57        if (getline (&line, &siz, in) == -1)
58          return 3;
59        free (line);
60      }
61      fclose (in);
62      return 0;
63    }
64    ]])], [am_cv_func_working_getline=yes] dnl The library version works.
65    , [am_cv_func_working_getline=no] dnl The library version does NOT work.
66    , dnl We're cross compiling. Assume it works on glibc2 systems.
67      [AC_EGREP_CPP([Lucky GNU user],
68         [
69#include <features.h>
70#ifdef __GNU_LIBRARY__
71 #if (__GLIBC__ >= 2) && !defined __UCLIBC__
72  Lucky GNU user
73 #endif
74#endif
75         ],
76         [am_cv_func_working_getline="guessing yes"],
77         [am_cv_func_working_getline="guessing no"])]
78    )])
79  fi
80
81  if test $ac_cv_have_decl_getline = no; then
82    HAVE_DECL_GETLINE=0
83  fi
84
85  case "$am_cv_func_working_getline" in
86    *no)
87      dnl Set REPLACE_GETLINE always: Even if we have not found the broken
88      dnl getline function among $LIBS, it may exist in libinet and the
89      dnl executable may be linked with -linet.
90      REPLACE_GETLINE=1
91      ;;
92  esac
93])
94
95# Prerequisites of lib/getline.c.
96AC_DEFUN([gl_PREREQ_GETLINE],
97[
98  :
99])
100