xref: /netbsd-src/external/bsd/cron/dist/closeall.c (revision 065057e63525be61eed7190f73cb58a269959e81)
1 #include <unistd.h>
2 #include <errno.h>
3 #include <fcntl.h>
4 
5 #ifdef __linux__
6 #include <linux/limits.h>
7 #endif
8 
9 #include "cron.h"
10 
close_all(int start)11 int close_all(int start)
12 {
13 #ifdef F_CLOSEM
14 	return fcntl(start, F_CLOSEM);
15 #else
16 	int fd, max;
17 
18 	max = sysconf(_SC_OPEN_MAX);
19 	if (max <= 0)
20 		return -1;
21 
22 #ifdef __linux__
23 	if (max < NR_OPEN)
24 		max = NR_OPEN;
25 #endif
26 
27 	for (fd = start; fd < max; fd++) {
28 		if (close(fd) && errno != EBADF)
29 			return -1;
30 	}
31 
32 	return 0;
33 #endif
34 }
35