add ext4,vfat and tar.bz2
[u-tools.git] / u-tools / apps / tar / gnu / getdtablesize.c
blob3258ae10a2f09fdadd62ed596dd609f48272940c
1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* getdtablesize() function for platforms that don't have it.
4 Copyright (C) 2008-2011 Free Software Foundation, Inc.
5 Written by Bruno Haible <bruno@clisp.org>, 2008.
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include <config.h>
22 /* Specification. */
23 #include <unistd.h>
25 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
27 #include <stdio.h>
29 /* Cache for the previous getdtablesize () result. */
30 static int dtablesize;
32 int
33 getdtablesize (void)
35 if (dtablesize == 0)
37 /* We are looking for the number N such that the valid file descriptors
38 are 0..N-1. It can be obtained through a loop as follows:
40 int fd;
41 for (fd = 3; fd < 65536; fd++)
42 if (dup2 (0, fd) == -1)
43 break;
44 return fd;
46 On Windows XP, the result is 2048.
47 The drawback of this loop is that it allocates memory for a libc
48 internal array that is never freed.
50 The number N can also be obtained as the upper bound for
51 _getmaxstdio (). _getmaxstdio () returns the maximum number of open
52 FILE objects. The sanity check in _setmaxstdio reveals the maximum
53 number of file descriptors. This too allocates memory, but it is
54 freed when we call _setmaxstdio with the original value. */
55 int orig_max_stdio = _getmaxstdio ();
56 unsigned int bound;
57 for (bound = 0x10000; _setmaxstdio (bound) < 0; bound = bound / 2)
59 _setmaxstdio (orig_max_stdio);
60 dtablesize = bound;
62 return dtablesize;
65 #endif