1/*- 2 * Copyright (c) 1998 Dag-Erling Co�dan Sm�rgrav 3 * All rights reserved. 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 * 1. Redistributions of source code must retain the above copyright --- 11 unchanged lines hidden (view full) --- 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * |
28 * $FreeBSD: head/lib/libfetch/common.c 55557 2000-01-07 12:58:40Z des $ |
29 */ 30 31#include <sys/param.h> 32#include <sys/socket.h> |
33#include <sys/time.h> |
34#include <netinet/in.h> 35 36#include <com_err.h> 37#include <errno.h> 38#include <netdb.h> 39#include <stdlib.h> 40#include <stdio.h> 41#include <string.h> --- 196 unchanged lines hidden (view full) --- 238 close(sd); 239 return -1; 240 } 241 242 return sd; 243} 244 245 |
246/* 247 * Read a line of text from a socket w/ timeout 248 */ 249#define MIN_BUF_SIZE 1024 250 251int 252_fetch_getln(int fd, char **buf, size_t *size, size_t *len) 253{ 254 struct timeval now, timeout, wait; 255 fd_set readfds; 256 int r; 257 char c; 258 259 if (*buf == NULL) { 260 if ((*buf = malloc(MIN_BUF_SIZE)) == NULL) { 261 errno = ENOMEM; 262 return -1; 263 } 264 *size = MIN_BUF_SIZE; 265 } 266 267 **buf = '\0'; 268 *len = 0; 269 270 if (fetchTimeout) { 271 gettimeofday(&timeout, NULL); 272 timeout.tv_sec += fetchTimeout; 273 FD_ZERO(&readfds); 274 } 275 276 do { 277 if (fetchTimeout) { 278 FD_SET(fd, &readfds); 279 gettimeofday(&now, NULL); 280 wait.tv_sec = timeout.tv_sec - now.tv_sec; 281 wait.tv_usec = timeout.tv_usec - now.tv_usec; 282 if (wait.tv_usec < 0) { 283 wait.tv_usec += 1000000; 284 wait.tv_sec--; 285 } 286 if (wait.tv_sec < 0) { 287 errno = ETIMEDOUT; 288 return -1; 289 } 290 r = select(fd+1, &readfds, NULL, NULL, &wait); 291 if (r == -1) { 292 if (errno == EINTR) 293 continue; 294 /* EBADF or EINVAL: shouldn't happen */ 295 return -1; 296 } 297 if (!FD_ISSET(fd, &readfds)) 298 continue; 299 } 300 r = read(fd, &c, 1); 301 if (r == 0) 302 break; 303 if (r == -1) { 304 if (errno == EINTR) 305 continue; 306 /* any other error is bad news */ 307 return -1; 308 } 309 (*buf)[*len] = c; 310 *len += 1; 311 if (*len == *size) { 312 char *tmp; 313 314 if ((tmp = realloc(*buf, *size * 2 + 1)) == NULL) { 315 errno = ENOMEM; 316 return -1; 317 } 318 *buf = tmp; 319 *size = *size * 2 + 1; 320 } 321 } while (c != '\n'); 322 323 return 0; 324} 325 326 |
327/*** Directory-related utility functions *************************************/ 328 329int 330_fetch_add_entry(struct url_ent **p, int *size, int *len, 331 char *name, struct url_stat *stat) 332{ 333 struct url_ent *tmp; 334 --- 32 unchanged lines hidden --- |