17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bdstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bdstevel@tonic-gate * (the "License").  You may not use this file except in compliance
77c478bdstevel@tonic-gate * with the License.
87c478bdstevel@tonic-gate *
97c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bdstevel@tonic-gate * See the License for the specific language governing permissions
127c478bdstevel@tonic-gate * and limitations under the License.
137c478bdstevel@tonic-gate *
147c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bdstevel@tonic-gate *
207c478bdstevel@tonic-gate * CDDL HEADER END
217c478bdstevel@tonic-gate */
226c83d09robbin
236c83d09robbin/*
246c83d09robbin * Copyright 1997 Sun Microsystems, Inc.  All rights reserved.
256c83d09robbin * Use is subject to license terms.
266c83d09robbin */
276c83d09robbin
287c478bdstevel@tonic-gate/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
297c478bdstevel@tonic-gate/*	  All Rights Reserved  	*/
307c478bdstevel@tonic-gate
317c478bdstevel@tonic-gate
327c478bdstevel@tonic-gate/*
337c478bdstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988
347c478bdstevel@tonic-gate * The Regents of the University of California
357c478bdstevel@tonic-gate * All Rights Reserved
367c478bdstevel@tonic-gate *
377c478bdstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from
387c478bdstevel@tonic-gate * software developed by the University of California, Berkeley, and its
397c478bdstevel@tonic-gate * contributors.
407c478bdstevel@tonic-gate */
417c478bdstevel@tonic-gate
427c478bdstevel@tonic-gate#pragma ident	"%Z%%M%	%I%	%E% SMI"
437c478bdstevel@tonic-gate
447c478bdstevel@tonic-gate/*
457c478bdstevel@tonic-gate * mailx -- a modified version of a University of California at Berkeley
467c478bdstevel@tonic-gate *	mail program
477c478bdstevel@tonic-gate *
487c478bdstevel@tonic-gate * Memory allocation routines.
497c478bdstevel@tonic-gate * Memory handed out here are reclaimed at the top of the command
507c478bdstevel@tonic-gate * loop each time, so they need not be freed.
517c478bdstevel@tonic-gate */
527c478bdstevel@tonic-gate
537c478bdstevel@tonic-gate#include "rcv.h"
547c478bdstevel@tonic-gate#include <locale.h>
557c478bdstevel@tonic-gate
567c478bdstevel@tonic-gatestatic void		*lastptr;	/* addr of last buffer allocated */
577c478bdstevel@tonic-gatestatic struct strings	*lastsp;	/* last string space allocated from */
587c478bdstevel@tonic-gate
597c478bdstevel@tonic-gate/*
607c478bdstevel@tonic-gate * Allocate size more bytes of space and return the address of the
617c478bdstevel@tonic-gate * first byte to the caller.  An even number of bytes are always
627c478bdstevel@tonic-gate * allocated so that the space will always be on a word boundary.
637c478bdstevel@tonic-gate * The string spaces are of exponentially increasing size, to satisfy
647c478bdstevel@tonic-gate * the occasional user with enormous string size requests.
657c478bdstevel@tonic-gate */
667c478bdstevel@tonic-gate
677c478bdstevel@tonic-gatevoid *
687c478bdstevel@tonic-gatesalloc(unsigned size)
697c478bdstevel@tonic-gate{
707c478bdstevel@tonic-gate	register char *t;
717c478bdstevel@tonic-gate	register unsigned s;
727c478bdstevel@tonic-gate	register struct strings *sp;
737c478bdstevel@tonic-gate	int index;
747c478bdstevel@tonic-gate
757c478bdstevel@tonic-gate	s = size;
767c478bdstevel@tonic-gate#if defined(u3b) || defined(sparc)
777c478bdstevel@tonic-gate	s += 3;		/* needs alignment on quad boundary */
787c478bdstevel@tonic-gate	s &= ~03;
797c478bdstevel@tonic-gate#elif defined(i386)
807c478bdstevel@tonic-gate	s++;
817c478bdstevel@tonic-gate	s &= ~01;
827c478bdstevel@tonic-gate#else
837c478bdstevel@tonic-gate#error Unknown architecture!
847c478bdstevel@tonic-gate#endif
857c478bdstevel@tonic-gate	index = 0;
867c478bdstevel@tonic-gate	for (sp = &stringdope[0]; sp < &stringdope[NSPACE]; sp++) {
877c478bdstevel@tonic-gate		if (sp->s_topFree == NOSTR && (STRINGSIZE << index) >= s)
887c478bdstevel@tonic-gate			break;
897c478bdstevel@tonic-gate		if (sp->s_nleft >= s)
907c478bdstevel@tonic-gate			break;
917c478bdstevel@tonic-gate		index++;
927c478bdstevel@tonic-gate	}
937c478bdstevel@tonic-gate	if (sp >= &stringdope[NSPACE])
947c478bdstevel@tonic-gate		panic("String too large");
957c478bdstevel@tonic-gate	if (sp->s_topFree == NOSTR) {
967c478bdstevel@tonic-gate		index = sp - &stringdope[0];
977c478bdstevel@tonic-gate		sp->s_topFree = (char *) calloc(STRINGSIZE << index,
987c478bdstevel@tonic-gate		    (unsigned) 1);
997c478bdstevel@tonic-gate		if (sp->s_topFree == NOSTR) {
1007c478bdstevel@tonic-gate			fprintf(stderr, gettext("No room for space %d\n"),
1017c478bdstevel@tonic-gate			    index);
1027c478bdstevel@tonic-gate			panic("Internal error");
1037c478bdstevel@tonic-gate		}
1047c478bdstevel@tonic-gate		sp->s_nextFree = sp->s_topFree;
1057c478bdstevel@tonic-gate		sp->s_nleft = STRINGSIZE << index;
1067c478bdstevel@tonic-gate	}
1077c478bdstevel@tonic-gate	sp->s_nleft -= s;
1087c478bdstevel@tonic-gate	t = sp->s_nextFree;
1097c478bdstevel@tonic-gate	sp->s_nextFree += s;
1107c478bdstevel@tonic-gate	lastptr = t;
1117c478bdstevel@tonic-gate	lastsp = sp;
1127c478bdstevel@tonic-gate	return(t);
1137c478bdstevel@tonic-gate}
1147c478bdstevel@tonic-gate
1157c478bdstevel@tonic-gate/*
1167c478bdstevel@tonic-gate * Reallocate size bytes of space and return the address of the
1177c478bdstevel@tonic-gate * first byte to the caller.  The old data is copied into the new area.
1187c478bdstevel@tonic-gate */
1197c478bdstevel@tonic-gate
1207c478bdstevel@tonic-gatevoid *
1217c478bdstevel@tonic-gatesrealloc(void *optr, unsigned size)
1227c478bdstevel@tonic-gate{
1237c478bdstevel@tonic-gate	void *nptr;
1247c478bdstevel@tonic-gate
1257c478bdstevel@tonic-gate	/* if we just want to expand the last allocation, that's easy */
1267c478bdstevel@tonic-gate	if (optr == lastptr) {
1277c478bdstevel@tonic-gate		register unsigned s, delta;
1287c478bdstevel@tonic-gate		register struct strings *sp = lastsp;
1297c478bdstevel@tonic-gate
1307c478bdstevel@tonic-gate		s = size;
1317c478bdstevel@tonic-gate#if defined(u3b) || defined(sparc)
1327c478bdstevel@tonic-gate		s += 3;		/* needs alignment on quad boundary */
1337c478bdstevel@tonic-gate		s &= ~03;
1347c478bdstevel@tonic-gate#elif defined(i386)
1357c478bdstevel@tonic-gate		s++;
1367c478bdstevel@tonic-gate		s &= ~01;
1377c478bdstevel@tonic-gate#else
1387c478bdstevel@tonic-gate#error Unknown architecture!
1396c83d09robbin#endif /* defined(u3b) || defined(sparc) */
1407c478bdstevel@tonic-gate		delta = s - (sp->s_nextFree - (char *)optr);
1417c478bdstevel@tonic-gate		if (delta <= sp->s_nleft) {
1427c478bdstevel@tonic-gate			sp->s_nextFree += delta;
1437c478bdstevel@tonic-gate			sp->s_nleft -= delta;
1447c478bdstevel@tonic-gate			return (optr);
1457c478bdstevel@tonic-gate		}
1467c478bdstevel@tonic-gate	}
1477c478bdstevel@tonic-gate	nptr = salloc(size);
1487c478bdstevel@tonic-gate	if (nptr)
1497c478bdstevel@tonic-gate		memcpy(nptr, optr, size);	/* XXX - copying too much */
1507c478bdstevel@tonic-gate	return nptr;
1517c478bdstevel@tonic-gate}
1527c478bdstevel@tonic-gate
1537c478bdstevel@tonic-gate/*
1547c478bdstevel@tonic-gate * Reset the string area to be empty.
1557c478bdstevel@tonic-gate * Called to free all strings allocated
1567c478bdstevel@tonic-gate * since last reset.
1577c478bdstevel@tonic-gate */
1587c478bdstevel@tonic-gatevoid
1597c478bdstevel@tonic-gatesreset(void)
1607c478bdstevel@tonic-gate{
1617c478bdstevel@tonic-gate	register struct strings *sp;
1627c478bdstevel@tonic-gate	register int index;
1637c478bdstevel@tonic-gate
1647c478bdstevel@tonic-gate	if (noreset)
1657c478bdstevel@tonic-gate		return;
1667c478bdstevel@tonic-gate	minit();
1677c478bdstevel@tonic-gate	index = 0;
1687c478bdstevel@tonic-gate	for (sp = &stringdope[0]; sp < &stringdope[NSPACE]; sp++) {
1697c478bdstevel@tonic-gate		if (sp->s_topFree == NOSTR)
1707c478bdstevel@tonic-gate			continue;
1717c478bdstevel@tonic-gate		sp->s_nextFree = sp->s_topFree;
1727c478bdstevel@tonic-gate		sp->s_nleft = STRINGSIZE << index;
1737c478bdstevel@tonic-gate		index++;
1747c478bdstevel@tonic-gate	}
1757c478bdstevel@tonic-gate	lastptr = NULL;
1767c478bdstevel@tonic-gate	lastsp = NULL;
1777c478bdstevel@tonic-gate}
178