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

Annotation of src/usr.bin/rcs/buf.c, Revision 1.15

1.15    ! ray         1: /*     $OpenBSD: buf.c,v 1.14 2010/07/23 08:31:19 ray Exp $    */
1.1       joris       2: /*
                      3:  * Copyright (c) 2003 Jean-Francois Brousseau <jfb@openbsd.org>
                      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.12      xsa        27: #include <sys/queue.h>
                     28: #include <sys/stat.h>
                     29:
                     30: #include <err.h>
                     31: #include <errno.h>
                     32: #include <fcntl.h>
                     33: #include <stdarg.h>
                     34: #include <stdio.h>
                     35: #include <string.h>
                     36: #include <unistd.h>
1.1       joris      37:
                     38: #include "buf.h"
                     39: #include "xmalloc.h"
                     40: #include "worklist.h"
                     41:
                     42: #define BUF_INCR       128
                     43:
1.15    ! ray        44: struct buf {
1.1       joris      45:        u_int   cb_flags;
                     46:
1.10      ray        47:        /* buffer handle, buffer size, and data length */
1.1       joris      48:        u_char  *cb_buf;
                     49:        size_t   cb_size;
                     50:        size_t   cb_len;
                     51: };
                     52:
1.10      ray        53: #define SIZE_LEFT(b)   (b->cb_size - b->cb_len)
1.1       joris      54:
1.15    ! ray        55: static void    buf_grow(BUF *, size_t);
1.1       joris      56:
                     57: /*
1.15    ! ray        58:  * buf_alloc()
1.1       joris      59:  *
                     60:  * Create a new buffer structure and return a pointer to it.  This structure
1.15    ! ray        61:  * uses dynamically-allocated memory and must be freed with buf_free(),
1.1       joris      62:  * once the buffer is no longer needed.
                     63:  */
                     64: BUF *
1.15    ! ray        65: buf_alloc(size_t len, u_int flags)
1.1       joris      66: {
                     67:        BUF *b;
                     68:
                     69:        b = xmalloc(sizeof(*b));
                     70:        /* Postpone creation of zero-sized buffers */
                     71:        if (len > 0)
                     72:                b->cb_buf = xcalloc(1, len);
                     73:        else
                     74:                b->cb_buf = NULL;
                     75:
                     76:        b->cb_flags = flags;
                     77:        b->cb_size = len;
                     78:        b->cb_len = 0;
                     79:
                     80:        return (b);
                     81: }
                     82:
                     83: /*
1.15    ! ray        84:  * buf_load()
1.1       joris      85:  *
                     86:  * Open the file specified by <path> and load all of its contents into a
                     87:  * buffer.
1.8       ray        88:  * Returns the loaded buffer on success or NULL on failure.
                     89:  * Sets errno on error.
1.1       joris      90:  */
                     91: BUF *
1.15    ! ray        92: buf_load(const char *path, u_int flags)
1.1       joris      93: {
                     94:        int fd;
                     95:        ssize_t ret;
                     96:        size_t len;
                     97:        u_char *bp;
                     98:        struct stat st;
                     99:        BUF *buf;
                    100:
1.8       ray       101:        buf = NULL;
                    102:
                    103:        if ((fd = open(path, O_RDONLY, 0600)) == -1)
                    104:                goto out;
1.1       joris     105:
                    106:        if (fstat(fd, &st) == -1)
1.8       ray       107:                goto out;
1.1       joris     108:
1.8       ray       109:        if (st.st_size > SIZE_MAX) {
                    110:                errno = EFBIG;
                    111:                goto out;
                    112:        }
1.15    ! ray       113:        buf = buf_alloc(st.st_size, flags);
1.10      ray       114:        for (bp = buf->cb_buf; ; bp += (size_t)ret) {
1.1       joris     115:                len = SIZE_LEFT(buf);
                    116:                ret = read(fd, bp, len);
                    117:                if (ret == -1) {
1.8       ray       118:                        int saved_errno;
                    119:
                    120:                        saved_errno = errno;
1.15    ! ray       121:                        buf_free(buf);
1.8       ray       122:                        buf = NULL;
                    123:                        errno = saved_errno;
                    124:                        goto out;
1.1       joris     125:                } else if (ret == 0)
                    126:                        break;
                    127:
                    128:                buf->cb_len += (size_t)ret;
                    129:        }
                    130:
1.8       ray       131: out:
                    132:        if (fd != -1) {
                    133:                int saved_errno;
                    134:
                    135:                /* We may want to preserve errno here. */
                    136:                saved_errno = errno;
                    137:                (void)close(fd);
                    138:                errno = saved_errno;
                    139:        }
1.1       joris     140:
                    141:        return (buf);
                    142: }
                    143:
                    144: void
