1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 23*7c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 24*7c478bd9Sstevel@tonic-gate 25*7c478bd9Sstevel@tonic-gate 26*7c478bd9Sstevel@tonic-gate /* 27*7c478bd9Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988 28*7c478bd9Sstevel@tonic-gate * The Regents of the University of California 29*7c478bd9Sstevel@tonic-gate * All Rights Reserved 30*7c478bd9Sstevel@tonic-gate * 31*7c478bd9Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 32*7c478bd9Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 33*7c478bd9Sstevel@tonic-gate * contributors. 34*7c478bd9Sstevel@tonic-gate */ 35*7c478bd9Sstevel@tonic-gate 36*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 37*7c478bd9Sstevel@tonic-gate 38*7c478bd9Sstevel@tonic-gate /* 39*7c478bd9Sstevel@tonic-gate * Copyright (c) 1993-1997, by Sun Microsystems, Inc. 40*7c478bd9Sstevel@tonic-gate * All rights reserved. 41*7c478bd9Sstevel@tonic-gate */ 42*7c478bd9Sstevel@tonic-gate 43*7c478bd9Sstevel@tonic-gate /* 44*7c478bd9Sstevel@tonic-gate * mailx -- a modified version of a University of California at Berkeley 45*7c478bd9Sstevel@tonic-gate * mail program 46*7c478bd9Sstevel@tonic-gate * 47*7c478bd9Sstevel@tonic-gate * Memory allocation routines. 48*7c478bd9Sstevel@tonic-gate * Memory handed out here are reclaimed at the top of the command 49*7c478bd9Sstevel@tonic-gate * loop each time, so they need not be freed. 50*7c478bd9Sstevel@tonic-gate */ 51*7c478bd9Sstevel@tonic-gate 52*7c478bd9Sstevel@tonic-gate #include "rcv.h" 53*7c478bd9Sstevel@tonic-gate #include <locale.h> 54*7c478bd9Sstevel@tonic-gate 55*7c478bd9Sstevel@tonic-gate static void *lastptr; /* addr of last buffer allocated */ 56*7c478bd9Sstevel@tonic-gate static struct strings *lastsp; /* last string space allocated from */ 57*7c478bd9Sstevel@tonic-gate 58*7c478bd9Sstevel@tonic-gate /* 59*7c478bd9Sstevel@tonic-gate * Allocate size more bytes of space and return the address of the 60*7c478bd9Sstevel@tonic-gate * first byte to the caller. An even number of bytes are always 61*7c478bd9Sstevel@tonic-gate * allocated so that the space will always be on a word boundary. 62*7c478bd9Sstevel@tonic-gate * The string spaces are of exponentially increasing size, to satisfy 63*7c478bd9Sstevel@tonic-gate * the occasional user with enormous string size requests. 64*7c478bd9Sstevel@tonic-gate */ 65*7c478bd9Sstevel@tonic-gate 66*7c478bd9Sstevel@tonic-gate void * 67*7c478bd9Sstevel@tonic-gate salloc(unsigned size) 68*7c478bd9Sstevel@tonic-gate { 69*7c478bd9Sstevel@tonic-gate register char *t; 70*7c478bd9Sstevel@tonic-gate register unsigned s; 71*7c478bd9Sstevel@tonic-gate register struct strings *sp; 72*7c478bd9Sstevel@tonic-gate int index; 73*7c478bd9Sstevel@tonic-gate 74*7c478bd9Sstevel@tonic-gate s = size; 75*7c478bd9Sstevel@tonic-gate #if defined(u3b) || defined(sparc) 76*7c478bd9Sstevel@tonic-gate s += 3; /* needs alignment on quad boundary */ 77*7c478bd9Sstevel@tonic-gate s &= ~03; 78*7c478bd9Sstevel@tonic-gate #elif defined(i386) 79*7c478bd9Sstevel@tonic-gate s++; 80*7c478bd9Sstevel@tonic-gate s &= ~01; 81*7c478bd9Sstevel@tonic-gate #else 82*7c478bd9Sstevel@tonic-gate #error Unknown architecture! 83*7c478bd9Sstevel@tonic-gate #endif 84*7c478bd9Sstevel@tonic-gate index = 0; 85*7c478bd9Sstevel@tonic-gate for (sp = &stringdope[0]; sp < &stringdope[NSPACE]; sp++) { 86*7c478bd9Sstevel@tonic-gate if (sp->s_topFree == NOSTR && (STRINGSIZE << index) >= s) 87*7c478bd9Sstevel@tonic-gate break; 88*7c478bd9Sstevel@tonic-gate if (sp->s_nleft >= s) 89*7c478bd9Sstevel@tonic-gate break; 90*7c478bd9Sstevel@tonic-gate index++; 91*7c478bd9Sstevel@tonic-gate } 92*7c478bd9Sstevel@tonic-gate if (sp >= &stringdope[NSPACE]) 93*7c478bd9Sstevel@tonic-gate panic("String too large"); 94*7c478bd9Sstevel@tonic-gate if (sp->s_topFree == NOSTR) { 95*7c478bd9Sstevel@tonic-gate index = sp - &stringdope[0]; 96*7c478bd9Sstevel@tonic-gate sp->s_topFree = (char *) calloc(STRINGSIZE << index, 97*7c478bd9Sstevel@tonic-gate (unsigned) 1); 98*7c478bd9Sstevel@tonic-gate if (sp->s_topFree == NOSTR) { 99*7c478bd9Sstevel@tonic-gate fprintf(stderr, gettext("No room for space %d\n"), 100*7c478bd9Sstevel@tonic-gate index); 101*7c478bd9Sstevel@tonic-gate panic("Internal error"); 102*7c478bd9Sstevel@tonic-gate } 103*7c478bd9Sstevel@tonic-gate sp->s_nextFree = sp->s_topFree; 104*7c478bd9Sstevel@tonic-gate sp->s_nleft = STRINGSIZE << index; 105*7c478bd9Sstevel@tonic-gate } 106*7c478bd9Sstevel@tonic-gate sp->s_nleft -= s; 107*7c478bd9Sstevel@tonic-gate t = sp->s_nextFree; 108*7c478bd9Sstevel@tonic-gate sp->s_nextFree += s; 109*7c478bd9Sstevel@tonic-gate lastptr = t; 110*7c478bd9Sstevel@tonic-gate lastsp = sp; 111*7c478bd9Sstevel@tonic-gate return(t); 112*7c478bd9Sstevel@tonic-gate } 113*7c478bd9Sstevel@tonic-gate 114*7c478bd9Sstevel@tonic-gate /* 115*7c478bd9Sstevel@tonic-gate * Reallocate size bytes of space and return the address of the 116*7c478bd9Sstevel@tonic-gate * first byte to the caller. The old data is copied into the new area. 117*7c478bd9Sstevel@tonic-gate */ 118*7c478bd9Sstevel@tonic-gate 119*7c478bd9Sstevel@tonic-gate void * 120*7c478bd9Sstevel@tonic-gate srealloc(void *optr, unsigned size) 121*7c478bd9Sstevel@tonic-gate { 122*7c478bd9Sstevel@tonic-gate void *nptr; 123*7c478bd9Sstevel@tonic-gate 124*7c478bd9Sstevel@tonic-gate /* if we just want to expand the last allocation, that's easy */ 125*7c478bd9Sstevel@tonic-gate if (optr == lastptr) { 126*7c478bd9Sstevel@tonic-gate register unsigned s, delta; 127*7c478bd9Sstevel@tonic-gate register struct strings *sp = lastsp; 128*7c478bd9Sstevel@tonic-gate 129*7c478bd9Sstevel@tonic-gate s = size; 130*7c478bd9Sstevel@tonic-gate #if defined(u3b) || defined(sparc) 131*7c478bd9Sstevel@tonic-gate s += 3; /* needs alignment on quad boundary */ 132*7c478bd9Sstevel@tonic-gate s &= ~03; 133*7c478bd9Sstevel@tonic-gate #elif defined(i386) 134*7c478bd9Sstevel@tonic-gate s++; 135*7c478bd9Sstevel@tonic-gate s &= ~01; 136*7c478bd9Sstevel@tonic-gate #else 137*7c478bd9Sstevel@tonic-gate #error Unknown architecture! 138*7c478bd9Sstevel@tonic-gate #endif defined(u3b) || defined(sparc) 139*7c478bd9Sstevel@tonic-gate delta = s - (sp->s_nextFree - (char *)optr); 140*7c478bd9Sstevel@tonic-gate if (delta <= sp->s_nleft) { 141*7c478bd9Sstevel@tonic-gate sp->s_nextFree += delta; 142*7c478bd9Sstevel@tonic-gate sp->s_nleft -= delta; 143*7c478bd9Sstevel@tonic-gate return (optr); 144*7c478bd9Sstevel@tonic-gate } 145*7c478bd9Sstevel@tonic-gate } 146*7c478bd9Sstevel@tonic-gate nptr = salloc(size); 147*7c478bd9Sstevel@tonic-gate if (nptr) 148*7c478bd9Sstevel@tonic-gate memcpy(nptr, optr, size); /* XXX - copying too much */ 149*7c478bd9Sstevel@tonic-gate return nptr; 150*7c478bd9Sstevel@tonic-gate } 151*7c478bd9Sstevel@tonic-gate 152*7c478bd9Sstevel@tonic-gate /* 153*7c478bd9Sstevel@tonic-gate * Reset the string area to be empty. 154*7c478bd9Sstevel@tonic-gate * Called to free all strings allocated 155*7c478bd9Sstevel@tonic-gate * since last reset. 156*7c478bd9Sstevel@tonic-gate */ 157*7c478bd9Sstevel@tonic-gate void 158*7c478bd9Sstevel@tonic-gate sreset(void) 159*7c478bd9Sstevel@tonic-gate { 160*7c478bd9Sstevel@tonic-gate register struct strings *sp; 161*7c478bd9Sstevel@tonic-gate register int index; 162*7c478bd9Sstevel@tonic-gate 163*7c478bd9Sstevel@tonic-gate if (noreset) 164*7c478bd9Sstevel@tonic-gate return; 165*7c478bd9Sstevel@tonic-gate minit(); 166*7c478bd9Sstevel@tonic-gate index = 0; 167*7c478bd9Sstevel@tonic-gate for (sp = &stringdope[0]; sp < &stringdope[NSPACE]; sp++) { 168*7c478bd9Sstevel@tonic-gate if (sp->s_topFree == NOSTR) 169*7c478bd9Sstevel@tonic-gate continue; 170*7c478bd9Sstevel@tonic-gate sp->s_nextFree = sp->s_topFree; 171*7c478bd9Sstevel@tonic-gate sp->s_nleft = STRINGSIZE << index; 172*7c478bd9Sstevel@tonic-gate index++; 173*7c478bd9Sstevel@tonic-gate } 174*7c478bd9Sstevel@tonic-gate lastptr = NULL; 175*7c478bd9Sstevel@tonic-gate lastsp = NULL; 176*7c478bd9Sstevel@tonic-gate } 177