a40bb0de765e9da72a8fe07a2f4b6a4275287681
[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/lib/getdtablesize.c b/lib/getdtablesize.c
19 new file mode 100644
20 index 0000000..60e7f5a
21 --- /dev/null
22 +++ b/lib/getdtablesize.c
23 @@ -0,0 +1,63 @@
24 +/* getdtablesize() function for platforms that don't have it.
25 +   Copyright (C) 2008-2011 Free Software Foundation, Inc.
26 +   Written by Bruno Haible <bruno@clisp.org>, 2008.
27 +
28 +   This program is free software: you can redistribute it and/or modify
29 +   it under the terms of the GNU General Public License as published by
30 +   the Free Software Foundation; either version 3 of the License, or
31 +   (at your option) any later version.
32 +
33 +   This program is distributed in the hope that it will be useful,
34 +   but WITHOUT ANY WARRANTY; without even the implied warranty of
35 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36 +   GNU General Public License for more details.
37 +
38 +   You should have received a copy of the GNU General Public License
39 +   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
40 +
41 +#include <config.h>
42 +
43 +/* Specification.  */
44 +#include <unistd.h>
45 +
46 +#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
47 +
48 +#include <stdio.h>
49 +
50 +/* Cache for the previous getdtablesize () result.  */
51 +static int dtablesize;
52 +
53 +int
54 +getdtablesize (void)
55 +{
56 +  if (dtablesize == 0)
57 +    {
58 +      /* We are looking for the number N such that the valid file descriptors
59 +         are 0..N-1.  It can be obtained through a loop as follows:
60 +           {
61 +             int fd;
62 +             for (fd = 3; fd < 65536; fd++)
63 +               if (dup2 (0, fd) == -1)
64 +                 break;
65 +             return fd;
66 +           }
67 +         On Windows XP, the result is 2048.
68 +         The drawback of this loop is that it allocates memory for a libc
69 +         internal array that is never freed.
70 +
71 +         The number N can also be obtained as the upper bound for
72 +         _getmaxstdio ().  _getmaxstdio () returns the maximum number of open
73 +         FILE objects.  The sanity check in _setmaxstdio reveals the maximum
74 +         number of file descriptors.  This too allocates memory, but it is
75 +         freed when we call _setmaxstdio with the original value.  */
76 +      int orig_max_stdio = _getmaxstdio ();
77 +      unsigned int bound;
78 +      for (bound = 0x10000; _setmaxstdio (bound) < 0; bound = bound / 2)
79 +        ;
80 +      _setmaxstdio (orig_max_stdio);
81 +      dtablesize = bound;
82 +    }
83 +  return dtablesize;
84 +}
85 +
86 +#endif
87 diff --git a/m4/getdtablesize.m4 b/m4/getdtablesize.m4
88 new file mode 100644
89 index 0000000..40a598b
90 --- /dev/null
91 +++ b/m4/getdtablesize.m4
92 @@ -0,0 +1,14 @@
93 +# getdtablesize.m4 serial 2
94 +dnl Copyright (C) 2008-2011 Free Software Foundation, Inc.
95 +dnl This file is free software; the Free Software Foundation
96 +dnl gives unlimited permission to copy and/or distribute it,
97 +dnl with or without modifications, as long as this notice is preserved.
98 +
99 +AC_DEFUN([gl_FUNC_GETDTABLESIZE],
100 +[
101 +  AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
102 +  AC_CHECK_FUNCS_ONCE([getdtablesize])
103 +  if test $ac_cv_func_getdtablesize != yes; then
104 +    HAVE_GETDTABLESIZE=0
105 +  fi
106 +])