diff options
| author | Michael Kerrisk <mtk.manpages@gmail.com> | 2008-12-05 15:26:20 -0500 |
|---|---|---|
| committer | Michael Kerrisk <mtk.manpages@gmail.com> | 2008-12-05 22:47:24 -0500 |
| commit | 21c39e5919736ccde78b95dc4dba66142f5221d7 (patch) | |
| tree | 1da29d8c6fcf575847a139b55ec6c1bfeae1f055 /man2/select_tut.2 | |
| parent | 54781a2395c65d9718987316bfe9190f2e59c934 (diff) | |
| download | man-pages-21c39e5919736ccde78b95dc4dba66142f5221d7.tar.gz | |
select_tut.2: Fix SHUT_FD* macros in example program
Add "do {} while (0)"
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
Diffstat (limited to 'man2/select_tut.2')
| -rw-r--r-- | man2/select_tut.2 | 66 |
1 files changed, 32 insertions, 34 deletions
diff --git a/man2/select_tut.2 b/man2/select_tut.2 index 92e9779d50..61e70c8f09 100644 --- a/man2/select_tut.2 +++ b/man2/select_tut.2 @@ -26,7 +26,7 @@ .\" 2006-05-13, mtk, removed much material that is redundant with select.2 .\" various other changes .\" -.TH SELECT_TUT 2 2007-12-18 "Linux" "Linux Programmer's Manual" +.TH SELECT_TUT 2 2008-12-05 "Linux" "Linux Programmer's Manual" .SH NAME select, pselect, FD_CLR, FD_ISSET, FD_SET, FD_ZERO \- synchronous I/O multiplexing @@ -639,21 +639,21 @@ connect_socket(int connect_port, char *address) return s; } -#define SHUT_FD1 { \\ - if (fd1 >= 0) { \\ - shutdown(fd1, SHUT_RDWR); \\ - close(fd1); \\ - fd1 = \-1; \\ - } \\ - } - -#define SHUT_FD2 { \\ - if (fd2 >= 0) { \\ - shutdown(fd2, SHUT_RDWR); \\ - close(fd2); \\ - fd2 = \-1; \\ - } \\ - } +#define SHUT_FD1 do { \\ + if (fd1 >= 0) { \\ + shutdown(fd1, SHUT_RDWR); \\ + close(fd1); \\ + fd1 = \-1; \\ + } \\ + } while (0) + +#define SHUT_FD2 do { \\ + if (fd2 >= 0) { \\ + shutdown(fd2, SHUT_RDWR); \\ + close(fd2); \\ + fd2 = \-1; \\ + } \\ + } while (0) #define BUF_SIZE 1024 @@ -738,9 +738,9 @@ main(int argc, char **argv) buf2_avail = buf2_written = 0; fd1 = r; fd2 = connect_socket(forward_port, argv[3]); - if (fd2 < 0) { + if (fd2 < 0) SHUT_FD1; - } else + else printf("connect from %s\\n", inet_ntoa(client_address.sin_addr)); } @@ -753,9 +753,9 @@ main(int argc, char **argv) char c; errno = 0; r = recv(fd1, &c, 1, MSG_OOB); - if (r < 1) { + if (r < 1) SHUT_FD1; - } else + else send(fd2, &c, 1, MSG_OOB); } if (fd2 > 0) @@ -763,45 +763,45 @@ main(int argc, char **argv) char c; errno = 0; r = recv(fd2, &c, 1, MSG_OOB); - if (r < 1) { + if (r < 1) SHUT_FD1; - } else + else send(fd1, &c, 1, MSG_OOB); } if (fd1 > 0) if (FD_ISSET(fd1, &rd)) { r = read(fd1, buf1 + buf1_avail, BUF_SIZE \- buf1_avail); - if (r < 1) { + if (r < 1) SHUT_FD1; - } else + else buf1_avail += r; } if (fd2 > 0) if (FD_ISSET(fd2, &rd)) { r = read(fd2, buf2 + buf2_avail, BUF_SIZE \- buf2_avail); - if (r < 1) { + if (r < 1) SHUT_FD2; - } else + else buf2_avail += r; } if (fd1 > 0) if (FD_ISSET(fd1, &wr)) { r = write(fd1, buf2 + buf2_written, buf2_avail \- buf2_written); - if (r < 1) { + if (r < 1) SHUT_FD1; - } else + else buf2_written += r; } if (fd2 > 0) if (FD_ISSET(fd2, &wr)) { r = write(fd2, buf1 + buf1_written, buf1_avail \- buf1_written); - if (r < 1) { + if (r < 1) SHUT_FD2; - } else + else buf1_written += r; } @@ -815,12 +815,10 @@ main(int argc, char **argv) /* one side has closed the connection, keep writing to the other side until empty */ - if (fd1 < 0 && buf1_avail \- buf1_written == 0) { + if (fd1 < 0 && buf1_avail \- buf1_written == 0) SHUT_FD2; - } - if (fd2 < 0 && buf2_avail \- buf2_written == 0) { + if (fd2 < 0 && buf2_avail \- buf2_written == 0) SHUT_FD1; - } } exit(EXIT_SUCCESS); } |
