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