1.15    ! ray       145: buf_free(BUF *b)
1.1       joris     146: {
                    147:        if (b->cb_buf != NULL)
                    148:                xfree(b->cb_buf);
                    149:        xfree(b);
                    150: }
                    151:
                    152: /*
1.15    ! ray       153:  * buf_release()
1.1       joris     154:  *
                    155:  * Free the buffer <b>'s structural information but do not free the contents
                    156:  * of the buffer.  Instead, they are returned and should be freed later using
                    157:  * free().
                    158:  */
                    159: void *
1.15    ! ray       160: buf_release(BUF *b)
1.1       joris     161: {
1.4       niallo    162:        void *tmp;
1.1       joris     163:
                    164:        tmp = b->cb_buf;
                    165:        xfree(b);
                    166:        return (tmp);
1.5       niallo    167: }
                    168:
                    169: /*
1.15    ! ray       170:  * buf_get()
1.5       niallo    171:  */
1.11      xsa       172: u_char *
1.15    ! ray       173: buf_get(BUF *b)
1.5       niallo    174: {
                    175:        return (b->cb_buf);
1.1       joris     176: }
                    177:
                    178: /*
1.15    ! ray       179:  * buf_empty()
1.1       joris     180:  *
                    181:  * Empty the contents of the buffer <b> and reset pointers.
                    182:  */
                    183: void
1.15    ! ray       184: buf_empty(BUF *b)
1.1       joris     185: {
                    186:        memset(b->cb_buf, 0, b->cb_size);
                    187:        b->cb_len = 0;
                    188: }
                    189:
                    190: /*
1.15    ! ray       191:  * buf_putc()
1.1       joris     192:  *
                    193:  * Append a single character <c> to the end of the buffer <b>.
                    194:  */
                    195: void
1.15    ! ray       196: buf_putc(BUF *b, int c)
1.1       joris     197: {
                    198:        u_char *bp;
                    199:
1.10      ray       200:        bp = b->cb_buf + b->cb_len;
1.1       joris     201:        if (bp == (b->cb_buf + b->cb_size)) {
                    202:                /* extend */
                    203:                if (b->cb_flags & BUF_AUTOEXT)
1.15    ! ray       204:                        buf_grow(b, (size_t)BUF_INCR);
1.1       joris     205:                else
1.15    ! ray       206:                        errx(1, "buf_putc failed");
1.1       joris     207:
                    208:                /* the buffer might have been moved */
1.10      ray       209:                bp = b->cb_buf + b->cb_len;
1.1       joris     210:        }
                    211:        *bp = (u_char)c;
                    212:        b->cb_len++;
                    213: }
                    214:
                    215: /*
1.15    ! ray       216:  * buf_getc()
1.1       joris     217:  *
                    218:  * Return u_char at buffer position <pos>.
                    219:  *
                    220:  */
                    221: u_char
1.15    ! ray       222: buf_getc(BUF *b, size_t pos)
1.1       joris     223: {
1.10      ray       224:        return (b->cb_buf[pos]);
1.1       joris     225: }
                    226:
                    227: /*
1.15    ! ray       228:  * buf_append()
1.1       joris     229:  *
                    230:  * Append <len> bytes of data pointed to by <data> to the buffer <b>.  If the
                    231:  * buffer is too small to accept all data, it will attempt to append as much
                    232:  * data as possible, or if the BUF_AUTOEXT flag is set for the buffer, it
                    233:  * will get resized to an appropriate size to accept all data.
                    234:  * Returns the number of bytes successfully appended to the buffer.
                    235:  */
1.7       ray       236: size_t
1.15    ! ray       237: buf_append(BUF *b, const void *data, size_t len)
1.1       joris     238: {
                    239:        size_t left, rlen;
                    240:        u_char *bp, *bep;
                    241:
1.10      ray       242:        bp = b->cb_buf + b->cb_len;
1.1       joris     243:        bep = b->cb_buf + b->cb_size;
                    244:        left = bep - bp;
                    245:        rlen = len;
                    246:
                    247:        if (left < len) {
                    248:                if (b->cb_flags & BUF_AUTOEXT) {
1.15    ! ray       249:                        buf_grow(b, len - left);
1.10      ray       250:                        bp = b->cb_buf + b->cb_len;
1.1       joris     251:                } else
                    252:                        rlen = bep - bp;
                    253:        }
                    254:
                    255:        memcpy(bp, data, rlen);
                    256:        b->cb_len += rlen;
                    257:
                    258:        return (rlen);
                    259: }
                    260:
                    261: /*
1.15    ! ray       262:  * buf_fappend()
1.1       joris     263:  *
                    264:  */
