=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/cvs/buf.c,v retrieving revision 1.73 retrieving revision 1.74 diff -u -r1.73 -r1.74 --- src/usr.bin/cvs/buf.c 2009/03/23 18:21:23 1.73 +++ src/usr.bin/cvs/buf.c 2009/03/25 21:19:20 1.74 @@ -1,4 +1,4 @@ -/* $OpenBSD: buf.c,v 1.73 2009/03/23 18:21:23 joris Exp $ */ +/* $OpenBSD: buf.c,v 1.74 2009/03/25 21:19:20 joris Exp $ */ /* * Copyright (c) 2003 Jean-Francois Brousseau * All rights reserved. @@ -38,30 +38,24 @@ #include "buf.h" #define BUF_INCR 128 +#define BUF_GROW(bp, len) \ + do { \ + b->cb_buf = xrealloc(b->cb_buf, 1, b->cb_size + len); \ + b->cb_size += len; \ + } while (0); struct cvs_buf { - /* buffer handle, buffer size, and data length */ u_char *cb_buf; size_t cb_size; size_t cb_len; }; -static void cvs_buf_grow(BUF *, size_t); - -/* - * cvs_buf_alloc() - * - * Create a new buffer structure and return a pointer to it. This structure - * uses dynamically-allocated memory and must be freed with cvs_buf_free(), - * once the buffer is no longer needed. - */ BUF * cvs_buf_alloc(size_t len) { BUF *b; b = xmalloc(sizeof(*b)); - /* Postpone creation of zero-sized buffers */ if (len > 0) b->cb_buf = xcalloc(1, len); else @@ -110,11 +104,6 @@ return (buf); } -/* - * cvs_buf_free() - * - * Free the buffer and all associated data. - */ void cvs_buf_free(BUF *b) { @@ -123,13 +112,6 @@ xfree(b); } -/* - * cvs_buf_release() - * - * Free the buffer 's structural information but do not free the contents - * of the buffer. Instead, they are returned and should be freed later using - * free(). - */ u_char * cvs_buf_release(BUF *b) { @@ -140,72 +122,26 @@ return (tmp); } -/* - * cvs_buf_empty() - * - * Empty the contents of the buffer and reset pointers. - */ void -cvs_buf_empty(BUF *b) -{ - memset(b->cb_buf, 0, b->cb_size); - b->cb_len = 0; -} - -/* - * cvs_buf_putc() - * - * Append a single character to the end of the buffer . - */ -void cvs_buf_putc(BUF *b, int c) { u_char *bp; bp = b->cb_buf + b->cb_len; if (bp == (b->cb_buf + b->cb_size)) { - /* extend */ - cvs_buf_grow(b, (size_t)BUF_INCR); - - /* the buffer might have been moved */ + BUF_GROW(b, BUF_INCR); bp = b->cb_buf + b->cb_len; } *bp = (u_char)c; b->cb_len++; } -/* - * cvs_buf_puts() - * - * Wrapper function for constant strings to cvs_buf_append. - */ void cvs_buf_puts(BUF *b, const char *str) { cvs_buf_append(b, str, strlen(str)); } -/* - * cvs_buf_getc() - * - * Return u_char at buffer position . - * - */ -u_char -cvs_buf_getc(BUF *b, size_t pos) -{ - return (b->cb_buf[pos]); -} - -/* - * cvs_buf_append() - * - * Append bytes of data pointed to by to the buffer . If the - * buffer is too small to accept all data, it will attempt to append as much - * data as possible, or if the BUF_AUTOEXT flag is set for the buffer, it - * will get resized to an appropriate size to accept all data. - * Returns the number of bytes successfully appended to the buffer. - */ void cvs_buf_append(BUF *b, const void *data, size_t len) { @@ -217,7 +153,7 @@ left = bep - bp; if (left < len) { - cvs_buf_grow(b, len - left); + BUF_GROW(b, len - left); bp = b->cb_buf + b->cb_len; } @@ -225,22 +161,12 @@ b->cb_len += len; } -/* - * cvs_buf_len() - * - * Returns the size of the buffer that is being used. - */ size_t cvs_buf_len(BUF *b) { return (b->cb_len); } -/* - * cvs_buf_write_fd() - * - * Write the contents of the buffer to the specified - */ int cvs_buf_write_fd(BUF *b, int fd) { @@ -249,17 +175,11 @@ return (0); } -/* - * cvs_buf_write() - * - * Write the contents of the buffer to the file whose path is given in - * . If the file does not exist, it is created with mode . - */ int cvs_buf_write(BUF *b, const char *path, mode_t mode) { int fd; - open: +open: if ((fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, mode)) == -1) { if (errno == EACCES && unlink(path) != -1) goto open; @@ -280,13 +200,6 @@ return (0); } -/* - * cvs_buf_write_stmp() - * - * Write the contents of the buffer to a temporary file whose path is - * specified using