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

Annotation of src/usr.bin/make/buf.c, Revision 1.26

1.26    ! espie       1: /*     $OpenBSD: buf.c,v 1.25 2012/11/07 14:18:41 espie Exp $  */
1.15      espie       2: /*     $NetBSD: buf.c,v 1.9 1996/12/31 17:53:21 christos Exp $ */
1.1       deraadt     3:
                      4: /*
1.8       espie       5:  * Copyright (c) 1999 Marc Espie.
                      6:  *
1.15      espie       7:  * Extensive code changes for the OpenBSD project.
1.8       espie       8:  *
                      9:  * Redistribution and use in source and binary forms, with or without
                     10:  * modification, are permitted provided that the following conditions
                     11:  * are met:
                     12:  * 1. Redistributions of source code must retain the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer.
                     14:  * 2. Redistributions in binary form must reproduce the above copyright
                     15:  *    notice, this list of conditions and the following disclaimer in the
                     16:  *    documentation and/or other materials provided with the distribution.
                     17:  *
                     18:  * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS
                     19:  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
                     20:  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
                     21:  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OPENBSD
                     22:  * PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
                     23:  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
                     24:  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     25:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     26:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     27:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
                     28:  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     29:  */
                     30: /*
1.1       deraadt    31:  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
                     32:  * Copyright (c) 1988, 1989 by Adam de Boor
                     33:  * Copyright (c) 1989 by Berkeley Softworks
                     34:  * All rights reserved.
                     35:  *
                     36:  * This code is derived from software contributed to Berkeley by
                     37:  * Adam de Boor.
                     38:  *
                     39:  * Redistribution and use in source and binary forms, with or without
                     40:  * modification, are permitted provided that the following conditions
                     41:  * are met:
                     42:  * 1. Redistributions of source code must retain the above copyright
                     43:  *    notice, this list of conditions and the following disclaimer.
                     44:  * 2. Redistributions in binary form must reproduce the above copyright
                     45:  *    notice, this list of conditions and the following disclaimer in the
                     46:  *    documentation and/or other materials provided with the distribution.
1.19      millert    47:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    48:  *    may be used to endorse or promote products derived from this software
                     49:  *    without specific prior written permission.
                     50:  *
                     51:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     52:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     53:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     54:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     55:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     56:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     57:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     58:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     59:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     60:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     61:  * SUCH DAMAGE.
                     62:  */
                     63:
                     64: /*-
                     65:  * buf.c --
1.20      espie      66:  *     Functions for automatically expanded buffers.
1.1       deraadt    67:  */
                     68:
1.16      espie      69: #include <ctype.h>
1.26    ! espie      70: #include <limits.h>
1.16      espie      71: #include <stddef.h>
1.26    ! espie      72: #include <stdlib.h>
1.17      espie      73: #include <string.h>
1.24      espie      74: #include <stdio.h>
                     75: #include <stdarg.h>
1.16      espie      76: #include "config.h"
                     77: #include "defines.h"
                     78: #include "buf.h"
                     79: #include "stats.h"
                     80: #include "memory.h"
1.15      espie      81:
                     82: #ifdef STATS_BUF
                     83: #define DO_STAT_BUF(bp, nb)                                    \
                     84:        STAT_BUFS_EXPANSION++;                  \
                     85:        if ((bp)->endPtr - (bp)->buffer == 1)                   \
                     86:                STAT_WEIRD_INEFFICIENT++;
                     87: #else
                     88: #define DO_STAT_BUF(a, b)
                     89: #endif
                     90:
1.26    ! espie      91: static void
        !            92: fatal_overflow()
        !            93: {
        !            94:        fprintf(stderr, "buffer size overflow\n");
        !            95:        exit(2);
        !            96: }
        !            97:
