[BACK]Return to buf.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / cvs

Annotation of src/usr.bin/cvs/buf.c, Revision 1.29

1.29    ! niallo      1: /*     $OpenBSD: buf.c,v 1.28 2006/01/02 08:11:56 xsa Exp $    */
1.1       jfb         2: /*
1.2       jfb         3:  * Copyright (c) 2003 Jean-Francois Brousseau <jfb@openbsd.org>
1.1       jfb         4:  * All rights reserved.
                      5:  *
                      6:  * Redistribution and use in source and binary forms, with or without
                      7:  * modification, are permitted provided that the following conditions
                      8:  * are met:
                      9:  *
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. The name of the author may not be used to endorse or promote products
                     13:  *    derived from this software without specific prior written permission.
                     14:  *
                     15:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
                     16:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
                     17:  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
                     18:  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
                     19:  * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
                     20:  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
                     21:  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
                     22:  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
                     23:  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
                     24:  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     25:  */
                     26:
1.28      xsa        27: #include "includes.h"
1.1       jfb        28:
                     29: #include "buf.h"
                     30: #include "log.h"
1.19      joris      31: #include "xmalloc.h"
1.1       jfb        32:
1.15      xsa        33: #define BUF_INCR       128
1.1       jfb        34:
                     35: struct cvs_buf {
1.15      xsa        36:        u_int   cb_flags;
1.1       jfb        37:
                     38:        /* buffer handle and size */
1.15      xsa        39:        u_char  *cb_buf;
                     40:        size_t   cb_size;
1.1       jfb        41:
                     42:        /* start and length of valid data in buffer */
1.15      xsa        43:        u_char  *cb_cur;
                     44:        size_t   cb_len;
1.1       jfb        45: };
                     46:
1.16      moritz     47: #define SIZE_LEFT(b)   (b->cb_size - (size_t)(b->cb_cur - b->cb_buf) \
                     48:                            - b->cb_len)
1.1       jfb        49:
1.15      xsa        50: static ssize_t cvs_buf_grow(BUF *, size_t);
1.1       jfb        51:
                     52: /*
                     53:  * cvs_buf_alloc()
                     54:  *
                     55:  * Create a new buffer structure and return a pointer to it.  This structure
                     56:  * uses dynamically-allocated memory and must be freed with cvs_buf_free(),
                     57:  * once the buffer is no longer needed.
                     58:  */
1.15      xsa        59: BUF *
1.1       jfb        60: cvs_buf_alloc(size_t len, u_int flags)
                     61: {
                     62:        BUF *b;
                     63:
1.19      joris      64:        b = (BUF *)xmalloc(sizeof(*b));
                     65:        b->cb_buf = xmalloc(len);
1.3       joris      66:        memset(b->cb_buf, 0, len);
1.1       jfb        67:
                     68:        b->cb_flags = flags;
                     69:        b->cb_size = len;
1.8       jfb        70:        b->cb_cur = b->cb_buf;
1.1       jfb        71:        b->cb_len = 0;
                     72:
                     73:        return (b);
                     74: }
                     75:
                     76: /*
                     77:  * cvs_buf_load()
                     78:  *
                     79:  * Open the file specified by <path> and load all of its contents into a
                     80:  * buffer.
1.20      xsa        81:  * Returns the loaded buffer on success.
1.1       jfb        82:  */
1.15      xsa        83: BUF *
1.1       jfb        84: cvs_buf_load(const char *path, u_int flags)
                     85: {
                     86:        int fd;
                     87:        ssize_t ret;
                     88:        size_t len;
1.8       jfb        89:        u_char *bp;
1.1       jfb        90:        struct stat st;
                     91:        BUF *buf;
                     92:
1.20      xsa        93:        if ((fd = open(path, O_RDONLY, 0600)) == -1)
                     94:                fatal("cvs_buf_load: open: `%s': %s", path, strerror(errno));
1.1       jfb        95:
1.20      xsa        96:        if (fstat(fd, &st) == -1)
                     97:                fatal("cvs_buf_load: fstat: %s", strerror(errno));
1.1       jfb        98:
1.25      joris      99:        buf = cvs_buf_alloc((size_t)st.st_size, flags);
1.1       jfb       100:        for (bp = buf->cb_cur; ; bp += (size_t)ret) {
1.16      moritz    101:                len = SIZE_LEFT(buf);
1.1       jfb       102:                ret = read(fd, bp, len);
                    103:                if (ret == -1) {
1.8       jfb       104:                        cvs_buf_free(buf);
1.20      xsa       105:                        fatal("cvs_buf_load: read: %s", strerror(errno));
1.5       deraadt   106:                } else if (ret == 0)
1.1       jfb       107:                        break;
                    108:
                    109:                buf->cb_len += (size_t)ret;
                    110:        }
                    111:
                    112:        (void)close(fd);
                    113:
                    114:        return (buf);
                    115: }
                    116:
                    117: /*
                    118:  * cvs_buf_free()
                    119:  *
                    120:  * Free the buffer <b> and all associated data.
                    121:  */
                    122: void
                    123: cvs_buf_free(BUF *b)
                    124: {
1.19      joris     125:        xfree(b->cb_buf);
                    126:        xfree(b);
1.1       jfb       127: }
                    128:
                    129: /*
                    130:  * cvs_buf_release()
                    131:  *
                    132:  * Free the buffer <b>'s structural information but do not free the contents
                    133:  * of the buffer.  Instead, they are returned and should be freed later using
                    134:  * free().
                    135:  */
