Debian Codegraph @

packages/seacas/libraries/suplib_c/open_file_limit.c

raw
1 // Copyright(C) 1999-2021, 2023 National Technology & Engineering Solutions
2 // of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
3 // NTESS, the U.S. Government retains certain rights in this software.
4 //
5 // See packages/seacas/LICENSE for details
6
7 #if defined(WIN32) || defined(__WIN32__) || defined(_WIN32) || defined(_MSC_VER) || \
8 defined(__MINGW32__) || defined(_WIN64) || defined(__MINGW64__)
9 #include <stdio.h>
10 #else
11 #include <limits.h>
12 #include <unistd.h>
13 #endif
14
15 int open_file_limit(void)
16 {
17 // Returns maximum number of files that one process can have open
18 // at one time. (POSIX)
19 #if defined(WIN32) || defined(__WIN32__) || defined(_WIN32) || defined(_MSC_VER) || \
20 defined(__MINGW32__) || defined(_WIN64) || defined(__MINGW64__)
21 int fdmax = _getmaxstdio();
22 #else
23 int fdmax = sysconf(_SC_OPEN_MAX);
24 if (fdmax == -1) {
25 // POSIX indication that there is no limit on open files...
26 fdmax = INT_MAX;
27 }
28 #endif
29
30 // File descriptors are assigned in order (0,1,2,3,...) on a per-process
31 // basis.
32
33 // Assume that we have stdin, stdout, stderr.
34 // (3 total).
35
36 return fdmax - 3;
37
38 // Could iterate from 0..fdmax and check for the first EBADF (bad
39 // file descriptor) error return from fcntl, but that takes too long
40 // and may cause other problems.
41 //
42 // Another possibility is to do an open and see which descriptor is
43 // returned -- take that as 1 more than the current count of open
44 // files instead of assuming 3.
45 //
46 }