diff options
Diffstat (limited to 'lib/buffer.c')
| -rw-r--r-- | lib/buffer.c | 48 |
1 files changed, 44 insertions, 4 deletions
diff --git a/lib/buffer.c b/lib/buffer.c index 192a647eca..a7c4fe4f2f 100644 --- a/lib/buffer.c +++ b/lib/buffer.c @@ -14,10 +14,9 @@ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * - * You should have received a copy of the GNU General Public License - * along with GNU Zebra; see the file COPYING. If not, write to the - * Free Software Foundation, Inc., 59 Temple Place - Suite 330, - * Boston, MA 02111-1307, USA. + * You should have received a copy of the GNU General Public License along + * with this program; see the file COPYING; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #include <zebra.h> @@ -186,6 +185,47 @@ void buffer_putstr(struct buffer *b, const char *c) buffer_put(b, c, strlen(c)); } +/* Expand \n to \r\n */ +void buffer_put_crlf(struct buffer *b, const void *origp, size_t origsize) +{ + struct buffer_data *data = b->tail; + const char *p = origp, *end = p + origsize, *lf; + size_t size; + + lf = memchr(p, '\n', end - p); + + /* We use even last one byte of data buffer. */ + while (p < end) { + size_t avail, chunk; + + /* If there is no data buffer add it. */ + if (data == NULL || data->cp == b->size) + data = buffer_add(b); + + size = (lf ? lf : end) - p; + avail = b->size - data->cp; + + chunk = (size <= avail) ? size : avail; + memcpy(data->data + data->cp, p, chunk); + + p += chunk; + data->cp += chunk; + + if (lf && size <= avail) { + /* we just copied up to (including) a '\n' */ + if (data->cp == b->size) + data = buffer_add(b); + data->data[data->cp++] = '\r'; + if (data->cp == b->size) + data = buffer_add(b); + data->data[data->cp++] = '\n'; + + p++; + lf = memchr(p, '\n', end - p); + } + } +} + /* Keep flushing data to the fd until the buffer is empty or an error is encountered or the operation would block. */ buffer_status_t buffer_flush_all(struct buffer *b, int fd) |