1.7       ray       265: size_t
1.15    ! ray       266: buf_fappend(BUF *b, const char *fmt, ...)
1.1       joris     267: {
1.7       ray       268:        size_t ret;
                    269:        int n;
1.1       joris     270:        char *str;
                    271:        va_list vap;
                    272:
                    273:        va_start(vap, fmt);
1.7       ray       274:        n = vasprintf(&str, fmt, vap);
1.1       joris     275:        va_end(vap);
                    276:
1.7       ray       277:        if (n == -1)
1.15    ! ray       278:                errx(1, "buf_fappend: failed to format data");
1.1       joris     279:
1.15    ! ray       280:        ret = buf_append(b, str, n);
1.1       joris     281:        xfree(str);
                    282:        return (ret);
                    283: }
                    284:
                    285: /*
1.15    ! ray       286:  * buf_len()
1.1       joris     287:  *
                    288:  * Returns the size of the buffer that is being used.
                    289:  */
                    290: size_t
1.15    ! ray       291: buf_len(BUF *b)
1.1       joris     292: {
                    293:        return (b->cb_len);
                    294: }
                    295:
                    296: /*
1.15    ! ray       297:  * buf_write_fd()
1.1       joris     298:  *
                    299:  * Write the contents of the buffer <b> to the specified <fd>
                    300:  */
                    301: int
1.15    ! ray       302: buf_write_fd(BUF *b, int fd)
1.1       joris     303: {
                    304:        u_char *bp;
                    305:        size_t len;
                    306:        ssize_t ret;
                    307:
                    308:        len = b->cb_len;
1.10      ray       309:        bp = b->cb_buf;
1.1       joris     310:
                    311:        do {
                    312:                ret = write(fd, bp, len);
                    313:                if (ret == -1) {
                    314:                        if (errno == EINTR || errno == EAGAIN)
                    315:                                continue;
                    316:                        return (-1);
                    317:                }
                    318:
                    319:                len -= (size_t)ret;
                    320:                bp += (size_t)ret;
                    321:        } while (len > 0);
                    322:
                    323:        return (0);
                    324: }
                    325:
                    326: /*
1.15    ! ray       327:  * buf_write()
1.1       joris     328:  *
                    329:  * Write the contents of the buffer <b> to the file whose path is given in
                    330:  * <path>.  If the file does not exist, it is created with mode <mode>.
                    331:  */
                    332: int
1.15    ! ray       333: buf_write(BUF *b, const char *path, mode_t mode)
1.1       joris     334: {
                    335:        int fd;
                    336:  open:
                    337:        if ((fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, mode)) == -1) {
                    338:                if (errno == EACCES && unlink(path) != -1)
                    339:                        goto open;
                    340:                else
1.3       xsa       341:                        err(1, "%s", path);
1.1       joris     342:        }
                    343:
1.15    ! ray       344:        if (buf_write_fd(b, fd) == -1) {
1.1       joris     345:                (void)unlink(path);
1.15    ! ray       346:                errx(1, "buf_write: buf_write_fd: `%s'", path);
1.1       joris     347:        }
                    348:
                    349:        if (fchmod(fd, mode) < 0)
                    350:                warn("permissions not set on file %s", path);
                    351:
                    352:        (void)close(fd);
                    353:
                    354:        return (0);
                    355: }
                    356:
                    357: /*
1.15    ! ray       358:  * buf_write_stmp()
1.1       joris     359:  *
                    360:  * Write the contents of the buffer <b> to a temporary file whose path is
                    361:  * specified using <template> (see mkstemp.3). NB. This function will modify
                    362:  * <template>, as per mkstemp
                    363:  */
                    364: void
1.15    ! ray       365: buf_write_stmp(BUF *b, char *template)
1.1       joris     366: {
                    367:        int fd;
                    368:
                    369:        if ((fd = mkstemp(template)) == -1)
1.3       xsa       370:                err(1, "%s", template);
1.1       joris     371:
1.14      ray       372:        worklist_add(template, &temp_files);
1.1       joris     373:
1.15    ! ray       374:        if (buf_write_fd(b, fd) == -1) {
1.1       joris     375:                (void)unlink(template);
1.15    ! ray       376:                errx(1, "buf_write_stmp: buf_write_fd: `%s'", template);
1.1       joris     377:        }
                    378:
                    379:        (void)close(fd);
                    380: }
                    381:
                    382: /*
1.15    ! ray       383:  * buf_grow()
1.1       joris     384:  *
                    385:  * Grow the buffer <b> by <len> bytes.  The contents are unchanged by this
                    386:  * operation regardless of the result.
                    387:  */
                    388: static void
1.15    ! ray       389: buf_grow(BUF *b, size_t len)
1.1       joris     390: {
1.13      ray       391:        b->cb_buf = xrealloc(b->cb_buf, 1, b->cb_size + len);
1.1       joris     392:        b->cb_size += len;
                    393: }