diff options
author | Thomas Guyot-Sionnest <dermoth@aei.ca> | 2010-04-08 01:11:46 (GMT) |
---|---|---|
committer | Thomas Guyot-Sionnest <dermoth@aei.ca> | 2010-04-13 01:26:35 (GMT) |
commit | 74da141e618ef99959d509cb2e7be35a348a39db (patch) | |
tree | 88ebc38b381a1021fc2d74864a71e230ae591c3d /gl/getdtablesize.c | |
parent | c63a4f726a0b6ad8cf6040f947754a81fd4683bb (diff) | |
download | monitoring-plugins-74da141e618ef99959d509cb2e7be35a348a39db.tar.gz |
Sync with the latest Gnulib code (177f525)
Signed-off-by: Thomas Guyot-Sionnest <dermoth@aei.ca>
Diffstat (limited to 'gl/getdtablesize.c')
-rw-r--r-- | gl/getdtablesize.c | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/gl/getdtablesize.c b/gl/getdtablesize.c new file mode 100644 index 0000000..a565a2d --- /dev/null +++ b/gl/getdtablesize.c | |||
@@ -0,0 +1,63 @@ | |||
1 | /* getdtablesize() function for platforms that don't have it. | ||
2 | Copyright (C) 2008-2010 Free Software Foundation, Inc. | ||
3 | Written by Bruno Haible <bruno@clisp.org>, 2008. | ||
4 | |||
5 | This program is free software: you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published by | ||
7 | the Free Software Foundation; either version 3 of the License, or | ||
8 | (at your option) any later version. | ||
9 | |||
10 | This program is distributed in the hope that it will be useful, | ||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | GNU General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | ||
17 | |||
18 | #include <config.h> | ||
19 | |||
20 | /* Specification. */ | ||
21 | #include <unistd.h> | ||
22 | |||
23 | #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ | ||
24 | |||
25 | #include <stdio.h> | ||
26 | |||
27 | /* Cache for the previous getdtablesize () result. */ | ||
28 | static int dtablesize; | ||
29 | |||
30 | int | ||
31 | getdtablesize (void) | ||
32 | { | ||
33 | if (dtablesize == 0) | ||
34 | { | ||
35 | /* We are looking for the number N such that the valid file descriptors | ||
36 | are 0..N-1. It can be obtained through a loop as follows: | ||
37 | { | ||
38 | int fd; | ||
39 | for (fd = 3; fd < 65536; fd++) | ||
40 | if (dup2 (0, fd) == -1) | ||
41 | break; | ||
42 | return fd; | ||
43 | } | ||
44 | On Windows XP, the result is 2048. | ||
45 | The drawback of this loop is that it allocates memory for a libc | ||
46 | internal array that is never freed. | ||
47 | |||
48 | The number N can also be obtained as the upper bound for | ||
49 | _getmaxstdio (). _getmaxstdio () returns the maximum number of open | ||
50 | FILE objects. The sanity check in _setmaxstdio reveals the maximum | ||
51 | number of file descriptors. This too allocates memory, but it is | ||
52 | freed when we call _setmaxstdio with the original value. */ | ||
53 | int orig_max_stdio = _getmaxstdio (); | ||
54 | unsigned int bound; | ||
55 | for (bound = 0x10000; _setmaxstdio (bound) < 0; bound = bound / 2) | ||
56 | ; | ||
57 | _setmaxstdio (orig_max_stdio); | ||
58 | dtablesize = bound; | ||
59 | } | ||
60 | return dtablesize; | ||
61 | } | ||
62 | |||
63 | #endif | ||