1
0
Files
irix-657m-src/eoe/cmd/mailx/strings.c
2022-09-29 17:59:04 +03:00

129 lines
2.8 KiB
C

/*
* Copyright (c) 1980 Regents of the University of California.
* All rights reserved. The Berkeley software License Agreement
* specifies the terms and conditions for redistribution.
*/
#ifndef lint
static char *sccsid = "@(#)strings.c 5.2 (Berkeley) 6/21/85";
#endif /* !lint */
/*
* Mail -- a mail program
*
* String allocation routines.
* Strings handed out here are reclaimed at the top of the command
* loop each time, so they need not be freed.
*/
#include "glob.h"
/*
* The pointers for the string allocation routines,
* there are NSPACE independent areas.
* The first holds STRINGSIZE bytes, the next
* twice as much, and so on.
*/
#define NSPACE 25 /* Total number of string spaces */
struct strings {
char *s_topFree; /* Beginning of this area */
char *s_nextFree; /* Next alloctable place here */
unsigned s_nleft; /* Number of bytes left here */
} stringdope[NSPACE];
/*
* Allocate size more bytes of space and return the address of the
* first byte to the caller. An even number of bytes are always
* allocated so that the space will always be on a word boundary.
* The string spaces are of exponentially increasing size, to satisfy
* the occasional user with enormous string size requests.
*/
char *
salloc(size)
{
register char *t;
register int s, index;
register struct strings *sp;
s = size;
#if defined(SVR3) || defined(_SVR4_SOURCE)
s += (sizeof(long)-1);
s &= ~(sizeof(long)-1);
#else
s++;
s &= ~01;
#endif
index = 0;
for (sp = &stringdope[0]; sp < &stringdope[NSPACE]; sp++) {
if (sp->s_topFree == NOSTR && (STRINGSIZE << index) >= s)
break;
if (sp->s_nleft >= s)
break;
index++;
}
if (sp >= &stringdope[NSPACE])
panic("String too large");
if (sp->s_topFree == NOSTR) {
index = sp - &stringdope[0];
sp->s_topFree = (char *) calloc((size_t)STRINGSIZE << index,
(size_t) 1);
if (sp->s_topFree == NOSTR) {
fprintf(stderr, "No room for space %d\n", index);
panic("Internal error");
}
sp->s_nextFree = sp->s_topFree;
sp->s_nleft = STRINGSIZE << index;
}
sp->s_nleft -= s;
t = sp->s_nextFree;
sp->s_nextFree += s;
return(t);
}
/*
* Reset the string area to be empty.
* Called to free all strings allocated
* since last reset.
*/
sreset()
{
register struct strings *sp;
register int index;
if (noreset)
return;
index = 0;
for (sp = &stringdope[0]; sp < &stringdope[NSPACE]; sp++) {
if (sp->s_topFree == NOSTR)
continue;
sp->s_nextFree = sp->s_topFree;
sp->s_nleft = STRINGSIZE << index;
index++;
}
}
/*
* Return a pointer to a dynamic copy of the argument.
*/
char *
savestr(str)
char *str;
{
register char *cp, *cp2, *top;
for (cp = str; *cp; cp++)
;
top = salloc(cp-str + 1);
if (top == NOSTR)
return(NOSTR);
for (cp = str, cp2 = top; *cp; cp++)
*cp2++ = *cp;
*cp2 = 0;
return(top);
}