acme9k/mail/util.c

91 lines
1.5 KiB
C
Raw Normal View History

2019-11-14 23:15:48 +00:00
#include <u.h>
#include <libc.h>
#include <bio.h>
#include <thread.h>
#include <plumb.h>
#include <9pclient.h>
#include "dat.h"
void* emalloc(uint n) {
void* p;
2019-11-14 23:15:48 +00:00
p = malloc(n);
if (p == nil)
error("can't malloc: %r");
memset(p, 0, n);
setmalloctag(p, getcallerpc(&n));
return p;
2019-11-14 23:15:48 +00:00
}
void* erealloc(void* p, uint n) {
p = realloc(p, n);
if (p == nil)
error("can't realloc: %r");
setmalloctag(p, getcallerpc(&n));
return p;
2019-11-14 23:15:48 +00:00
}
char* estrdup(char* s) {
char* t;
2019-11-14 23:15:48 +00:00
t = emalloc(strlen(s) + 1);
strcpy(t, s);
return t;
2019-11-14 23:15:48 +00:00
}
char* estrstrdup(char* s, char* t) {
char* u;
2019-11-14 23:15:48 +00:00
u = emalloc(strlen(s) + strlen(t) + 1);
strcpy(u, s);
strcat(u, t);
return u;
2019-11-14 23:15:48 +00:00
}
char* eappend(char* s, char* sep, char* t) {
char* u;
2019-11-14 23:15:48 +00:00
if (t == nil)
u = estrstrdup(s, sep);
else {
u = emalloc(strlen(s) + strlen(sep) + strlen(t) + 1);
strcpy(u, s);
strcat(u, sep);
strcat(u, t);
}
free(s);
return u;
2019-11-14 23:15:48 +00:00
}
char* egrow(char* s, char* sep, char* t) {
s = eappend(s, sep, t);
free(t);
return s;
2019-11-14 23:15:48 +00:00
}
void error(char* fmt, ...) {
Fmt f;
char buf[64];
va_list arg;
2019-11-14 23:15:48 +00:00
fmtfdinit(&f, 2, buf, sizeof buf);
fmtprint(&f, "Mail: ");
va_start(arg, fmt);
fmtvprint(&f, fmt, arg);
va_end(arg);
fmtprint(&f, "\n");
fmtfdflush(&f);
threadexitsall(fmt);
2019-11-14 23:15:48 +00:00
}
void ctlprint(CFid* fd, char* fmt, ...) {
int n;
va_list arg;
2019-11-14 23:15:48 +00:00
va_start(arg, fmt);
n = fsvprint(fd, fmt, arg);
va_end(arg);
if (n <= 0)
error("control file write error: %r");
2019-11-14 23:15:48 +00:00
}