xref: /netbsd/usr.bin/mail/strings.c (revision d449716a)
1*d449716aSchristos /*	$NetBSD: strings.c,v 1.18 2010/01/12 14:45:31 christos Exp $	*/
288b833a7Schristos 
361f28255Scgd /*
42cb5542fSderaadt  * Copyright (c) 1980, 1993
52cb5542fSderaadt  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * Redistribution and use in source and binary forms, with or without
861f28255Scgd  * modification, are permitted provided that the following conditions
961f28255Scgd  * are met:
1061f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1261f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1461f28255Scgd  *    documentation and/or other materials provided with the distribution.
1589aaa1bbSagc  * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd  *    may be used to endorse or promote products derived from this software
1761f28255Scgd  *    without specific prior written permission.
1861f28255Scgd  *
1961f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd  * SUCH DAMAGE.
3061f28255Scgd  */
3161f28255Scgd 
327c81c8f3Slukem #include <sys/cdefs.h>
3361f28255Scgd #ifndef lint
3488b833a7Schristos #if 0
3588b833a7Schristos static char sccsid[] = "@(#)strings.c	8.1 (Berkeley) 6/6/93";
3688b833a7Schristos #else
37*d449716aSchristos __RCSID("$NetBSD: strings.c,v 1.18 2010/01/12 14:45:31 christos Exp $");
3888b833a7Schristos #endif
3961f28255Scgd #endif /* not lint */
4061f28255Scgd 
4161f28255Scgd /*
4261f28255Scgd  * Mail -- a mail program
4361f28255Scgd  *
4461f28255Scgd  * String allocation routines.
4561f28255Scgd  * Strings handed out here are reclaimed at the top of the command
4661f28255Scgd  * loop each time, so they need not be freed.
4761f28255Scgd  */
4861f28255Scgd 
4961f28255Scgd #include "rcv.h"
502cb5542fSderaadt #include "extern.h"
5161f28255Scgd 
52f3098750Schristos #define	STRINGSIZE	((unsigned) 128)/* Dynamic allocation units */
53f3098750Schristos 
54f3098750Schristos /*
55f3098750Schristos  * The pointers for the string allocation routines,
56f3098750Schristos  * there are NSPACE independent areas.
57f3098750Schristos  * The first holds STRINGSIZE bytes, the next
58f3098750Schristos  * twice as much, and so on.
59f3098750Schristos  */
60f3098750Schristos #define	NSPACE	25			/* Total number of string spaces */
61f3098750Schristos static struct strings {
62f3098750Schristos 	char	*s_topFree;		/* Beginning of this area */
63f3098750Schristos 	char	*s_nextFree;		/* Next alloctable place here */
64ca13337dSchristos 	size_t	s_nleft;		/* Number of bytes left here */
65f3098750Schristos } stringdope[NSPACE];
66f3098750Schristos 
6761f28255Scgd /*
6861f28255Scgd  * Allocate size more bytes of space and return the address of the
6961f28255Scgd  * first byte to the caller.  An even number of bytes are always
7061f28255Scgd  * allocated so that the space will always be on a word boundary.
7161f28255Scgd  * The string spaces are of exponentially increasing size, to satisfy
7261f28255Scgd  * the occasional user with enormous string size requests.
7361f28255Scgd  */
74f3098750Schristos PUBLIC void *
salloc(size_t size)75ca286310Schristos salloc(size_t size)
7661f28255Scgd {
777c81c8f3Slukem 	char *t;
78f3098750Schristos 	size_t s;
797c81c8f3Slukem 	struct strings *sp;
804e972651Swiz 	int idx;
8161f28255Scgd 
8261f28255Scgd 	s = size;
834a83a3c0Scgd 	s += (sizeof(char *) - 1);
844a83a3c0Scgd 	s &= ~(sizeof(char *) - 1);
854e972651Swiz 	idx = 0;
8661f28255Scgd 	for (sp = &stringdope[0]; sp < &stringdope[NSPACE]; sp++) {
87ab850155Swiz 		if (sp->s_topFree == NULL && (STRINGSIZE << idx) >= s)
8861f28255Scgd 			break;
8961f28255Scgd 		if (sp->s_nleft >= s)
9061f28255Scgd 			break;
914e972651Swiz 		idx++;
9261f28255Scgd 	}
9361f28255Scgd 	if (sp >= &stringdope[NSPACE])
94*d449716aSchristos 		errx(EXIT_FAILURE, "String too large");
95ab850155Swiz 	if (sp->s_topFree == NULL) {
96ca13337dSchristos 		idx = (int)(sp - &stringdope[0]);
974e972651Swiz 		sp->s_topFree = malloc(STRINGSIZE << idx);
98ab850155Swiz 		if (sp->s_topFree == NULL)
99*d449716aSchristos 			errx(EXIT_FAILURE, "No room for space %d", idx);
10061f28255Scgd 		sp->s_nextFree = sp->s_topFree;
1014e972651Swiz 		sp->s_nleft = STRINGSIZE << idx;
10261f28255Scgd 	}
10361f28255Scgd 	sp->s_nleft -= s;
10461f28255Scgd 	t = sp->s_nextFree;
10561f28255Scgd 	sp->s_nextFree += s;
106f3098750Schristos 	return t;
10761f28255Scgd }
10861f28255Scgd 
1098207b28aSchristos /*
1108207b28aSchristos  * Allocate zeroed space for 'number' elments of size 'size'.
1118207b28aSchristos  */
112f3098750Schristos PUBLIC void *
csalloc(size_t number,size_t size)1138207b28aSchristos csalloc(size_t number, size_t size)
1148207b28aSchristos {
1158207b28aSchristos 	void *p;
1168207b28aSchristos 	p = salloc(number * size);
1178207b28aSchristos 	(void)memset(p, 0, number * size);
1188207b28aSchristos 	return p;
1198207b28aSchristos }
1208207b28aSchristos 
12161f28255Scgd /*
12261f28255Scgd  * Reset the string area to be empty.
12361f28255Scgd  * Called to free all strings allocated
12461f28255Scgd  * since last reset.
12561f28255Scgd  */
126f3098750Schristos PUBLIC void
sreset(void)127b127ccccSwiz sreset(void)
12861f28255Scgd {
1297c81c8f3Slukem 	struct strings *sp;
1304e972651Swiz 	int idx;
13161f28255Scgd 
13261f28255Scgd 	if (noreset)
13361f28255Scgd 		return;
1344e972651Swiz 	idx = 0;
13561f28255Scgd 	for (sp = &stringdope[0]; sp < &stringdope[NSPACE]; sp++) {
136ab850155Swiz 		if (sp->s_topFree == NULL)
13761f28255Scgd 			continue;
13861f28255Scgd 		sp->s_nextFree = sp->s_topFree;
1394e972651Swiz 		sp->s_nleft = STRINGSIZE << idx;
1404e972651Swiz 		idx++;
14161f28255Scgd 	}
14261f28255Scgd }
14361f28255Scgd 
14461f28255Scgd /*
14561f28255Scgd  * Make the string area permanent.
14661f28255Scgd  * Meant to be called in main, after initialization.
14761f28255Scgd  */
148f3098750Schristos PUBLIC void
spreserve(void)149b127ccccSwiz spreserve(void)
15061f28255Scgd {
1517c81c8f3Slukem 	struct strings *sp;
15261f28255Scgd 
15361f28255Scgd 	for (sp = &stringdope[0]; sp < &stringdope[NSPACE]; sp++)
154ab850155Swiz 		sp->s_topFree = NULL;
15561f28255Scgd }
156