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