[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.26

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