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