term.c (5543:3af77710f397) | term.c (5579:29cd33ac16cb) |
---|---|
1/* $OpenBSD: netcat.c,v 1.57 2002/12/30 18:00:18 stevesk Exp $ */ 2/* 3 * Copyright (c) 2001 Eric Jackson <ericj@monkey.org> 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * --- 125 unchanged lines hidden (view full) --- 134 135 freeaddrinfo(res); 136 137 return (s); 138} 139 140/* 141 * readwrite() | 1/* $OpenBSD: netcat.c,v 1.57 2002/12/30 18:00:18 stevesk Exp $ */ 2/* 3 * Copyright (c) 2001 Eric Jackson <ericj@monkey.org> 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * --- 125 unchanged lines hidden (view full) --- 134 135 freeaddrinfo(res); 136 137 return (s); 138} 139 140/* 141 * readwrite() |
142 * Loop that polls on the network file descriptor and stdin. | 142 * Loop that selects on the network file descriptor and stdin. 143 * Changed from poll() by Ali Saidi to make work on Mac OS X >= 10.4 |
143 */ 144void 145readwrite(int nfd) 146{ | 144 */ 145void 146readwrite(int nfd) 147{ |
147 struct pollfd pfd[2]; | 148 fd_set read_fds; |
148 char buf[BUFSIZ]; | 149 char buf[BUFSIZ]; |
149 int wfd = fileno(stdin), n, ret; | 150 int wfd = fileno(stdin), n, ret, max_fd; |
150 int lfd = fileno(stdout); 151 int escape = 0; | 151 int lfd = fileno(stdout); 152 int escape = 0; |
153 struct timeval timeout; |
|
152 | 154 |
153 /* Setup Network FD */ 154 pfd[0].fd = nfd; 155 pfd[0].events = POLLIN; | 155 if (nfd == -1) 156 return; |
156 | 157 |
157 /* Setup STDIN FD */ 158 pfd[1].fd = wfd; 159 pfd[1].events = POLLIN; | 158 FD_ZERO(&read_fds); |
160 | 159 |
161 while (pfd[0].fd != -1) { 162 if ((n = poll(pfd, 2, -1)) < 0) { | 160 FD_SET(wfd, &read_fds); 161 FD_SET(nfd, &read_fds); 162 max_fd = nfd + 1; 163 164 timeout.tv_sec = 1; 165 timeout.tv_usec = 0; 166 167 while (1) { 168 n = select(max_fd, &read_fds, NULL, NULL, &timeout); 169 if (n < 0) { |
163 close(nfd); | 170 close(nfd); |
164 err(1, "Polling Error"); | 171 perror("Select Error:"); |
165 } 166 | 172 } 173 |
167 if (n == 0) | 174 if (n == 0) { 175 if (read(nfd, buf, 0) < 0) 176 return; 177 goto setup_select; 178 } 179 180 if (read(nfd, buf, 0) < 0) |
168 return; 169 | 181 return; 182 |
170 if (pfd[0].revents & POLLIN) { | 183 if (FD_ISSET(nfd, &read_fds)) { |
171 if ((n = read(nfd, buf, sizeof(buf))) < 0) 172 return; 173 else if (n == 0) { 174 shutdown(nfd, SHUT_RD); | 184 if ((n = read(nfd, buf, sizeof(buf))) < 0) 185 return; 186 else if (n == 0) { 187 shutdown(nfd, SHUT_RD); |
175 pfd[0].fd = -1; 176 pfd[0].events = 0; | 188 return; |
177 } else { 178 if ((ret = atomicio(write, lfd, buf, n)) != n) 179 return; 180 } 181 } 182 | 189 } else { 190 if ((ret = atomicio(write, lfd, buf, n)) != n) 191 return; 192 } 193 } 194 |
183 if (pfd[1].revents & POLLIN) { | 195 if (FD_ISSET(wfd, &read_fds)) { |
184 if ((n = read(wfd, buf, sizeof(buf))) < 0) 185 return; 186 else if (n == 0) { 187 shutdown(nfd, SHUT_WR); | 196 if ((n = read(wfd, buf, sizeof(buf))) < 0) 197 return; 198 else if (n == 0) { 199 shutdown(nfd, SHUT_WR); |
188 pfd[1].fd = -1; 189 pfd[1].events = 0; | |
190 } else { 191 if (escape) { 192 char buf2[] = "~"; 193 if (*buf == '.') { 194 printf("quit!\n"); 195 return; 196 } 197 escape = 0; --- 5 unchanged lines hidden (view full) --- 203 if (escape) 204 continue; 205 } 206 207 if ((ret = atomicio(write, nfd, buf, n)) != n) 208 return; 209 } 210 } | 200 } else { 201 if (escape) { 202 char buf2[] = "~"; 203 if (*buf == '.') { 204 printf("quit!\n"); 205 return; 206 } 207 escape = 0; --- 5 unchanged lines hidden (view full) --- 213 if (escape) 214 continue; 215 } 216 217 if ((ret = atomicio(write, nfd, buf, n)) != n) 218 return; 219 } 220 } |
211 } | 221setup_select: 222 FD_ZERO(&read_fds); 223 FD_SET(wfd, &read_fds); 224 FD_SET(nfd, &read_fds); 225 timeout.tv_sec = 1; 226 timeout.tv_usec = 0; 227 } // while |
212} 213 214void 215usage(int ret) 216{ 217 fprintf(stderr, "usage: %s hostname port\n", progname); 218 if (ret) 219 exit(1); --- 95 unchanged lines hidden --- | 228} 229 230void 231usage(int ret) 232{ 233 fprintf(stderr, "usage: %s hostname port\n", progname); 234 if (ret) 235 exit(1); --- 95 unchanged lines hidden --- |