1.17      xsa       136: void *
1.1       jfb       137: cvs_buf_release(BUF *b)
                    138: {
1.8       jfb       139:        u_char *tmp;
1.6       tedu      140:
1.1       jfb       141:        tmp = b->cb_buf;
1.19      joris     142:        xfree(b);
1.1       jfb       143:        return (tmp);
                    144: }
                    145:
                    146: /*
                    147:  * cvs_buf_empty()
                    148:  *
                    149:  * Empty the contents of the buffer <b> and reset pointers.
                    150:  */
                    151: void
                    152: cvs_buf_empty(BUF *b)
                    153: {
1.16      moritz    154:        memset(b->cb_buf, 0, b->cb_size);
1.8       jfb       155:        b->cb_cur = b->cb_buf;
1.1       jfb       156:        b->cb_len = 0;
                    157: }
                    158:
                    159: /*
                    160:  * cvs_buf_copy()
                    161:  *
                    162:  * Copy the first <len> bytes of data in the buffer <b> starting at offset
                    163:  * <off> in the destination buffer <dst>, which can accept up to <len> bytes.
                    164:  * Returns the number of bytes successfully copied, or -1 on failure.
                    165:  */
                    166: ssize_t
                    167: cvs_buf_copy(BUF *b, size_t off, void *dst, size_t len)
                    168: {
                    169:        size_t rc;
                    170:
                    171:        if (off > b->cb_len)
1.21      xsa       172:                fatal("cvs_buf_copy failed");
1.1       jfb       173:
                    174:        rc = MIN(len, (b->cb_len - off));
1.16      moritz    175:        memcpy(dst, b->cb_buf + off, rc);
1.1       jfb       176:
                    177:        return (ssize_t)rc;
                    178: }
                    179:
                    180: /*
                    181:  * cvs_buf_set()
                    182:  *
1.16      moritz    183:  * Set the contents of the buffer <b> at offset <off> to the first <len>
                    184:  * bytes of data found at <src>.  If the buffer was not created with
                    185:  * BUF_AUTOEXT, as many bytes as possible will be copied in the buffer.
1.1       jfb       186:  */
1.16      moritz    187: ssize_t
1.1       jfb       188: cvs_buf_set(BUF *b, const void *src, size_t len, size_t off)
                    189: {
1.23      xsa       190:        size_t rlen = 0;
1.1       jfb       191:
                    192:        if (b->cb_size < (len + off)) {
1.20      xsa       193:                if ((b->cb_flags & BUF_AUTOEXT) &&
                    194:                    (cvs_buf_grow(b, len + off - b->cb_size) < 0))
                    195:                        fatal("cvs_buf_set failed");
1.1       jfb       196:                else
                    197:                        rlen = b->cb_size - off;
1.5       deraadt   198:        } else
1.1       jfb       199:                rlen = len;
                    200:
                    201:        memcpy((b->cb_buf + off), src, rlen);
                    202:
                    203:        if (b->cb_len == 0) {
                    204:                b->cb_cur = b->cb_buf + off;
                    205:                b->cb_len = rlen;
                    206:        }
                    207:
1.16      moritz    208:        return (rlen);
1.1       jfb       209: }
                    210:
                    211: /*
                    212:  * cvs_buf_putc()
                    213:  *
                    214:  * Append a single character <c> to the end of the buffer <b>.
1.21      xsa       215:  * Returns 0 on success.
1.1       jfb       216:  */
                    217: int
                    218: cvs_buf_putc(BUF *b, int c)
                    219: {
                    220:        u_char *bp;
                    221:
                    222:        bp = b->cb_cur + b->cb_len;
                    223:        if (bp == (b->cb_buf + b->cb_size)) {
                    224:                /* extend */
                    225:                if (!(b->cb_flags & BUF_AUTOEXT) ||
1.18      xsa       226:                    (cvs_buf_grow(b, (size_t)BUF_INCR) < 0))
1.21      xsa       227:                        fatal("cvs_buf_putc failed");
1.1       jfb       228:
                    229:                /* the buffer might have been moved */
                    230:                bp = b->cb_cur + b->cb_len;
                    231:        }
                    232:        *bp = (u_char)c;
                    233:        b->cb_len++;
                    234:
                    235:        return (0);
1.29    ! niallo    236: }
        !           237:
        !           238: /*
        !           239:  * cvs_buf_getc()
        !           240:  *
        !           241:  * Return u_char at buffer position <pos>.
        !           242:  *
        !           243:  */
        !           244: u_char
        !           245: cvs_buf_getc(BUF *b, u_int pos)
        !           246: {
        !           247:        return (b->cb_cur[pos]);
1.1       jfb       248: }
                    249:
                    250: /*
                    251:  * cvs_buf_append()
                    252:  *
                    253:  * Append <len> bytes of data pointed to by <data> to the buffer <b>.  If the
                    254:  * buffer is too small to accept all data, it will attempt to append as much
                    255:  * data as possible, or if the BUF_AUTOEXT flag is set for the buffer, it
                    256:  * will get resized to an appropriate size to accept all data.
1.27      joris     257:  * Returns the number of bytes successfully appended to the buffer.
1.1       jfb       258:  */
                    259: ssize_t
                    260: cvs_buf_append(BUF *b, const void *data, size_t len)
                    261: {
                    262:        size_t left, rlen;
1.8       jfb       263:        u_char *bp, *bep;
1.1       jfb       264:
                    265:        bp = b->cb_cur + b->cb_len;
                    266:        bep = b->cb_buf + b->cb_size;
                    267:        left = bep - bp;
                    268:        rlen = len;
                    269:
                    270:        if (left < len) {
                    271:                if (b->cb_flags & BUF_AUTOEXT) {
                    272:                        if (cvs_buf_grow(b, len - left) < 0)
1.20      xsa       273:                                fatal("cvs_buf_append failed");
1.1       jfb       274:                        bp = b->cb_cur + b->cb_len;
1.5       deraadt   275:                } else
1.1       jfb       276:                        rlen = bep - bp;
                    277:        }
                    278:
                    279:        memcpy(bp, data, rlen);
                    280:        b->cb_len += rlen;
                    281:
                    282:        return (rlen);
                    283: }
                    284:
                    285: /*
                    286:  * cvs_buf_fappend()
                    287:  *
                    288:  */
                    289: int
                    290: cvs_buf_fappend(BUF *b, const char *fmt, ...)
                    291: {
                    292:        int ret;
                    293:        char *str;
                    294:        va_list vap;
                    295:
                    296:        va_start(vap, fmt);
1.4       pat       297:        ret = vasprintf(&str, fmt, vap);
                    298:        va_end(vap);
1.1       jfb       299:
1.20      xsa       300:        if (ret == -1)
                    301:                fatal("cvs_buf_fappend: failed to format data");
1.1       jfb       302:
1.18      xsa       303:        ret = cvs_buf_append(b, str, (size_t)ret);
1.19      joris     304:        xfree(str);
1.1       jfb       305:        return (ret);
                    306: }
                    307:
                    308: /*
1.16      moritz    309:  * cvs_buf_len()
1.1       jfb       310:  *
                    311:  * Returns the size of the buffer that is being used.
                    312:  */
                    313: size_t
