void *mh_xmalloc(size_t);
void *mh_xrealloc(void *, size_t);
+void *mh_xcalloc(size_t, size_t);
char *pwd(void);
char *add(const char *, char *);
char *addlist(char *, const char *);
#include <h/addrsbr.h>
#include <h/mf.h>
#include <h/mts.h>
+#include <h/utils.h>
/* High level parsing of addresses:
dftype = LOCALHOST;
}
- mp = (struct mailname *) calloc ((size_t) 1, sizeof(*mp));
+ mp = (struct mailname *) mh_xcalloc ((size_t) 1, sizeof(*mp));
if (mp == NULL) {
if (eresult) {
strncpy (eresult, "insufficient memory to represent address",
/* Add new component to the hash table */
#define NEWCOMP(cm,name) do { \
- cm = ((struct comp *) calloc(1, sizeof (struct comp)));\
+ cm = ((struct comp *) mh_xcalloc (1, sizeof (struct comp)));\
cm->c_name = getcpy(name);\
cm->c_refcount++;\
ncomp++;\
*/
i = strlen(fstring)/2 + 1;
if (i==1) i++;
- next_fp = formatvec = (struct format *)calloc ((size_t) i,
+ next_fp = formatvec = (struct format *)mh_xcalloc ((size_t) i,
sizeof(struct format));
if (next_fp == NULL)
adios (NULL, "unable to allocate format storage");
CERROR("component used as both date and address");
}
cm->c_tws = (struct tws *)
- calloc((size_t) 1, sizeof(*cm->c_tws));
+ mh_xcalloc ((size_t) 1, sizeof(*cm->c_tws));
fp->f_type = preprocess;
PUTCOMP(sp);
cm->c_type |= CT_DATE;
* separator) or the last char (since the matchc would have found it
* if it was a real delim).
*/
- s->pat_map = (char **) calloc (256, sizeof(char *));
+ s->pat_map = (char **) mh_xcalloc (256, sizeof(char *));
for (cp = s->fdelim + 1; cp < s->delimend; cp++ )
s->pat_map[(unsigned char)*cp] = cp;
return memory;
}
+/*
+ * Safely call calloc
+ */
+void *
+mh_xcalloc(size_t nmemb, size_t size)
+{
+ void *memory;
+
+ if (nmemb == 0 || size == 0)
+ adios(NULL, "Tried to calloc 0 bytes");
+
+ if ((memory = calloc(nmemb, size))) {
+ return memory;
+ } else {
+ adios(NULL, "calloc failed");
+ }
+}
+
/*
* Return the present working directory, if the current directory does not
* exist, or is too long, make / the pwd.
#include <h/addrsbr.h>
#include <h/fmt_scan.h>
#include <h/mts.h>
+#include <h/utils.h>
#define NADDRS 100
(q = &pq)->pq_next = NULL;
while ((cp = getname (arg))) {
- if ((p = (struct pqpair *) calloc ((size_t) 1, sizeof(*p))) == NULL)
+ if ((p = (struct pqpair *) mh_xcalloc ((size_t) 1, sizeof(*p))) == NULL)
adios (NULL, "unable to allocate pqpair memory");
if ((mp = getm (cp, NULL, 0, error, sizeof(error))) == NULL) {
p->pq_text = getcpy (cp);
seq_setprev (mp); /* set the previous-sequence */
smsgs = (struct smsg *)
- calloc ((size_t) (MAXFOLDER + 2), sizeof(*smsgs));
+ mh_xcalloc ((size_t) (MAXFOLDER + 2), sizeof(*smsgs));
if (smsgs == NULL)
adios (NULL, "unable to allocate burst storage");
char buffer[BUFSIZ];
register struct drop *cp, *dp, *ep, *pp;
- pp = (struct drop *) calloc ((size_t) (len = MAXFOLDER), sizeof(*dp));
+ pp = (struct drop *) mh_xcalloc ((size_t) (len = MAXFOLDER), sizeof(*dp));
if (pp == NULL) {
if (noisy)
admonish (NULL, "unable to allocate drop storage");
}
msgp = mp->d_id;
- dp = (struct drop *) calloc ((size_t) (msgp + 1), sizeof(*dp));
+ dp = (struct drop *) mh_xcalloc ((size_t) (msgp + 1), sizeof(*dp));
if (dp == NULL) {
close (md);
return 0;
for (i = 0; i < addrs->size; i++) {
(q = &pq)->pq_next = NULL;
while ((cp = getname(addrs->msgs[i]))) {
- if ((p = (struct pqpair *) calloc ((size_t) 1, sizeof(*p))) == NULL)
+ if ((p = (struct pqpair *) mh_xcalloc ((size_t) 1, sizeof(*p))) == NULL)
adios (NULL, "unable to allocate pqpair memory");
if ((mp = getm(cp, NULL, 0, error, sizeof(error))) == NULL) {
p->pq_text = getcpy(cp);
/*
* Allocate space for primary (outside) content
*/
- if ((ct = (CT) calloc (1, sizeof(*ct))) == NULL)
+ if ((ct = (CT) mh_xcalloc (1, sizeof(*ct))) == NULL)
adios (NULL, "out of memory");
/*
ct->c_type = CT_MULTIPART;
ct->c_subtype = MULTI_MIXED;
- if ((m = (struct multipart *) calloc (1, sizeof(*m))) == NULL)
+ if ((m = (struct multipart *) mh_xcalloc (1, sizeof(*m))) == NULL)
adios (NULL, "out of memory");
ct->c_ctparams = (void *) m;
pp = &m->mp_parts;
if (!p)
continue;
- if ((part = (struct part *) calloc (1, sizeof(*part))) == NULL)
+ if ((part = (struct part *) mh_xcalloc (1, sizeof(*part))) == NULL)
adios (NULL, "out of memory");
*pp = part;
pp = &part->mp_next;
adios("reading", "Unable to open %s for", at_entry->filename);
}
- if ((p = (CT) calloc (1, sizeof(*p))) == NULL)
+ if ((p = (CT) mh_xcalloc (1, sizeof(*p))) == NULL)
adios(NULL, "out of memory");
init_decoded_content(p, infile);
setup_attach_content(p, at_entry->filename);
- if ((part = (struct part *) calloc (1, sizeof(*part))) == NULL)
+ if ((part = (struct part *) mh_xcalloc (1, sizeof(*part))) == NULL)
adios (NULL, "out of memory");
*pp = part;
pp = &part->mp_next;
struct part *part;
struct text *t;
- if ((p = (CT) calloc (1, sizeof(*p))) == NULL)
+ if ((p = (CT) mh_xcalloc (1, sizeof(*p))) == NULL)
adios(NULL, "out of memory");
init_decoded_content(p, infile);
p->c_begin = ftell(in);
p->c_end = ftell(in);
- if ((t = (struct text *) calloc (1, sizeof (*t))) == NULL)
+ if ((t = (struct text *) mh_xcalloc (1, sizeof (*t))) == NULL)
adios (NULL, "out of memory");
t->tx_charset = CHARSET_SPECIFIED;
p->c_ctparams = t;
- if ((part = (struct part *) calloc (1, sizeof(*part))) == NULL)
+ if ((part = (struct part *) mh_xcalloc (1, sizeof(*part))) == NULL)
adios (NULL, "out of memory");
*pp = part;
part->mp_part = p;
}
/* allocate basic Content structure */
- if ((ct = (CT) calloc (1, sizeof(*ct))) == NULL)
+ if ((ct = (CT) mh_xcalloc (1, sizeof(*ct))) == NULL)
adios (NULL, "out of memory");
*ctp = ct;
* reference, we need to create another Content structure
* for the message/external-body to wrap it in.
*/
- if ((ct = (CT) calloc (1, sizeof(*ct))) == NULL)
+ if ((ct = (CT) mh_xcalloc (1, sizeof(*ct))) == NULL)
adios (NULL, "out of memory");
init_decoded_content(ct, infilename);
*ctp = ct;
ct->c_type = CT_MESSAGE;
ct->c_subtype = MESSAGE_EXTERNAL;
- if ((e = (struct exbody *) calloc (1, sizeof(*e))) == NULL)
+ if ((e = (struct exbody *) mh_xcalloc (1, sizeof(*e))) == NULL)
adios (NULL, "out of memory");
ct->c_ctparams = (void *) e;
ct->c_type = CT_MULTIPART;
ct->c_subtype = MULTI_DIGEST;
- if ((m = (struct multipart *) calloc (1, sizeof(*m))) == NULL)
+ if ((m = (struct multipart *) mh_xcalloc (1, sizeof(*m))) == NULL)
adios (NULL, "out of memory");
ct->c_ctparams = (void *) m;
pp = &m->mp_parts;
CT p;
CE pe;
- if ((p = (CT) calloc (1, sizeof(*p))) == NULL)
+ if ((p = (CT) mh_xcalloc (1, sizeof(*p))) == NULL)
adios (NULL, "out of memory");
init_decoded_content (p, infilename);
pe = &p->c_cefile;
if (listsw && stat (pe->ce_file, &st) != NOTOK)
p->c_end = (long) st.st_size;
- if ((part = (struct part *) calloc (1, sizeof(*part))) == NULL)
+ if ((part = (struct part *) mh_xcalloc (1, sizeof(*part))) == NULL)
adios (NULL, "out of memory");
*pp = part;
pp = &part->mp_next;
ct->c_type = CT_MULTIPART;
ct->c_subtype = vrsn;
- if ((m = (struct multipart *) calloc (1, sizeof(*m))) == NULL)
+ if ((m = (struct multipart *) mh_xcalloc (1, sizeof(*m))) == NULL)
adios (NULL, "out of memory");
ct->c_ctparams = (void *) m;
if (!p)
continue;
- if ((part = (struct part *) calloc (1, sizeof(*part))) == NULL)
+ if ((part = (struct part *) mh_xcalloc (1, sizeof(*part))) == NULL)
adios (NULL, "out of memory");
*pp = part;
pp = &part->mp_next;
}
}
- if (! (cts = (CT *) calloc ((size_t) 2, sizeof *cts))) {
+ if (! (cts = (CT *) mh_xcalloc ((size_t) 2, sizeof *cts))) {
adios (NULL, "out of memory");
}
ctp = cts;
done (1);
seq_setprev (mp); /* set the previous-sequence */
- if (! (cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof *cts))) {
+ if (! (cts = (CT *) mh_xcalloc ((size_t) (mp->numsel + 1), sizeof *cts))) {
adios (NULL, "out of memory");
}
ctp = cts;
divide_part (CT ct) {
CT new_part;
- if ((new_part = (CT) calloc (1, sizeof *new_part)) == NULL)
+ if ((new_part = (CT) mh_xcalloc (1, sizeof *new_part)) == NULL)
adios (NULL, "out of memory");
/* Just copy over what is needed for decoding. c_vrsn and
struct multipart *m;
const struct str2init *ctinit;
- if ((ct = (CT) calloc (1, sizeof *ct)) == NULL)
+ if ((ct = (CT) mh_xcalloc (1, sizeof *ct)) == NULL)
adios (NULL, "out of memory");
/* Set up the multipart/alternative part. These fields of *ct were
- initialized to 0 by calloc():
+ initialized to 0 by mh_xcalloc():
c_fp, c_unlink, c_begin, c_end,
c_vrsn, c_ctline, c_celine,
c_id, c_descr, c_dispo, c_partno,
p->mp_next->mp_next = NULL;
p->mp_next->mp_part = first_alt;
- if ((m = (struct multipart *) calloc (1, sizeof (struct multipart))) ==
+ if ((m = (struct multipart *) mh_xcalloc (1, sizeof (struct multipart))) ==
NULL)
adios (NULL, "out of memory");
m->mp_start = concat (boundary, "\n", NULL);
* check if message is coming from file
*/
if (file) {
- if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
+ if (!(cts = (CT *) mh_xcalloc ((size_t) 2, sizeof(*cts))))
adios (NULL, "out of memory");
ctp = cts;
done (1);
seq_setprev (mp); /* set the previous-sequence */
- if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
+ if (!(cts = (CT *) mh_xcalloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
adios (NULL, "out of memory");
ctp = cts;
return 1;
}
- args = (struct arglist *) calloc((size_t) 1, sizeof(struct arglist));
+ args = (struct arglist *) mh_xcalloc ((size_t) 1, sizeof(struct arglist));
if (arglist_tail)
arglist_tail->a_next = args;
(q = &pq)->pq_next = NULL;
while ((cp = getname (ap))) {
- if ((p = (struct pqpair *) calloc ((size_t) 1, sizeof(*p))) == NULL)
+ if ((p = (struct pqpair *) mh_xcalloc ((size_t) 1, sizeof(*p))) == NULL)
adios (NULL, "unable to allocate pqpair memory");
else {
if ((mp = getm (cp, NULL, 0, error, sizeof(error))) == NULL) {
{
struct mcomp *c1;
- if ((c1 = (struct mcomp *) calloc ((size_t) 1, sizeof(*c1))) == NULL)
+ if ((c1 = (struct mcomp *) mh_xcalloc ((size_t) 1, sizeof(*c1))) == NULL)
adios (NULL, "unable to allocate comp memory");
else {
c1->c_flags = flags & ~INIT;
* check if message is coming from file
*/
if (file) {
- if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
+ if (!(cts = (CT *) mh_xcalloc ((size_t) 2, sizeof(*cts))))
adios (NULL, "out of memory");
ctp = cts;
done (1);
seq_setprev (mp); /* set the previous-sequence */
- if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
+ if (!(cts = (CT *) mh_xcalloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
adios (NULL, "out of memory");
ctp = cts;
m_getfld_state_t gstate = 0;
/* allocate the content structure */
- if (!(ct = (CT) calloc (1, sizeof(*ct))))
+ if (!(ct = (CT) mh_xcalloc (1, sizeof(*ct))))
adios (NULL, "out of memory");
ct->c_fp = in;
ct->c_subtype = kv->kv_value;
/* allocate text character set structure */
- if ((t = (struct text *) calloc (1, sizeof(*t))) == NULL)
+ if ((t = (struct text *) mh_xcalloc (1, sizeof(*t))) == NULL)
adios (NULL, "out of memory");
ct->c_ctparams = (void *) t;
}
/* allocate primary structure for multipart info */
- if ((m = (struct multipart *) calloc (1, sizeof(*m))) == NULL)
+ if ((m = (struct multipart *) mh_xcalloc (1, sizeof(*m))) == NULL)
adios (NULL, "out of memory");
ct->c_ctparams = (void *) m;
if (strcmp (bufp + 2, m->mp_start))
continue;
next_part:
- if ((part = (struct part *) calloc (1, sizeof(*part))) == NULL)
+ if ((part = (struct part *) mh_xcalloc (1, sizeof(*part))) == NULL)
adios (NULL, "out of memory");
*next = part;
next = &part->mp_next;
PM pm;
struct partial *p;
- if ((p = (struct partial *) calloc (1, sizeof(*p))) == NULL)
+ if ((p = (struct partial *) mh_xcalloc (1, sizeof(*p))) == NULL)
adios (NULL, "out of memory");
ct->c_ctparams = (void *) p;
CT p;
FILE *fp;
- if ((e = (struct exbody *) calloc (1, sizeof(*e))) == NULL)
+ if ((e = (struct exbody *) mh_xcalloc (1, sizeof(*e))) == NULL)
adios (NULL, "out of memory");
ct->c_ctparams = (void *) e;
* check if message is coming from file
*/
if (file) {
- if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
+ if (!(cts = (CT *) mh_xcalloc ((size_t) 2, sizeof(*cts))))
adios (NULL, "out of memory");
ctp = cts;
seq_setprev (mp); /* set the Previous-Sequence */
seq_setunseen (mp, 1); /* unset the Unseen-Sequence */
- if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
+ if (!(cts = (CT *) mh_xcalloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
adios (NULL, "out of memory");
ctp = cts;
* check if message is coming from file
*/
if (file) {
- if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
+ if (!(cts = (CT *) mh_xcalloc ((size_t) 2, sizeof(*cts))))
adios (NULL, "out of memory");
ctp = cts;
done (1);
seq_setprev (mp); /* set the previous-sequence */
- if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
+ if (!(cts = (CT *) mh_xcalloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
adios (NULL, "out of memory");
ctp = cts;
return NOTOK;
}
- if ((base = (CT *) calloc ((size_t) (i + 1), sizeof(*base))) == NULL)
+ if ((base = (CT *) mh_xcalloc ((size_t) (i + 1), sizeof(*base))) == NULL)
adios (NULL, "out of memory");
ctq = base;
{
register struct nexus *p;
- if ((p = (struct nexus *) calloc ((size_t) 1, sizeof *p)) == NULL)
+ if ((p = (struct nexus *) mh_xcalloc ((size_t) 1, sizeof *p)) == NULL)
adios (NULL, "unable to allocate component storage");
p->n_action = action;
* and it's our responsibility to free it.
*/
- nxtbuf = compbuffers = (char **) calloc((size_t) ncomps, sizeof(char *));
+ nxtbuf = compbuffers = (char **) mh_xcalloc ((size_t) ncomps, sizeof(char *));
if (nxtbuf == NULL)
adios (NULL, "unable to allocate component buffers");
- used_buf = (struct comp **) calloc((size_t) (ncomps+1),
+ used_buf = (struct comp **) mh_xcalloc ((size_t) (ncomps+1),
sizeof(struct comp *));
if (used_buf == NULL)
adios (NULL, "unable to allocate component buffer stack");
if (! datecomp->c_text) {
if (datecomp->c_tws == NULL)
datecomp->c_tws = (struct tws *)
- calloc((size_t) 1, sizeof(*datecomp->c_tws));
+ mh_xcalloc ((size_t) 1, sizeof(*datecomp->c_tws));
if (datecomp->c_tws == NULL)
adios (NULL, "unable to allocate tws buffer");
*datecomp->c_tws = *dlocaltime ((time_t *) &st.st_mtime);
* the collection of messages with the same subj
* given a message number.
*/
- il = (struct smsg ***) calloc (mp->hghsel+1, sizeof(*il));
+ il = (struct smsg ***) mh_xcalloc (mp->hghsel+1, sizeof(*il));
if (! il)
adios (NULL, "couldn't allocate msg list");
for (i = 0; i < nmsgs; i++)
twscopy (&tb, dlocaltimenow ());
smsgs = (struct smsg *)
- calloc ((size_t) (mp->hghsel - mp->lowsel + 2),
+ mh_xcalloc ((size_t) (mp->hghsel - mp->lowsel + 2),
sizeof(*smsgs));
if (smsgs == NULL)
adios (NULL, "unable to allocate sort storage");