Blob
1 #include "a.h"3 void*4 emalloc(int n)5 {6 void *v;8 v = mallocz(n, 1);9 if(v == nil)10 sysfatal("out of memory");11 return v;12 }14 void*15 erealloc(void *v, int n)16 {17 v = realloc(v, n);18 if(v == nil)19 sysfatal("out of memory");20 return v;21 }23 char*24 estrdup(char *s)25 {26 s = strdup(s);27 if(s == nil)28 sysfatal("out of memory");29 return s;30 }32 int33 timefmt(Fmt *f)34 {35 Tm tm;36 vlong ms;38 ms = nsec()/1000000;40 tm = *localtime(ms/1000);41 fmtprint(f, "%02d:%02d:%02d.%03d",42 tm.hour, tm.min, tm.sec,43 (int)(ms%1000));44 return 0;45 }47 int48 writen(int fd, void *buf, int n)49 {50 long m, tot;52 for(tot=0; tot<n; tot+=m){53 m = n - tot;54 if(m > 8192)55 m = 8192;56 if(write(fd, (uchar*)buf+tot, m) != m)57 break;58 }59 return tot;60 }62 int63 urlencodefmt(Fmt *fmt)64 {65 int x;66 char *s;68 s = va_arg(fmt->args, char*);69 for(; *s; s++){70 x = (uchar)*s;71 if(x == ' ')72 fmtrune(fmt, '+');73 else if(('a' <= x && x <= 'z') || ('A' <= x && x <= 'Z') || ('0' <= x && x <= '9')74 || strchr("$-_.+!*'()", x)){75 fmtrune(fmt, x);76 }else77 fmtprint(fmt, "%%%02ux", x);78 }79 return 0;80 }