1.16      moritz    314: cvs_buf_len(BUF *b)
1.1       jfb       315: {
                    316:        return (b->cb_len);
                    317: }
                    318:
                    319: /*
                    320:  * cvs_buf_peek()
                    321:  *
                    322:  * Peek at the contents of the buffer <b> at offset <off>.
                    323:  */
1.17      xsa       324: const void *
1.1       jfb       325: cvs_buf_peek(BUF *b, size_t off)
                    326: {
                    327:        if (off >= b->cb_len)
                    328:                return (NULL);
                    329:
                    330:        return (b->cb_buf + off);
                    331: }
                    332:
                    333: /*
1.7       djm       334:  * cvs_buf_write_fd()
1.1       jfb       335:  *
1.7       djm       336:  * Write the contents of the buffer <b> to the specified <fd>
1.1       jfb       337:  */
                    338: int
1.7       djm       339: cvs_buf_write_fd(BUF *b, int fd)
1.1       jfb       340: {
                    341:        u_char *bp;
                    342:        size_t len;
                    343:        ssize_t ret;
                    344:
                    345:        len = b->cb_len;
                    346:        bp = b->cb_cur;
                    347:
                    348:        do {
1.16      moritz    349:                ret = write(fd, bp, len);
1.1       jfb       350:                if (ret == -1) {
1.7       djm       351:                        if (errno == EINTR || errno == EAGAIN)
                    352:                                continue;
1.22      xsa       353:                        return (-1);
1.1       jfb       354:                }
                    355:
                    356:                len -= (size_t)ret;
                    357:                bp += (size_t)ret;
                    358:        } while (len > 0);
                    359:
1.7       djm       360:        return (0);
                    361: }
                    362:
                    363: /*
                    364:  * cvs_buf_write()
                    365:  *
                    366:  * Write the contents of the buffer <b> to the file whose path is given in
                    367:  * <path>.  If the file does not exist, it is created with mode <mode>.
                    368:  */
                    369: int
                    370: cvs_buf_write(BUF *b, const char *path, mode_t mode)
                    371: {
1.22      xsa       372:        int fd;
1.7       djm       373:
1.20      xsa       374:        if ((fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, mode)) == -1)
                    375:                fatal("open: `%s': %s", path, strerror(errno));
