static int nfsnprintf( char *buf, int blen, const char *fmt, ... );
+static int nfvasprintf(char **strp, const char *fmt, va_list ap)
+{
+ int len;
+ char tmp[8192];
+
+ len = vsnprintf(tmp, sizeof(tmp), fmt, ap);
+ if (len < 0)
+ die("Fatal: Out of memory\n");
+ if (len >= sizeof(tmp))
+ die("imap command overflow !\n");
+ *strp = xmemdupz(tmp, len);
+ return len;
+}
static void arc4_init( void );
static unsigned char arc4_getbyte( void );
goto bail;
cur->len = s - p;
s++;
- cur->val = xmalloc( cur->len + 1 );
- memcpy( cur->val, p, cur->len );
- cur->val[cur->len] = 0;
+ cur->val = xmemdupz(p, cur->len);
} else {
/* atom */
p = s;
if (level && *s == ')')
break;
cur->len = s - p;
- if (cur->len == 3 && !memcmp ("NIL", p, 3))
+ if (cur->len == 3 && !memcmp ("NIL", p, 3)) {
cur->val = NIL;
- else {
- cur->val = xmalloc( cur->len + 1 );
- memcpy( cur->val, p, cur->len );
- cur->val[cur->len] = 0;
+ } else {
+ cur->val = xmemdupz(p, cur->len);
}
}
static int
read_message( FILE *f, msg_data_t *msg )
{
- int len, r;
+ struct strbuf buf;
- memset( msg, 0, sizeof *msg );
- len = CHUNKSIZE;
- msg->data = xmalloc( len+1 );
- msg->data[0] = 0;
-
- while(!feof( f )) {
- if (msg->len >= len) {
- void *p;
- len += CHUNKSIZE;
- p = xrealloc(msg->data, len+1);
- if (!p)
- break;
- msg->data = p;
- }
- r = fread( &msg->data[msg->len], 1, len - msg->len, f );
- if (r <= 0)
+ memset(msg, 0, sizeof(*msg));
+ strbuf_init(&buf, 0);
+
+ do {
+ if (strbuf_fread(&buf, CHUNKSIZE, f) <= 0)
break;
- msg->len += r;
- }
- msg->data[msg->len] = 0;
+ } while (!feof(f));
+
+ msg->len = buf.len;
+ msg->data = strbuf_detach(&buf);
return msg->len;
}
if (p)
msg->len = &p[1] - data;
- msg->data = xmalloc( msg->len + 1 );
- if (!msg->data)
- return 0;
-
- memcpy( msg->data, data, msg->len );
- msg->data[ msg->len ] = 0;
-
+ msg->data = xmemdupz(data, msg->len);
*ofs += msg->len;
- return 1;
+ return 1;
}
static imap_server_conf_t server =