This source file includes following definitions.
- _setmaxstdio_nothrow
- getdtablesize
- getdtablesize
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 #include <config.h>
19
20
21 #include <unistd.h>
22
23 #if defined _WIN32 && ! defined __CYGWIN__
24
25 # include <stdio.h>
26
27 # if HAVE_MSVC_INVALID_PARAMETER_HANDLER
28 # include "msvc-inval.h"
29 # endif
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 # else
50 # define _setmaxstdio_nothrow _setmaxstdio
51 # endif
52
53
54
55 static int dtablesize;
56
57 int
58 getdtablesize (void)
59 {
60 if (dtablesize == 0)
61 {
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80 int orig_max_stdio = _getmaxstdio ();
81 unsigned int bound;
82 for (bound = 0x10000; _setmaxstdio_nothrow (bound) < 0; bound = bound / 2)
83 ;
84 _setmaxstdio_nothrow (orig_max_stdio);
85 dtablesize = bound;
86 }
87 return dtablesize;
88 }
89
90 #else
91
92 # include <limits.h>
93 # include <sys/resource.h>
94
95 # ifndef RLIM_SAVED_CUR
96 # define RLIM_SAVED_CUR RLIM_INFINITY
97 # endif
98 # ifndef RLIM_SAVED_MAX
99 # define RLIM_SAVED_MAX RLIM_INFINITY
100 # endif
101
102 # ifdef __CYGWIN__
103
104
105
106 # define rlim_cur rlim_max
107 # endif
108
109 int
110 getdtablesize (void)
111 {
112 struct rlimit lim;
113
114 if (getrlimit (RLIMIT_NOFILE, &lim) == 0
115 && 0 <= lim.rlim_cur && lim.rlim_cur <= INT_MAX
116 && lim.rlim_cur != RLIM_INFINITY
117 && lim.rlim_cur != RLIM_SAVED_CUR
118 && lim.rlim_cur != RLIM_SAVED_MAX)
119 return lim.rlim_cur;
120
121 return INT_MAX;
122 }
123
124 #endif