"Fossies" - the Fresh Open Source Software Archive 
Member "portfwd-0.29/src/fd_set.cc" (30 May 2005, 878 Bytes) of package /linux/privat/old/portfwd-0.29.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
1 /*
2 $Id: fd_set.cc,v 1.1 2005/05/30 02:13:28 evertonm Exp $
3 */
4
5
6 #include <ulimit.h>
7 #include <sys/select.h>
8 #include <sys/types.h>
9 #include <unistd.h>
10 #include <sys/time.h>
11 #include <sys/resource.h>
12 #include <errno.h>
13
14 #include "fd_set.h"
15 #include "util.h"
16
17
18 void close_fds(int first_fds) {
19
20 struct rlimit rl;
21 if (getrlimit(RLIMIT_NOFILE, &rl) < 0)
22 rl.rlim_max = PORTFWD_MAX_FD;
23
24 unsigned int fd;
25 if (rl.rlim_max == RLIM_INFINITY)
26 rl.rlim_max = PORTFWD_MAX_FD;
27
28 for (fd = first_fds; fd < rl.rlim_max; ++fd)
29 close(fd);
30
31 errno = 0; /* close() on invalid fd */
32 }
33
34 void fdset(int fd, fd_set *fds, int *maxfd)
35 {
36 FD_SET(fd, fds);
37 *maxfd = MAX(*maxfd, fd + 1);
38 }
39
40 void fdclear(int fd, fd_set *fds, int *maxfd)
41 {
42 int i;
43
44 FD_CLR(fd, fds);
45
46 for (i = *maxfd - 1; i >= 0; --i)
47 if (FD_ISSET(i, fds))
48 break;
49
50 *maxfd = i + 1;
51 }
52
53
54 /* eof */