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