1.16      espie      98: /* BufExpand(bp, nb)
                     99:  *     Expand buffer bp to hold upto nb additional
1.15      espie     100:  *     chars.  Makes sure there's room for an extra '\0' char at
                    101:  *     the end of the buffer to terminate the string.  */
                    102: #define BufExpand(bp,nb)                               \
1.11      espie     103: do {                                                   \
1.21      espie     104:        size_t   occupied = (bp)->inPtr - (bp)->buffer; \
                    105:        size_t   size = (bp)->endPtr - (bp)->buffer;    \
                    106:        DO_STAT_BUF(bp, nb);                            \
1.11      espie     107:                                                        \
1.21      espie     108:        do {                                            \
1.26    ! espie     109:                if (size <= SIZE_MAX/2) {               \
        !           110:                        size *= 2 ;                     \
        !           111:                } else {                                \
        !           112:                        fatal_overflow();               \
        !           113:                }                                       \
1.21      espie     114:        } while (size - occupied < (nb)+1+BUF_MARGIN);  \
                    115:        (bp)->buffer = (bp)->inPtr = (bp)->endPtr =     \
                    116:                erealloc((bp)->buffer, size);           \
                    117:        (bp)->inPtr += occupied;                        \
                    118:        (bp)->endPtr += size;                           \
1.11      espie     119: } while (0);
1.1       deraadt   120:
1.15      espie     121: #define BUF_DEF_SIZE   256     /* Default buffer size */
1.7       espie     122: #define BUF_MARGIN     256     /* Make sure we are comfortable */
1.1       deraadt   123:
1.16      espie     124: /* the hard case for Buf_AddChar: buffer must be expanded to accommodate
1.9       espie     125:  * one more char.  */
1.1       deraadt   126: void
1.20      espie     127: BufOverflow(Buffer bp)
1.1       deraadt   128: {
1.21      espie     129:        BufExpand(bp, 1);
1.1       deraadt   130: }
1.11      espie     131:
1.15      espie     132:
1.1       deraadt   133: void
1.20      espie     134: Buf_AddChars(Buffer bp, size_t numBytes, const char *bytesPtr)
1.1       deraadt   135: {
                    136:
1.21      espie     137:        if ((size_t)(bp->endPtr - bp->inPtr) < numBytes+1)
                    138:                BufExpand(bp, numBytes);
1.1       deraadt   139:
1.21      espie     140:        memcpy(bp->inPtr, bytesPtr, numBytes);
                    141:        bp->inPtr += numBytes;
1.1       deraadt   142: }
                    143:
1.24      espie     144: void
                    145: Buf_printf(Buffer bp, const char *fmt, ...)
                    146: {
                    147:        va_list va;
                    148:        int n;
                    149:        va_start(va, fmt);
                    150:        n = vsnprintf(bp->inPtr, bp->endPtr - bp->inPtr, fmt, va);
                    151:        va_end(va);
                    152:        if (n > bp->endPtr - bp->inPtr) {
                    153:                va_list vb;
                    154:                BufExpand(bp, n);
                    155:                va_start(vb, fmt);
                    156:                (void)vsnprintf(bp->inPtr, bp->endPtr - bp->inPtr, fmt, vb);
                    157:                va_end(vb);
                    158:        }
                    159:        bp->inPtr += n;
                    160: }
1.15      espie     161:
1.12      espie     162: void
1.20      espie     163: Buf_Init(Buffer bp, size_t size)
1.1       deraadt   164: {
1.15      espie     165: #ifdef STATS_BUF
1.21      espie     166:        STAT_TOTAL_BUFS++;
                    167:        if (size == 0)
                    168:                STAT_DEFAULT_BUFS++;
                    169:        if (size == 1)
                    170:                STAT_WEIRD_BUFS++;
1.15      espie     171: #endif
1.21      espie     172:        if (size == 0)
                    173:                size = BUF_DEF_SIZE;
                    174:        bp->inPtr = bp->endPtr = bp->buffer = emalloc(size);
                    175:        bp->endPtr += size;
1.1       deraadt   176: }