simple line buffering */
| 413 | |
| 414 | /* simple line buffering */ |
| 415 | static int buffer_gets(struct imap_buffer *b, char **s) |
| 416 | { |
| 417 | int n; |
| 418 | int start = b->offset; |
| 419 | |
| 420 | *s = b->buf + start; |
| 421 | |
| 422 | for (;;) { |
| 423 | /* make sure we have enough data to read the \r\n sequence */ |
| 424 | if (b->offset + 1 >= b->bytes) { |
| 425 | if (start) { |
| 426 | /* shift down used bytes */ |
| 427 | *s = b->buf; |
| 428 | |
| 429 | assert(start <= b->bytes); |
| 430 | n = b->bytes - start; |
| 431 | |
| 432 | if (n) |
| 433 | memmove(b->buf, b->buf + start, n); |
| 434 | b->offset -= start; |
| 435 | b->bytes = n; |
| 436 | start = 0; |
| 437 | } |
| 438 | |
| 439 | n = socket_read(&b->sock, b->buf + b->bytes, |
| 440 | sizeof(b->buf) - b->bytes); |
| 441 | |
| 442 | if (n <= 0) |
| 443 | return -1; |
| 444 | |
| 445 | b->bytes += n; |
| 446 | } |
| 447 | |
| 448 | if (b->buf[b->offset] == '\r') { |
| 449 | assert(b->offset + 1 < b->bytes); |
| 450 | if (b->buf[b->offset + 1] == '\n') { |
| 451 | b->buf[b->offset] = 0; /* terminate the string */ |
| 452 | b->offset += 2; /* next line */ |
| 453 | if ((0 < verbosity) || (list_folders && strstr(*s, "* LIST"))) |
| 454 | puts(*s); |
| 455 | return 0; |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | b->offset++; |
| 460 | } |
| 461 | /* not reached */ |
| 462 | } |
| 463 | |
| 464 | __attribute__((format (printf, 1, 2))) |
| 465 | static void imap_info(const char *msg, ...) |
no test coverage detected