From: ray Date: Wed, 28 Jul 2010 09:07:11 +0000 (+0000) Subject: Make BUFs autoextend by default. This was already done in OpenCVS X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=0ee1412879042d6560e9f2f9513e0609d85aea59;p=openbsd Make BUFs autoextend by default. This was already done in OpenCVS a while ago. OK zinovik nicm --- diff --git a/usr.bin/rcs/buf.c b/usr.bin/rcs/buf.c index 43c67439827..3e81c55c999 100644 --- a/usr.bin/rcs/buf.c +++ b/usr.bin/rcs/buf.c @@ -1,4 +1,4 @@ -/* $OpenBSD: buf.c,v 1.15 2010/07/23 21:46:05 ray Exp $ */ +/* $OpenBSD: buf.c,v 1.16 2010/07/28 09:07:11 ray Exp $ */ /* * Copyright (c) 2003 Jean-Francois Brousseau * All rights reserved. @@ -42,8 +42,6 @@ #define BUF_INCR 128 struct buf { - u_int cb_flags; - /* buffer handle, buffer size, and data length */ u_char *cb_buf; size_t cb_size; @@ -62,7 +60,7 @@ static void buf_grow(BUF *, size_t); * once the buffer is no longer needed. */ BUF * -buf_alloc(size_t len, u_int flags) +buf_alloc(size_t len) { BUF *b; @@ -73,7 +71,6 @@ buf_alloc(size_t len, u_int flags) else b->cb_buf = NULL; - b->cb_flags = flags; b->cb_size = len; b->cb_len = 0; @@ -89,7 +86,7 @@ buf_alloc(size_t len, u_int flags) * Sets errno on error. */ BUF * -buf_load(const char *path, u_int flags) +buf_load(const char *path) { int fd; ssize_t ret; @@ -110,7 +107,7 @@ buf_load(const char *path, u_int flags) errno = EFBIG; goto out; } - buf = buf_alloc(st.st_size, flags); + buf = buf_alloc(st.st_size); for (bp = buf->cb_buf; ; bp += (size_t)ret) { len = SIZE_LEFT(buf); ret = read(fd, bp, len); @@ -200,10 +197,7 @@ buf_putc(BUF *b, int c) bp = b->cb_buf + b->cb_len; if (bp == (b->cb_buf + b->cb_size)) { /* extend */ - if (b->cb_flags & BUF_AUTOEXT) - buf_grow(b, (size_t)BUF_INCR); - else - errx(1, "buf_putc failed"); + buf_grow(b, BUF_INCR); /* the buffer might have been moved */ bp = b->cb_buf + b->cb_len; @@ -228,8 +222,7 @@ buf_getc(BUF *b, size_t pos) * 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 + * buffer is too small to accept all data, it * will get resized to an appropriate size to accept all data. * Returns the number of bytes successfully appended to the buffer. */ @@ -245,11 +238,8 @@ buf_append(BUF *b, const void *data, size_t len) rlen = len; if (left < len) { - if (b->cb_flags & BUF_AUTOEXT) { - buf_grow(b, len - left); - bp = b->cb_buf + b->cb_len; - } else - rlen = bep - bp; + buf_grow(b, len - left); + bp = b->cb_buf + b->cb_len; } memcpy(bp, data, rlen); diff --git a/usr.bin/rcs/buf.h b/usr.bin/rcs/buf.h index 990a4b8195f..a4ec9237237 100644 --- a/usr.bin/rcs/buf.h +++ b/usr.bin/rcs/buf.h @@ -1,4 +1,4 @@ -/* $OpenBSD: buf.h,v 1.10 2010/07/23 21:46:05 ray Exp $ */ +/* $OpenBSD: buf.h,v 1.11 2010/07/28 09:07:11 ray Exp $ */ /* * Copyright (c) 2003 Jean-Francois Brousseau * All rights reserved. @@ -40,13 +40,10 @@ #include -/* flags */ -#define BUF_AUTOEXT 1 /* autoextend on append */ - typedef struct buf BUF; -BUF *buf_alloc(size_t, u_int); -BUF *buf_load(const char *, u_int); +BUF *buf_alloc(size_t); +BUF *buf_load(const char *); void buf_free(BUF *); void *buf_release(BUF *); u_char buf_getc(BUF *, size_t); diff --git a/usr.bin/rcs/ci.c b/usr.bin/rcs/ci.c index faa70c9ddec..6740f665db2 100644 --- a/usr.bin/rcs/ci.c +++ b/usr.bin/rcs/ci.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ci.c,v 1.208 2010/07/23 23:59:32 ray Exp $ */ +/* $OpenBSD: ci.c,v 1.209 2010/07/28 09:07:11 ray Exp $ */ /* * Copyright (c) 2005, 2006 Niall O'Higgins * All rights reserved. @@ -348,7 +348,7 @@ checkin_diff_file(struct checkin_params *pb) b1 = b2 = b3 = NULL; path1 = path2 = NULL; - if ((b1 = buf_load(pb->filename, BUF_AUTOEXT)) == NULL) { + if ((b1 = buf_load(pb->filename)) == NULL) { warnx("failed to load file: `%s'", pb->filename); goto out; } @@ -358,11 +358,7 @@ checkin_diff_file(struct checkin_params *pb) goto out; } b2 = rcs_kwexp_buf(b2, pb->file, pb->frev); - - if ((b3 = buf_alloc(128, BUF_AUTOEXT)) == NULL) { - warnx("failed to allocated buffer for diff"); - goto out; - } + b3 = buf_alloc(128); (void)xasprintf(&path1, "%s/diff1.XXXXXXXXXX", rcs_tmpdir); buf_write_stmp(b1, path1); @@ -451,7 +447,7 @@ checkin_update(struct checkin_params *pb) pb->frev = pb->file->rf_head; /* Load file contents */ - if ((bp = buf_load(pb->filename, BUF_AUTOEXT)) == NULL) + if ((bp = buf_load(pb->filename)) == NULL) return (-1); /* If this is a zero-ending RCSNUM eg 4.0, increment it (eg to 4.1) */ @@ -627,7 +623,7 @@ checkin_init(struct checkin_params *pb) } /* Load file contents */ - if ((bp = buf_load(pb->filename, BUF_AUTOEXT)) == NULL) + if ((bp = buf_load(pb->filename)) == NULL) return (-1); /* Get default values from working copy if -k specified */ @@ -918,7 +914,7 @@ checkin_keywordscan(BUF *data, RCSNUM **rev, time_t *date, char **author, } len = c - start + 1; - buf = buf_alloc(len + 1, 0); + buf = buf_alloc(len + 1); buf_append(buf, start, len); /* XXX - Not binary safe. */ diff --git a/usr.bin/rcs/co.c b/usr.bin/rcs/co.c index 5268c15abd9..90c9e14ceca 100644 --- a/usr.bin/rcs/co.c +++ b/usr.bin/rcs/co.c @@ -1,4 +1,4 @@ -/* $OpenBSD: co.c,v 1.111 2010/07/23 21:46:05 ray Exp $ */ +/* $OpenBSD: co.c,v 1.112 2010/07/28 09:07:11 ray Exp $ */ /* * Copyright (c) 2005 Joris Vink * All rights reserved. @@ -354,7 +354,7 @@ checkout_rev(RCSFILE *file, RCSNUM *frev, const char *dst, int flags, return (-1); } } else { - bp = buf_alloc(1, 0); + bp = buf_alloc(1); } /* diff --git a/usr.bin/rcs/diff3.c b/usr.bin/rcs/diff3.c index faa995bdfa3..228318cfbbf 100644 --- a/usr.bin/rcs/diff3.c +++ b/usr.bin/rcs/diff3.c @@ -1,4 +1,4 @@ -/* $OpenBSD: diff3.c,v 1.30 2010/07/23 21:46:05 ray Exp $ */ +/* $OpenBSD: diff3.c,v 1.31 2010/07/28 09:07:11 ray Exp $ */ /* * Copyright (C) Caldera International Inc. 2001-2002. @@ -166,16 +166,16 @@ merge_diff3(char **av, int flags) if ((flags & MERGE_EFLAG) && !(flags & MERGE_OFLAG)) oflag = 0; - if ((b1 = buf_load(av[0], BUF_AUTOEXT)) == NULL) + if ((b1 = buf_load(av[0])) == NULL) goto out; - if ((b2 = buf_load(av[1], BUF_AUTOEXT)) == NULL) + if ((b2 = buf_load(av[1])) == NULL) goto out; - if ((b3 = buf_load(av[2], BUF_AUTOEXT)) == NULL) + if ((b3 = buf_load(av[2])) == NULL) goto out; - d1 = buf_alloc(128, BUF_AUTOEXT); - d2 = buf_alloc(128, BUF_AUTOEXT); - diffb = buf_alloc(128, BUF_AUTOEXT); + d1 = buf_alloc(128); + d2 = buf_alloc(128); + diffb = buf_alloc(128); (void)xasprintf(&path1, "%s/diff1.XXXXXXXXXX", rcs_tmpdir); (void)xasprintf(&path2, "%s/diff2.XXXXXXXXXX", rcs_tmpdir); @@ -287,7 +287,7 @@ rcs_diff3(RCSFILE *rf, char *workfile, RCSNUM *rev1, RCSNUM *rev2, int flags) rcsnum_tostr(rev1, r1, sizeof(r1)); rcsnum_tostr(rev2, r2, sizeof(r2)); - if ((b1 = buf_load(workfile, BUF_AUTOEXT)) == NULL) + if ((b1 = buf_load(workfile)) == NULL) goto out; if (!(flags & QUIET)) @@ -300,9 +300,9 @@ rcs_diff3(RCSFILE *rf, char *workfile, RCSNUM *rev1, RCSNUM *rev2, int flags) if ((b3 = rcs_getrev(rf, rev2)) == NULL) goto out; - d1 = buf_alloc(128, BUF_AUTOEXT); - d2 = buf_alloc(128, BUF_AUTOEXT); - diffb = buf_alloc(128, BUF_AUTOEXT); + d1 = buf_alloc(128); + d2 = buf_alloc(128); + diffb = buf_alloc(128); (void)xasprintf(&path1, "%s/diff1.XXXXXXXXXX", rcs_tmpdir); (void)xasprintf(&path2, "%s/diff2.XXXXXXXXXX", rcs_tmpdir); diff --git a/usr.bin/rcs/ident.c b/usr.bin/rcs/ident.c index be68267fbc4..f2c0a554455 100644 --- a/usr.bin/rcs/ident.c +++ b/usr.bin/rcs/ident.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ident.c,v 1.27 2010/07/23 21:46:05 ray Exp $ */ +/* $OpenBSD: ident.c,v 1.28 2010/07/28 09:07:11 ray Exp $ */ /* * Copyright (c) 2005 Xavier Santolaria * All rights reserved. @@ -119,7 +119,7 @@ ident_line(FILE *fp) BUF *bp; size_t len; - bp = buf_alloc(512, BUF_AUTOEXT); + bp = buf_alloc(512); while ((c = getc(fp)) != VALDELIM) { if (c == EOF) diff --git a/usr.bin/rcs/rcs.c b/usr.bin/rcs/rcs.c index 94870dffc19..7adaa19028e 100644 --- a/usr.bin/rcs/rcs.c +++ b/usr.bin/rcs/rcs.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rcs.c,v 1.59 2010/07/23 21:46:05 ray Exp $ */ +/* $OpenBSD: rcs.c,v 1.60 2010/07/28 09:07:11 ray Exp $ */ /* * Copyright (c) 2004 Jean-Francois Brousseau * All rights reserved. @@ -1190,12 +1190,12 @@ rcs_getrev(RCSFILE *rfp, RCSNUM *frev) len = rdp->rd_tlen; if (len == 0) { - rbuf = buf_alloc(1, 0); + rbuf = buf_alloc(1); buf_empty(rbuf); return (rbuf); } - rbuf = buf_alloc(len, BUF_AUTOEXT); + rbuf = buf_alloc(len); buf_append(rbuf, rdp->rd_text, len); isbranch = 0; @@ -1455,7 +1455,7 @@ rcs_rev_remove(RCSFILE *rf, RCSNUM *rev) if ((nextbuf = rcs_getrev(rf, nextrdp->rd_num)) == NULL) errx(1, "error getting revision"); - newdiff = buf_alloc(64, BUF_AUTOEXT); + newdiff = buf_alloc(64); /* calculate new diff */ (void)xasprintf(&path_tmp1, "%s/diff1.XXXXXXXXXX", rcs_tmpdir); @@ -2832,7 +2832,7 @@ rcs_expand_keywords(char *rcsfile, struct rcs_delta *rdp, BUF *bp, int mode) errx(1, "rcs_expand_keywords: string truncated"); /* Concatenate everything together. */ - tmpbuf = buf_alloc(len + strlen(expbuf), BUF_AUTOEXT); + tmpbuf = buf_alloc(len + strlen(expbuf)); /* Append everything before keyword. */ buf_append(tmpbuf, buf_get(newbuf), start - (unsigned char *)buf_get(newbuf)); diff --git a/usr.bin/rcs/rcsclean.c b/usr.bin/rcs/rcsclean.c index f4cd973b739..80b7d2af75f 100644 --- a/usr.bin/rcs/rcsclean.c +++ b/usr.bin/rcs/rcsclean.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rcsclean.c,v 1.51 2010/07/23 21:46:05 ray Exp $ */ +/* $OpenBSD: rcsclean.c,v 1.52 2010/07/28 09:07:11 ray Exp $ */ /* * Copyright (c) 2005 Joris Vink * All rights reserved. @@ -165,7 +165,7 @@ rcsclean_file(char *fname, const char *rev_str) warnx("failed to get needed revision"); goto out; } - if ((b2 = buf_load(fname, 0)) == NULL) { + if ((b2 = buf_load(fname)) == NULL) { warnx("failed to load `%s'", fname); goto out; } diff --git a/usr.bin/rcs/rcsdiff.c b/usr.bin/rcs/rcsdiff.c index 1ba7a00678b..d84b4edb31b 100644 --- a/usr.bin/rcs/rcsdiff.c +++ b/usr.bin/rcs/rcsdiff.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rcsdiff.c,v 1.76 2010/07/23 21:46:05 ray Exp $ */ +/* $OpenBSD: rcsdiff.c,v 1.77 2010/07/28 09:07:11 ray Exp $ */ /* * Copyright (c) 2005 Joris Vink * All rights reserved. @@ -316,7 +316,7 @@ rcsdiff_file(RCSFILE *file, RCSNUM *rev, const char *filename, int dflags) tv[0].tv_sec = (long)rcs_rev_getdate(file, rev); tv[1].tv_sec = tv[0].tv_sec; - if ((b2 = buf_load(filename, BUF_AUTOEXT)) == NULL) { + if ((b2 = buf_load(filename)) == NULL) { warnx("failed to load file: `%s'", filename); goto out; } diff --git a/usr.bin/rcs/rcsutil.c b/usr.bin/rcs/rcsutil.c index a4fa4928387..80948a00408 100644 --- a/usr.bin/rcs/rcsutil.c +++ b/usr.bin/rcs/rcsutil.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rcsutil.c,v 1.34 2010/07/23 21:46:05 ray Exp $ */ +/* $OpenBSD: rcsutil.c,v 1.35 2010/07/28 09:07:11 ray Exp $ */ /* * Copyright (c) 2005, 2006 Joris Vink * Copyright (c) 2006 Xavier Santolaria @@ -337,7 +337,7 @@ rcs_prompt(const char *prompt) size_t len; char *buf; - bp = buf_alloc(0, BUF_AUTOEXT); + bp = buf_alloc(0); if (isatty(STDIN_FILENO)) (void)fprintf(stderr, "%s", prompt); if (isatty(STDIN_FILENO)) @@ -451,7 +451,7 @@ rcs_set_description(RCSFILE *file, const char *in) /* Description is in file . */ if (in != NULL && *in != '-') { - if ((bp = buf_load(in, BUF_AUTOEXT)) == NULL) + if ((bp = buf_load(in)) == NULL) return (-1); buf_putc(bp, '\0'); content = buf_release(bp); @@ -535,7 +535,7 @@ rcs_patchfile(u_char *data, size_t dlen, u_char *patch, size_t plen, return (NULL); } - res = buf_alloc(1024, BUF_AUTOEXT); + res = buf_alloc(1024); TAILQ_FOREACH(lp, &dlines->l_lines, l_list) { if (lp->l_line == NULL) continue;