annotate lib/getdtablesize.c @ 12421:e8d2c6fc33ad

Use spaces for indentation, not tabs.
author Bruno Haible <bruno@clisp.org>
date Thu, 10 Dec 2009 20:28:30 +0100
parents 729b89da65db
children c2cbabec01dd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10582
738187526552 New module 'getdtablesize'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* getdtablesize() function for platforms that don't have it.
11883
729b89da65db Fix getdtablesize() on mingw.
Bruno Haible <bruno@clisp.org>
parents: 10582
diff changeset
2 Copyright (C) 2008-2009 Free Software Foundation, Inc.
10582
738187526552 New module 'getdtablesize'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3 Written by Bruno Haible <bruno@clisp.org>, 2008.
738187526552 New module 'getdtablesize'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4
738187526552 New module 'getdtablesize'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 This program is free software: you can redistribute it and/or modify
738187526552 New module 'getdtablesize'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6 it under the terms of the GNU General Public License as published by
738187526552 New module 'getdtablesize'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
7 the Free Software Foundation; either version 3 of the License, or
738187526552 New module 'getdtablesize'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8 (at your option) any later version.
738187526552 New module 'getdtablesize'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9
738187526552 New module 'getdtablesize'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 This program is distributed in the hope that it will be useful,
738187526552 New module 'getdtablesize'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
738187526552 New module 'getdtablesize'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
738187526552 New module 'getdtablesize'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13 GNU General Public License for more details.
738187526552 New module 'getdtablesize'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14
738187526552 New module 'getdtablesize'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
15 You should have received a copy of the GNU General Public License
738187526552 New module 'getdtablesize'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
738187526552 New module 'getdtablesize'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17
738187526552 New module 'getdtablesize'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18 #include <config.h>
738187526552 New module 'getdtablesize'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19
738187526552 New module 'getdtablesize'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20 /* Specification. */
738187526552 New module 'getdtablesize'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21 #include <unistd.h>
738187526552 New module 'getdtablesize'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22
738187526552 New module 'getdtablesize'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
738187526552 New module 'getdtablesize'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24
738187526552 New module 'getdtablesize'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25 #include <stdio.h>
738187526552 New module 'getdtablesize'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26
11883
729b89da65db Fix getdtablesize() on mingw.
Bruno Haible <bruno@clisp.org>
parents: 10582
diff changeset
27 /* Cache for the previous getdtablesize () result. */
729b89da65db Fix getdtablesize() on mingw.
Bruno Haible <bruno@clisp.org>
parents: 10582
diff changeset
28 static int dtablesize;
729b89da65db Fix getdtablesize() on mingw.
Bruno Haible <bruno@clisp.org>
parents: 10582
diff changeset
29
10582
738187526552 New module 'getdtablesize'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30 int
738187526552 New module 'getdtablesize'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 getdtablesize (void)
738187526552 New module 'getdtablesize'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32 {
11883
729b89da65db Fix getdtablesize() on mingw.
Bruno Haible <bruno@clisp.org>
parents: 10582
diff changeset
33 if (dtablesize == 0)
729b89da65db Fix getdtablesize() on mingw.
Bruno Haible <bruno@clisp.org>
parents: 10582
diff changeset
34 {
729b89da65db Fix getdtablesize() on mingw.
Bruno Haible <bruno@clisp.org>
parents: 10582
diff changeset
35 /* We are looking for the number N such that the valid file descriptors
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11883
diff changeset
36 are 0..N-1. It can be obtained through a loop as follows:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11883
diff changeset
37 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11883
diff changeset
38 int fd;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11883
diff changeset
39 for (fd = 3; fd < 65536; fd++)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11883
diff changeset
40 if (dup2 (0, fd) == -1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11883
diff changeset
41 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11883
diff changeset
42 return fd;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11883
diff changeset
43 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11883
diff changeset
44 On Windows XP, the result is 2048.
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11883
diff changeset
45 The drawback of this loop is that it allocates memory for a libc
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11883
diff changeset
46 internal array that is never freed.
11883
729b89da65db Fix getdtablesize() on mingw.
Bruno Haible <bruno@clisp.org>
parents: 10582
diff changeset
47
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11883
diff changeset
48 The number N can also be obtained as the upper bound for
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11883
diff changeset
49 _getmaxstdio (). _getmaxstdio () returns the maximum number of open
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11883
diff changeset
50 FILE objects. The sanity check in _setmaxstdio reveals the maximum
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11883
diff changeset
51 number of file descriptors. This too allocates memory, but it is
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11883
diff changeset
52 freed when we call _setmaxstdio with the original value. */
11883
729b89da65db Fix getdtablesize() on mingw.
Bruno Haible <bruno@clisp.org>
parents: 10582
diff changeset
53 int orig_max_stdio = _getmaxstdio ();
729b89da65db Fix getdtablesize() on mingw.
Bruno Haible <bruno@clisp.org>
parents: 10582
diff changeset
54 unsigned int bound;
729b89da65db Fix getdtablesize() on mingw.
Bruno Haible <bruno@clisp.org>
parents: 10582
diff changeset
55 for (bound = 0x10000; _setmaxstdio (bound) < 0; bound = bound / 2)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11883
diff changeset
56 ;
11883
729b89da65db Fix getdtablesize() on mingw.
Bruno Haible <bruno@clisp.org>
parents: 10582
diff changeset
57 _setmaxstdio (orig_max_stdio);
729b89da65db Fix getdtablesize() on mingw.
Bruno Haible <bruno@clisp.org>
parents: 10582
diff changeset
58 dtablesize = bound;
729b89da65db Fix getdtablesize() on mingw.
Bruno Haible <bruno@clisp.org>
parents: 10582
diff changeset
59 }
729b89da65db Fix getdtablesize() on mingw.
Bruno Haible <bruno@clisp.org>
parents: 10582
diff changeset
60 return dtablesize;
10582
738187526552 New module 'getdtablesize'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61 }
738187526552 New module 'getdtablesize'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62
738187526552 New module 'getdtablesize'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63 #endif