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

Annotation of src/usr.bin/mail/strings.c, Revision 1.10

1.10    ! mmcc        1: /*     $OpenBSD: strings.c,v 1.9 2009/10/27 23:59:40 deraadt Exp $     */
1.2       deraadt     2: /*     $NetBSD: strings.c,v 1.5 1996/06/08 19:48:40 christos Exp $     */
                      3:
1.1       deraadt     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.
1.8       millert    16:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    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 *
1.7       millert    52: salloc(int size)
1.1       deraadt    53: {
1.6       millert    54:        char *t;
                     55:        int s;
                     56:        struct strings *sp;
1.1       deraadt    57:        int index;
                     58:
                     59:        s = size;
1.3       millert    60:        s += (sizeof(char *) - 1);
                     61:        s &= ~(sizeof(char *) - 1);
1.1       deraadt    62:        index = 0;
                     63:        for (sp = &stringdope[0]; sp < &stringdope[NSPACE]; sp++) {
1.5       millert    64:                if (sp->s_topFree == NULL && (STRINGSIZE << index) >= s)
1.1       deraadt    65:                        break;
                     66:                if (sp->s_nleft >= s)
                     67:                        break;
                     68:                index++;
                     69:        }
                     70:        if (sp >= &stringdope[NSPACE])
1.6       millert    71:                errx(1, "String too large");
1.5       millert    72:        if (sp->s_topFree == NULL) {
1.1       deraadt    73:                index = sp - &stringdope[0];
1.10    ! mmcc       74:                sp->s_topFree = malloc(STRINGSIZE << index);
1.6       millert    75:                if (sp->s_topFree == NULL)
1.10    ! mmcc       76:                        err(1, "malloc");
1.1       deraadt    77:                sp->s_nextFree = sp->s_topFree;
                     78:                sp->s_nleft = STRINGSIZE << index;
                     79:        }
                     80:        sp->s_nleft -= s;
                     81:        t = sp->s_nextFree;
                     82:        sp->s_nextFree += s;
                     83:        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
1.7       millert    92: sreset(void)
1.1       deraadt    93: {
1.6       millert    94:        struct strings *sp;
                     95:        int index;
1.1       deraadt    96:
                     97:        if (noreset)
                     98:                return;
                     99:        index = 0;
                    100:        for (sp = &stringdope[0]; sp < &stringdope[NSPACE]; sp++) {
1.5       millert   101:                if (sp->s_topFree == NULL)
1.1       deraadt   102:                        continue;
                    103:                sp->s_nextFree = sp->s_topFree;
                    104:                sp->s_nleft = STRINGSIZE << index;
                    105:                index++;
                    106:        }
                    107: }
                    108:
                    109: /*
                    110:  * Make the string area permanent.
                    111:  * Meant to be called in main, after initialization.
                    112:  */
                    113: void
1.7       millert   114: spreserve(void)
1.1       deraadt   115: {
1.6       millert   116:        struct strings *sp;
1.1       deraadt   117:
                    118:        for (sp = &stringdope[0]; sp < &stringdope[NSPACE]; sp++)
1.5       millert   119:                sp->s_topFree = NULL;
1.1       deraadt   120: }