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

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