1  | 
     | 
     | 
    /*	$OpenBSD: strings.c,v 1.10 2015/10/16 17:56:07 mmcc Exp $	*/  | 
    
    
    2  | 
     | 
     | 
    /*	$NetBSD: strings.c,v 1.5 1996/06/08 19:48:40 christos Exp $	*/  | 
    
    
    3  | 
     | 
     | 
     | 
    
    
    4  | 
     | 
     | 
    /*  | 
    
    
    5  | 
     | 
     | 
     * Copyright (c) 1980, 1993  | 
    
    
    6  | 
     | 
     | 
     *	The Regents of the University of California.  All rights reserved.  | 
    
    
    7  | 
     | 
     | 
     *  | 
    
    
    8  | 
     | 
     | 
     * Redistribution and use in source and binary forms, with or without  | 
    
    
    9  | 
     | 
     | 
     * modification, are permitted provided that the following conditions  | 
    
    
    10  | 
     | 
     | 
     * are met:  | 
    
    
    11  | 
     | 
     | 
     * 1. Redistributions of source code must retain the above copyright  | 
    
    
    12  | 
     | 
     | 
     *    notice, this list of conditions and the following disclaimer.  | 
    
    
    13  | 
     | 
     | 
     * 2. Redistributions in binary form must reproduce the above copyright  | 
    
    
    14  | 
     | 
     | 
     *    notice, this list of conditions and the following disclaimer in the  | 
    
    
    15  | 
     | 
     | 
     *    documentation and/or other materials provided with the distribution.  | 
    
    
    16  | 
     | 
     | 
     * 3. Neither the name of the University nor the names of its contributors  | 
    
    
    17  | 
     | 
     | 
     *    may be used to endorse or promote products derived from this software  | 
    
    
    18  | 
     | 
     | 
     *    without specific prior written permission.  | 
    
    
    19  | 
     | 
     | 
     *  | 
    
    
    20  | 
     | 
     | 
     * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND  | 
    
    
    21  | 
     | 
     | 
     * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE  | 
    
    
    22  | 
     | 
     | 
     * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE  | 
    
    
    23  | 
     | 
     | 
     * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE  | 
    
    
    24  | 
     | 
     | 
     * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL  | 
    
    
    25  | 
     | 
     | 
     * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS  | 
    
    
    26  | 
     | 
     | 
     * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)  | 
    
    
    27  | 
     | 
     | 
     * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT  | 
    
    
    28  | 
     | 
     | 
     * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY  | 
    
    
    29  | 
     | 
     | 
     * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF  | 
    
    
    30  | 
     | 
     | 
     * SUCH DAMAGE.  | 
    
    
    31  | 
     | 
     | 
     */  | 
    
    
    32  | 
     | 
     | 
     | 
    
    
    33  | 
     | 
     | 
    /*  | 
    
    
    34  | 
     | 
     | 
     * Mail -- a mail program  | 
    
    
    35  | 
     | 
     | 
     *  | 
    
    
    36  | 
     | 
     | 
     * String allocation routines.  | 
    
    
    37  | 
     | 
     | 
     * Strings handed out here are reclaimed at the top of the command  | 
    
    
    38  | 
     | 
     | 
     * loop each time, so they need not be freed.  | 
    
    
    39  | 
     | 
     | 
     */  | 
    
    
    40  | 
     | 
     | 
     | 
    
    
    41  | 
     | 
     | 
    #include "rcv.h"  | 
    
    
    42  | 
     | 
     | 
    #include "extern.h"  | 
    
    
    43  | 
     | 
     | 
     | 
    
    
    44  | 
     | 
     | 
    /*  | 
    
    
    45  | 
     | 
     | 
     * Allocate size more bytes of space and return the address of the  | 
    
    
    46  | 
     | 
     | 
     * first byte to the caller.  An even number of bytes are always  | 
    
    
    47  | 
     | 
     | 
     * allocated so that the space will always be on a word boundary.  | 
    
    
    48  | 
     | 
     | 
     * The string spaces are of exponentially increasing size, to satisfy  | 
    
    
    49  | 
     | 
     | 
     * the occasional user with enormous string size requests.  | 
    
    
    50  | 
     | 
     | 
     */  | 
    
    
    51  | 
     | 
     | 
    char *  | 
    
    
    52  | 
     | 
     | 
    salloc(int size)  | 
    
    
    53  | 
     | 
     | 
    { | 
    
    
    54  | 
     | 
     | 
    	char *t;  | 
    
    
    55  | 
     | 
     | 
    	int s;  | 
    
    
    56  | 
     | 
     | 
    	struct strings *sp;  | 
    
    
    57  | 
     | 
     | 
    	int index;  | 
    
    
    58  | 
     | 
     | 
     | 
    
    
    59  | 
     | 
     | 
    	s = size;  | 
    
    
    60  | 
     | 
    76  | 
    	s += (sizeof(char *) - 1);  | 
    
    
    61  | 
     | 
    38  | 
    	s &= ~(sizeof(char *) - 1);  | 
    
    
    62  | 
     | 
     | 
    	index = 0;  | 
    
    
    63  | 
    ✓✗ | 
    76  | 
    	for (sp = &stringdope[0]; sp < &stringdope[NSPACE]; sp++) { | 
    
    
    64  | 
    ✓✓✗✓
  | 
    42  | 
    		if (sp->s_topFree == NULL && (STRINGSIZE << index) >= s)  | 
    
    
    65  | 
     | 
     | 
    			break;  | 
    
    
    66  | 
    ✗✓ | 
    34  | 
    		if (sp->s_nleft >= s)  | 
    
    
    67  | 
     | 
     | 
    			break;  | 
    
    
    68  | 
     | 
     | 
    		index++;  | 
    
    
    69  | 
     | 
     | 
    	}  | 
    
    
    70  | 
    ✗✓ | 
    38  | 
    	if (sp >= &stringdope[NSPACE])  | 
    
    
    71  | 
     | 
     | 
    		errx(1, "String too large");  | 
    
    
    72  | 
    ✓✓ | 
    38  | 
    	if (sp->s_topFree == NULL) { | 
    
    
    73  | 
     | 
    4  | 
    		index = sp - &stringdope[0];  | 
    
    
    74  | 
     | 
    4  | 
    		sp->s_topFree = malloc(STRINGSIZE << index);  | 
    
    
    75  | 
    ✗✓ | 
    4  | 
    		if (sp->s_topFree == NULL)  | 
    
    
    76  | 
     | 
     | 
    			err(1, "malloc");  | 
    
    
    77  | 
     | 
    4  | 
    		sp->s_nextFree = sp->s_topFree;  | 
    
    
    78  | 
     | 
    4  | 
    		sp->s_nleft = STRINGSIZE << index;  | 
    
    
    79  | 
     | 
    4  | 
    	}  | 
    
    
    80  | 
     | 
    38  | 
    	sp->s_nleft -= s;  | 
    
    
    81  | 
     | 
    38  | 
    	t = sp->s_nextFree;  | 
    
    
    82  | 
     | 
    38  | 
    	sp->s_nextFree += s;  | 
    
    
    83  | 
     | 
    38  | 
    	return(t);  | 
    
    
    84  | 
     | 
     | 
    }  | 
    
    
    85  | 
     | 
     | 
     | 
    
    
    86  | 
     | 
     | 
    /*  | 
    
    
    87  | 
     | 
     | 
     * Reset the string area to be empty.  | 
    
    
    88  | 
     | 
     | 
     * Called to free all strings allocated  | 
    
    
    89  | 
     | 
     | 
     * since last reset.  | 
    
    
    90  | 
     | 
     | 
     */  | 
    
    
    91  | 
     | 
     | 
    void  | 
    
    
    92  | 
     | 
     | 
    sreset(void)  | 
    
    
    93  | 
     | 
     | 
    { | 
    
    
    94  | 
     | 
     | 
    	struct strings *sp;  | 
    
    
    95  | 
     | 
     | 
    	int index;  | 
    
    
    96  | 
     | 
     | 
     | 
    
    
    97  | 
    ✗✓ | 
    12  | 
    	if (noreset)  | 
    
    
    98  | 
     | 
     | 
    		return;  | 
    
    
    99  | 
     | 
     | 
    	index = 0;  | 
    
    
    100  | 
    ✓✓ | 
    312  | 
    	for (sp = &stringdope[0]; sp < &stringdope[NSPACE]; sp++) { | 
    
    
    101  | 
    ✓✓ | 
    150  | 
    		if (sp->s_topFree == NULL)  | 
    
    
    102  | 
     | 
     | 
    			continue;  | 
    
    
    103  | 
     | 
    4  | 
    		sp->s_nextFree = sp->s_topFree;  | 
    
    
    104  | 
     | 
    4  | 
    		sp->s_nleft = STRINGSIZE << index;  | 
    
    
    105  | 
     | 
    4  | 
    		index++;  | 
    
    
    106  | 
     | 
    4  | 
    	}  | 
    
    
    107  | 
     | 
    12  | 
    }  | 
    
    
    108  | 
     | 
     | 
     | 
    
    
    109  | 
     | 
     | 
    /*  | 
    
    
    110  | 
     | 
     | 
     * Make the string area permanent.  | 
    
    
    111  | 
     | 
     | 
     * Meant to be called in main, after initialization.  | 
    
    
    112  | 
     | 
     | 
     */  | 
    
    
    113  | 
     | 
     | 
    void  | 
    
    
    114  | 
     | 
     | 
    spreserve(void)  | 
    
    
    115  | 
     | 
     | 
    { | 
    
    
    116  | 
     | 
     | 
    	struct strings *sp;  | 
    
    
    117  | 
     | 
     | 
     | 
    
    
    118  | 
    ✓✓ | 
    106  | 
    	for (sp = &stringdope[0]; sp < &stringdope[NSPACE]; sp++)  | 
    
    
    119  | 
     | 
    50  | 
    		sp->s_topFree = NULL;  | 
    
    
    120  | 
     | 
    2  | 
    }  |