1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
/*
**
** Copyright (C) 1993 Swedish University Network (SUNET)
**
**
** This program is developed by UDAC, Uppsala University by commission
** of the Swedish University Network (SUNET).
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITTNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
**
**
** Martin.Wendel@its.uu.se
** Torbjorn.Wictorin@its.uu.se
**
** ITS
** P.O. Box 887
** S-751 08 Uppsala
** Sweden
**
*/
#include <signal.h>
#include <stdlib.h>
#include "emil.h"
#define MAXSZ 16*1024 /* Maximum amount to allocate */
#define MULTF 2 /* Increase factor */
#ifdef SIGDANGER
extern int psdanger(int);
#endif
long memall = 0;
extern int in_fd;
static void * Zalloc(void * old, off_t prevsize, off_t newsize)
{
/* Allocate memory */
void * Z;
memall = memall + newsize - prevsize;
#ifdef DEBUG
alloc_total += newsize - prevsize;
if (edebug)
{
fprintf(stderr, "* Allocated %lu, new total: %lu\n", newsize - prevsize, alloc_total);
fflush(stderr);
}
#endif
if (old == NULL)
{
assert (prevsize == 0);
Z = malloc((size_t)newsize);
}
else {
assert (prevsize != 0);
assert (prevsize < newsize);
Z = realloc(old,(size_t)newsize);
}
if (Z == NULL) {
logger(LOG_ERR,"load_data cannot allocate memory");
closelog();
fprintf(stderr, "Emil: load_data: cannot allocate memory (using %lu bytes)\n", memall);
fflush(stderr);
exit(EX_OSERR);
}
memset(((char *) Z) + prevsize, 0, newsize - prevsize);
return Z;
}
struct data *
load_data()
{
struct data *out;
long chunk;
#ifdef SIGDANGER
long border;
#endif
char * tmp;
int l;
int left;
#ifdef DEBUG
if (edebug)
fprintf(stderr, "* Load data\n");
#endif
/* Allocate an output struct */
out = (struct data *) Yalloc(sizeof(struct data));
/* And an initial output buffer */
chunk = pz;
left = chunk;
out->contents = (char *) Zalloc(NULL,0,chunk);
out->size = chunk;
/* Read all on standard input and reallocate buffer if required */
while ((l=read(in_fd, (out->contents + out->end), chunk - 1)) > 0) {
out->end += l;
left -= l;
if (left < chunk)
{
chunk *= MULTF;
if (chunk > MAXSZ) chunk = MAXSZ;
#ifdef SIGDANGER
border = (psdanger(SIGDANGER) * pz) / 2;
if (chunk > border) chunk = border;
#endif
#ifdef DEBUG
if (edebug)
fprintf(stderr, "read %d characters\n", l);
if (edebug)
fprintf(stderr,"load_data: realloc %lud to %lud\n",
out->size,chunk + out->size);
#endif
out->contents =
(char *)Zalloc(out->contents,out->size,out->size+chunk);
out->size += chunk;
left += chunk;
}
}
out->contents[out->end] = 0;
/* Count number of lines */
for ( tmp = out->contents, out->lend = 0;
(tmp= index(tmp,'\n')) != NULL;
tmp++) out->lend++;
return(out);
}
int
append_data(struct data *d, char *c, int len, int busize)
{
if (d->size == 0)
{
if ((d->contents = Zalloc(NULL, 0, busize)) == NULL)
return(NOK);
d->size = busize;
}
while ((len + d->end + 2) > d->size) /* TW 941220 */
{
if ((d->contents = Zalloc(d->contents,d->size,d->size+busize)) == NULL)
return(NOK);
d->size += busize;
}
/* bcopy(c, (d->contents + d->end), len); */
memcpy((d->contents + d->end), c, (off_t) len);
d->end += len;
d->bodyend += len;
*(d->contents + d->end) = '\0';
return(OK);
}
int
append_char(struct data *d, char c, int busize)
{
if (d->size == 0)
{
if ((d->contents = Zalloc(NULL, 0, busize)) == NULL)
return(NOK);
d->size = busize;
}
while ((3 + d->end) > d->size)
{
if ((d->contents = Zalloc(d->contents,d->size,d->size+busize)) == NULL)
return(NOK);
d->size += busize;
}
*(d->contents + d->end) = c;
d->end += 1;
d->bodyend += 1;
*(d->contents + d->end) = '\0';
return(OK);
}
|