Is there a portable way (POSIX) to get the highest allocated file descriptor number for the current process?
I know that there\'s a nice way to get the number on AIX
The POSIX way is:
int maxfd=sysconf(_SC_OPEN_MAX);
for(int fd=3; fd
(note that's closing from 3 up, to keep stdin/stdout/stderr open)
close() harmlessly returns EBADF if the file descriptor is not open. There's no need to waste another system call checking.
Some Unixes support a closefrom(). This avoids the excessive number of calls to close() depending on the maximum possible file descriptor number. While the best solution I'm aware of, it's completely nonportable.