| 1 |
# getline.m4 |
| 2 |
# serial 35 |
| 3 |
|
| 4 |
dnl Copyright (C) 1998-2003, 2005-2007, 2009-2026 Free Software Foundation, |
| 5 |
dnl Inc. |
| 6 |
dnl |
| 7 |
dnl This file is free software; the Free Software Foundation |
| 8 |
dnl gives unlimited permission to copy and/or distribute it, |
| 9 |
dnl with or without modifications, as long as this notice is preserved. |
| 10 |
dnl This file is offered as-is, without any warranty. |
| 11 |
|
| 12 |
AC_PREREQ([2.59]) |
| 13 |
|
| 14 |
dnl See if there's a working, system-supplied version of the getline function. |
| 15 |
dnl We can't just do AC_REPLACE_FUNCS([getline]) because some systems |
| 16 |
dnl have a function by that name in -linet that doesn't have anything |
| 17 |
dnl to do with the function we need. |
| 18 |
AC_DEFUN([gl_FUNC_GETLINE], |
| 19 |
[ |
| 20 |
AC_REQUIRE([gl_STDIO_H_DEFAULTS]) |
| 21 |
AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles |
| 22 |
|
| 23 |
dnl Persuade glibc <stdio.h> to declare getline(). |
| 24 |
AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS]) |
| 25 |
|
| 26 |
AC_CHECK_DECLS_ONCE([getline]) |
| 27 |
|
| 28 |
gl_CHECK_FUNCS_ANDROID([getline], [[#include <stdio.h>]]) |
| 29 |
if test $ac_cv_func_getline = yes; then |
| 30 |
dnl Found it in some library. Verify that it works. |
| 31 |
AC_CACHE_CHECK([for working getline function], |
| 32 |
[am_cv_func_working_getline], |
| 33 |
[echo fooNbarN | tr -d '\012' | tr N '\012' > conftest.data |
| 34 |
touch conftest.empty |
| 35 |
AC_RUN_IFELSE([AC_LANG_SOURCE([[ |
| 36 |
# include <stdio.h> |
| 37 |
# include <stdlib.h> |
| 38 |
# include <string.h> |
| 39 |
int main () |
| 40 |
{ |
| 41 |
FILE *in = fopen ("./conftest.data", "r"); |
| 42 |
int result = 0; |
| 43 |
if (!in) |
| 44 |
return 1; |
| 45 |
{ |
| 46 |
/* Test result for a NULL buffer and a zero size. |
| 47 |
Based on a test program from Karl Heuer. */ |
| 48 |
char *line = NULL; |
| 49 |
size_t siz = 0; |
| 50 |
int len = getline (&line, &siz, in); |
| 51 |
if (!(len == 4 && line && strcmp (line, "foo\n") == 0)) |
| 52 |
result |= 2; |
| 53 |
free (line); |
| 54 |
} |
| 55 |
{ |
| 56 |
/* Test result for a NULL buffer and a non-zero size. |
| 57 |
This crashes on FreeBSD 8.0. */ |
| 58 |
char *line = NULL; |
| 59 |
size_t siz = (size_t)(~0) / 4; |
| 60 |
if (getline (&line, &siz, in) == -1) |
| 61 |
result |= 4; |
| 62 |
free (line); |
| 63 |
} |
| 64 |
fclose (in); |
| 65 |
{ |
| 66 |
/* Test that reading EOF as the first character sets the first byte |
| 67 |
in the buffer to NUL. This fails on glibc 2.42 and earlier. */ |
| 68 |
in = fopen ("./conftest.empty", "r"); |
| 69 |
if (!in) |
| 70 |
return 1; |
| 71 |
char *line = malloc (1); |
| 72 |
line[0] = 'A'; |
| 73 |
size_t siz = 1; |
| 74 |
if (getline (&line, &siz, in) != -1 || line[0] != '\0') |
| 75 |
result |= 8; |
| 76 |
free (line); |
| 77 |
} |
| 78 |
fclose (in); |
| 79 |
return result; |
| 80 |
} |
| 81 |
]])], |
| 82 |
[am_cv_func_working_getline=yes], |
| 83 |
[am_cv_func_working_getline=no], |
| 84 |
[case "$host_os" in |
| 85 |
# Guess yes on musl. |
| 86 |
*-musl* | midipix*) am_cv_func_working_getline="guessing yes" ;; |
| 87 |
# Guess no on glibc. |
| 88 |
*-gnu* | gnu*) am_cv_func_working_getline="guessing no" ;; |
| 89 |
*) am_cv_func_working_getline="$gl_cross_guess_normal" ;; |
| 90 |
esac |
| 91 |
]) |
| 92 |
rm -f conftest.data conftest.empty |
| 93 |
]) |
| 94 |
else |
| 95 |
am_cv_func_working_getline=no |
| 96 |
case "$gl_cv_onwards_func_getline" in |
| 97 |
future*) REPLACE_GETLINE=1 ;; |
| 98 |
esac |
| 99 |
fi |
| 100 |
|
| 101 |
if test $ac_cv_have_decl_getline = no; then |
| 102 |
HAVE_DECL_GETLINE=0 |
| 103 |
fi |
| 104 |
|
| 105 |
case "$am_cv_func_working_getline" in |
| 106 |
*yes) ;; |
| 107 |
*) |
| 108 |
dnl Set REPLACE_GETLINE always: Even if we have not found the broken |
| 109 |
dnl getline function among $LIBS, it may exist in libinet and the |
| 110 |
dnl executable may be linked with -linet. |
| 111 |
REPLACE_GETLINE=1 |
| 112 |
;; |
| 113 |
esac |
| 114 |
]) |
| 115 |
|
| 116 |
# Prerequisites of lib/getline.c. |
| 117 |
AC_DEFUN([gl_PREREQ_GETLINE], |
| 118 |
[ |
| 119 |
: |
| 120 |
]) |