diff options
author | cinap_lenrek <cinap_lenrek@localhost> | 2011-04-14 17:27:24 +0000 |
---|---|---|
committer | cinap_lenrek <cinap_lenrek@localhost> | 2011-04-14 17:27:24 +0000 |
commit | a150899221a5badff9740703b754f901b4f52762 (patch) | |
tree | 3d6911874b0ee763e40490d04f56fc09d89ccfa6 /acme/wiki/src/util.c | |
parent | 71cfa9c637386ebe00fc6d1bf6215db6657559f4 (diff) | |
download | plan9front-a150899221a5badff9740703b754f901b4f52762.tar.xz |
fill /acme
Diffstat (limited to 'acme/wiki/src/util.c')
-rw-r--r-- | acme/wiki/src/util.c | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/acme/wiki/src/util.c b/acme/wiki/src/util.c new file mode 100644 index 000000000..832d97321 --- /dev/null +++ b/acme/wiki/src/util.c @@ -0,0 +1,89 @@ +#include "awiki.h" + +void* +emalloc(uint n) +{ + void *p; + + p = malloc(n); + if(p == nil) + error("can't malloc: %r"); + memset(p, 0, n); + return p; +} + +char* +estrdup(char *s) +{ + char *t; + + t = emalloc(strlen(s)+1); + strcpy(t, s); + return t; +} + +char* +estrstrdup(char *s, char *t) +{ + char *u; + + u = emalloc(strlen(s)+strlen(t)+1); + strcpy(u, s); + strcat(u, t); + return u; +} + +char* +eappend(char *s, char *sep, char *t) +{ + char *u; + + 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; +} + +char* +egrow(char *s, char *sep, char *t) +{ + s = eappend(s, sep, t); + free(t); + return s; +} + +void +error(char *fmt, ...) +{ + int n; + va_list arg; + char buf[256]; + + fprint(2, "Wiki: "); + va_start(arg, fmt); + n = vseprint(buf, buf+sizeof buf, fmt, arg) - buf; + va_end(arg); + write(2, buf, n); + write(2, "\n", 1); + threadexitsall(fmt); +} + +void +ctlprint(int fd, char *fmt, ...) +{ + int n; + va_list arg; + char buf[256]; + + va_start(arg, fmt); + n = vseprint(buf, buf+sizeof buf, fmt, arg) - buf; + va_end(arg); + if(write(fd, buf, n) != n) + error("control file write(%s) error: %r", buf); +} |