2 * Copyright 2011 Red Hat Inc., Durham, North Carolina.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Steve Grubb <sgrubb@redhat.com>
28 #include "remote-fgets.h"
31 static char buffer[2*BUF_SIZE+1] = { 0 };
32 static char *current = buffer;
33 static char *const eptr = buffer+(2*BUF_SIZE);
36 int remote_fgets_eof(void)
41 /* Function to check if we have more data stored
42 * and ready to process. If we have a newline or enough
43 * bytes we return 1 for success. Otherwise 0 meaning that
44 * there is not enough to process without blocking. */
45 int remote_fgets_more(size_t blen)
47 char *ptr = strchr(buffer, '\n');
49 if (ptr || (size_t)(current-buffer) >= blen-1)
54 int remote_fgets(char *buf, size_t blen, int fd)
58 char *line_end = NULL;
61 /* See if we have more in the buffer first */
62 if (current != buffer) {
63 line_end = strchr(buffer, '\n');
64 if (line_end == NULL && (size_t)(current - buffer) >= blen-1)
65 line_end = current-1; // have enough to fill blen, so point to end
68 /* Otherwise get some new bytes */
69 if (line_end == NULL && current != eptr && !eof) {
72 /* Use current since we may be adding more */
74 len = read(fd, current, eptr - current);
75 } while (len < 0 && errno == EINTR);
84 /* Start from beginning to see if we have one */
85 line_end = strchr(buffer, '\n');
88 /* See what we have */
90 /* Include the last character (usually newline) */
91 line_len = (line_end+1) - buffer;
92 /* Make sure we are within the right size */
93 if (line_len > blen-1)
96 } else if (current == eptr) {
97 /* We are full but no newline */
100 } else if (current >= buffer+blen-1) {
101 /* Not completely full, no newline, but enough to fill buf */
106 size_t remainder_len;
108 /* Move to external buf and terminate it */
109 memcpy(buf, buffer, line_len);
111 remainder_len = current - (buffer + line_len);
112 if (remainder_len > 0) {
113 /* We have a few leftover bytes to move */
114 memmove(buffer, buffer+line_len, remainder_len);
115 current = buffer+remainder_len;
117 /* Got the whole thing, just reset */