master ohmu/acme2k / src / cmd / acme / mail / util.c
ohmu  ·  2026-09-21
  1#include <u.h>
  2#include <libc.h>
  3#include <bio.h>
  4#include <thread.h>
  5#include <plumb.h>
  6#include <9pclient.h>
  7#include "dat.h"
  8
  9void*
 10emalloc(uint n)
 11{
 12	void *p;
 13
 14	p = malloc(n);
 15	if(p == nil)
 16		error("can't malloc: %r");
 17	memset(p, 0, n);
 18	setmalloctag(p, getcallerpc(&n));
 19	return p;
 20}
 21
 22void*
 23erealloc(void *p, uint n)
 24{
 25	p = realloc(p, n);
 26	if(p == nil)
 27		error("can't realloc: %r");
 28	setmalloctag(p, getcallerpc(&n));
 29	return p;
 30}
 31
 32char*
 33estrdup(char *s)
 34{
 35	char *t;
 36
 37	t = emalloc(strlen(s)+1);
 38	strcpy(t, s);
 39	return t;
 40}
 41
 42char*
 43estrstrdup(char *s, char *t)
 44{
 45	char *u;
 46
 47	u = emalloc(strlen(s)+strlen(t)+1);
 48	strcpy(u, s);
 49	strcat(u, t);
 50	return u;
 51}
 52
 53char*
 54eappend(char *s, char *sep, char *t)
 55{
 56	char *u;
 57
 58	if(t == nil)
 59		u = estrstrdup(s, sep);
 60	else{
 61		u = emalloc(strlen(s)+strlen(sep)+strlen(t)+1);
 62		strcpy(u, s);
 63		strcat(u, sep);
 64		strcat(u, t);
 65	}
 66	free(s);
 67	return u;
 68}
 69
 70char*
 71egrow(char *s, char *sep, char *t)
 72{
 73	s = eappend(s, sep, t);
 74	free(t);
 75	return s;
 76}
 77
 78void
 79error(char *fmt, ...)
 80{
 81	Fmt f;
 82	char buf[64];
 83	va_list arg;
 84
 85	fmtfdinit(&f, 2, buf, sizeof buf);
 86	fmtprint(&f, "Mail: ");
 87	va_start(arg, fmt);
 88	fmtvprint(&f, fmt, arg);
 89	va_end(arg);
 90	fmtprint(&f, "\n");
 91	fmtfdflush(&f);
 92	threadexitsall(fmt);
 93}
 94
 95void
 96ctlprint(CFid *fd, char *fmt, ...)
 97{
 98	int n;
 99	va_list arg;
100
101	va_start(arg, fmt);
102	n = fsvprint(fd, fmt, arg);
103	va_end(arg);
104	if(n <= 0)
105		error("control file write error: %r");
106}
107