1.7       djm       376:
1.22      xsa       377:        if (cvs_buf_write_fd(b, fd) == -1) {
1.7       djm       378:                (void)unlink(path);
1.22      xsa       379:                fatal("cvs_buf_write: cvs_buf_write_fd: `%s'", path);
1.7       djm       380:        }
1.1       jfb       381:        (void)close(fd);
1.24      joris     382:
                    383:        if (chmod(path, mode) < 0)
                    384:                fatal("cvs_buf_write: chmod failed: %s", strerror(errno));
1.1       jfb       385:
1.22      xsa       386:        return (0);
1.1       jfb       387: }
                    388:
1.7       djm       389: /*
                    390:  * cvs_buf_write_stmp()
                    391:  *
1.13      joris     392:  * Write the contents of the buffer <b> to a temporary file whose path is
1.7       djm       393:  * specified using <template> (see mkstemp.3). NB. This function will modify
                    394:  * <template>, as per mkstemp
                    395:  */
                    396: int
                    397: cvs_buf_write_stmp(BUF *b, char *template, mode_t mode)
                    398: {
1.22      xsa       399:        int fd;
1.7       djm       400:
1.20      xsa       401:        if ((fd = mkstemp(template)) == -1)
                    402:                fatal("mkstemp: `%s': %s", template, strerror(errno));
1.7       djm       403:
1.22      xsa       404:        if (cvs_buf_write_fd(b, fd) == -1) {
1.7       djm       405:                (void)unlink(template);
1.22      xsa       406:                fatal("cvs_buf_write_stmp: cvs_buf_write_fd: `%s'", template);
1.7       djm       407:        }
                    408:        (void)close(fd);
                    409:
1.22      xsa       410:        return (0);
1.7       djm       411: }
1.1       jfb       412:
                    413: /*
                    414:  * cvs_buf_grow()
                    415:  *
                    416:  * Grow the buffer <b> by <len> bytes.  The contents are unchanged by this
                    417:  * operation regardless of the result.
                    418:  * Returns the new size on success, or -1 on failure.
                    419:  */
                    420: static ssize_t
                    421: cvs_buf_grow(BUF *b, size_t len)
                    422: {
                    423:        void *tmp;
                    424:        size_t diff;
                    425:
1.8       jfb       426:        diff = b->cb_cur - b->cb_buf;
1.19      joris     427:        tmp = xrealloc(b->cb_buf, b->cb_size + len);
1.1       jfb       428:        b->cb_buf = (u_char *)tmp;
                    429:        b->cb_size += len;
                    430:
                    431:        /* readjust pointers in case the buffer moved in memory */
                    432:        b->cb_cur = b->cb_buf + diff;
                    433:
                    434:        return (ssize_t)b->cb_size;
                    435: }