better patch for missing gnulib getdtablesize function in 1.4 upstream
[debian/gzip] / debian / patches / getdtablesize-missing.diff
1 diff --git a/configure.ac b/configure.ac
2 index 818dbd9..0919132 100644
3 --- a/configure.ac
4 +++ b/configure.ac
5 @@ -129,6 +129,12 @@ if test "$gl_gcc_warnings" = yes; then
6    AC_SUBST([GNULIB_WARN_CFLAGS])
7  fi
8  
9 +gl_FUNC_GETDTABLESIZE
10 +if test $HAVE_GETDTABLESIZE = 0; then
11 +  AC_LIBOBJ([getdtablesize])
12 +fi
13 +gl_UNISTD_MODULE_INDICATOR([getdtablesize])
14 +
15  # cc -E produces incorrect asm files on SVR4, so postprocess it.
16  ASCPPPOST="sed '/^ *#/d; s,//.*,,; s/% /%/g; s/\\. /./g'"
17  AC_SUBST([ASCPPPOST])
18 diff --git a/debian/patches/series b/debian/patches/series
19 index f008915..6015402 100644
20 --- a/debian/patches/series
21 +++ b/debian/patches/series
22 @@ -1,2 +1 @@
23 -getdtablesize-missing.diff
24  zless-LESSOPEN.diff
25 diff --git a/lib/getdtablesize.c b/lib/getdtablesize.c
26 new file mode 100644
27 index 0000000..60e7f5a
28 --- /dev/null
29 +++ b/lib/getdtablesize.c
30 @@ -0,0 +1,63 @@
31 +/* getdtablesize() function for platforms that don't have it.
32 +   Copyright (C) 2008-2011 Free Software Foundation, Inc.
33 +   Written by Bruno Haible <bruno@clisp.org>, 2008.
34 +
35 +   This program is free software: you can redistribute it and/or modify
36 +   it under the terms of the GNU General Public License as published by
37 +   the Free Software Foundation; either version 3 of the License, or
38 +   (at your option) any later version.
39 +
40 +   This program is distributed in the hope that it will be useful,
41 +   but WITHOUT ANY WARRANTY; without even the implied warranty of
42 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
43 +   GNU General Public License for more details.
44 +
45 +   You should have received a copy of the GNU General Public License
46 +   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
47 +
48 +#include <config.h>
49 +
50 +/* Specification.  */
51 +#include <unistd.h>
52 +
53 +#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
54 +
55 +#include <stdio.h>
56 +
57 +/* Cache for the previous getdtablesize () result.  */
58 +static int dtablesize;
59 +
60 +int
61 +getdtablesize (void)
62 +{
63 +  if (dtablesize == 0)
64 +    {
65 +      /* We are looking for the number N such that the valid file descriptors
66 +         are 0..N-1.  It can be obtained through a loop as follows:
67 +           {
68 +             int fd;
69 +             for (fd = 3; fd < 65536; fd++)
70 +               if (dup2 (0, fd) == -1)
71 +                 break;
72 +             return fd;
73 +           }
74 +         On Windows XP, the result is 2048.
75 +         The drawback of this loop is that it allocates memory for a libc
76 +         internal array that is never freed.
77 +
78 +         The number N can also be obtained as the upper bound for
79 +         _getmaxstdio ().  _getmaxstdio () returns the maximum number of open
80 +         FILE objects.  The sanity check in _setmaxstdio reveals the maximum
81 +         number of file descriptors.  This too allocates memory, but it is
82 +         freed when we call _setmaxstdio with the original value.  */
83 +      int orig_max_stdio = _getmaxstdio ();
84 +      unsigned int bound;
85 +      for (bound = 0x10000; _setmaxstdio (bound) < 0; bound = bound / 2)
86 +        ;
87 +      _setmaxstdio (orig_max_stdio);
88 +      dtablesize = bound;
89 +    }
90 +  return dtablesize;
91 +}
92 +
93 +#endif
94 diff --git a/m4/.gitignore b/m4/.gitignore
95 index a5c3278..e03f2bc 100644
96 --- a/m4/.gitignore
97 +++ b/m4/.gitignore
98 @@ -1,7 +1,6 @@
99  /asm-underscore.m4
100  /cloexec.m4
101  /configmake.m4
102 -/getdtablesize.m4
103  /gnulib-comp.m4
104  /gnulib-tool.m4
105  /wchar_h.m4
106 diff --git a/m4/getdtablesize.m4 b/m4/getdtablesize.m4
107 new file mode 100644
108 index 0000000..40a598b
109 --- /dev/null
110 +++ b/m4/getdtablesize.m4
111 @@ -0,0 +1,14 @@
112 +# getdtablesize.m4 serial 2
113 +dnl Copyright (C) 2008-2011 Free Software Foundation, Inc.
114 +dnl This file is free software; the Free Software Foundation
115 +dnl gives unlimited permission to copy and/or distribute it,
116 +dnl with or without modifications, as long as this notice is preserved.
117 +
118 +AC_DEFUN([gl_FUNC_GETDTABLESIZE],
119 +[
120 +  AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
121 +  AC_CHECK_FUNCS_ONCE([getdtablesize])
122 +  if test $ac_cv_func_getdtablesize != yes; then
123 +    HAVE_GETDTABLESIZE=0
124 +  fi
125 +])