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

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