Import upstream version 1.28
[debian/tar] / gnu / getdtablesize.c
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-2014 Free Software Foundation, Inc.
5    Written by Bruno Haible <bruno@clisp.org>, 2008.
6
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.
11
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.
16
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/>.  */
19
20 #include <config.h>
21
22 /* Specification.  */
23 #include <unistd.h>
24
25 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
26
27 # include <stdio.h>
28
29 # include "msvc-inval.h"
30
31 # if HAVE_MSVC_INVALID_PARAMETER_HANDLER
32 static int
33 _setmaxstdio_nothrow (int newmax)
34 {
35   int result;
36
37   TRY_MSVC_INVAL
38     {
39       result = _setmaxstdio (newmax);
40     }
41   CATCH_MSVC_INVAL
42     {
43       result = -1;
44     }
45   DONE_MSVC_INVAL;
46
47   return result;
48 }
49 #  define _setmaxstdio _setmaxstdio_nothrow
50 # endif
51
52 /* Cache for the previous getdtablesize () result.  Safe to cache because
53    Windows also lacks setrlimit.  */
54 static int dtablesize;
55
56 int
57 getdtablesize (void)
58 {
59   if (dtablesize == 0)
60     {
61       /* We are looking for the number N such that the valid file descriptors
62          are 0..N-1.  It can be obtained through a loop as follows:
63            {
64              int fd;
65              for (fd = 3; fd < 65536; fd++)
66                if (dup2 (0, fd) == -1)
67                  break;
68              return fd;
69            }
70          On Windows XP, the result is 2048.
71          The drawback of this loop is that it allocates memory for a libc
72          internal array that is never freed.
73
74          The number N can also be obtained as the upper bound for
75          _getmaxstdio ().  _getmaxstdio () returns the maximum number of open
76          FILE objects.  The sanity check in _setmaxstdio reveals the maximum
77          number of file descriptors.  This too allocates memory, but it is
78          freed when we call _setmaxstdio with the original value.  */
79       int orig_max_stdio = _getmaxstdio ();
80       unsigned int bound;
81       for (bound = 0x10000; _setmaxstdio (bound) < 0; bound = bound / 2)
82         ;
83       _setmaxstdio (orig_max_stdio);
84       dtablesize = bound;
85     }
86   return dtablesize;
87 }
88
89 #elif HAVE_GETDTABLESIZE
90
91 # include <sys/resource.h>
92 # undef getdtablesize
93
94 int
95 rpl_getdtablesize(void)
96 {
97   /* To date, this replacement is only compiled for Cygwin 1.7.25,
98      which auto-increased the RLIMIT_NOFILE soft limit until it
99      hits the compile-time constant hard limit of 3200.  Although
100      that version of cygwin supported a child process inheriting
101      a smaller soft limit, the smaller limit is not enforced, so
102      we might as well just report the hard limit.  */
103   struct rlimit lim;
104   if (!getrlimit (RLIMIT_NOFILE, &lim) && lim.rlim_max != RLIM_INFINITY)
105     return lim.rlim_max;
106   return getdtablesize ();
107 }
108
109 #endif