From: Fredrik Tolf Date: Mon, 4 Jun 2012 17:02:41 +0000 (+0200) Subject: lib: Made simpleerror callable with stdio handles as well as FDs. X-Git-Tag: 0.11~16 X-Git-Url: http://www.dolda2000.com/gitweb/?p=ashd.git;a=commitdiff_plain;h=65e8a9a08a303b49015ac21c951576f10831415d lib: Made simpleerror callable with stdio handles as well as FDs. --- diff --git a/lib/resp.c b/lib/resp.c index 2c0fa83..52fbd4b 100644 --- a/lib/resp.c +++ b/lib/resp.c @@ -86,16 +86,12 @@ char *htmlquote(char *text) return(ret = buf.b); } -void simpleerror(int fd, int code, char *msg, char *fmt, ...) +static void simpleerror2v(FILE *out, int code, char *msg, char *fmt, va_list args) { struct charbuf buf; char *tmp; - va_list args; - FILE *out; - va_start(args, fmt); tmp = vsprintf2(fmt, args); - va_end(args); bufinit(buf); bufcatstr(buf, "\r\n"); bufcatstr(buf, "\r\n"); @@ -108,17 +104,36 @@ void simpleerror(int fd, int code, char *msg, char *fmt, ...) bprintf(&buf, "

%s

\r\n", htmlquote(tmp)); bufcatstr(buf, "\r\n"); bufcatstr(buf, "\r\n"); - out = fdopen(dup(fd), "w"); fprintf(out, "HTTP/1.1 %i %s\n", code, msg); fprintf(out, "Content-Type: text/html\n"); fprintf(out, "Content-Length: %zi\n", buf.d); fprintf(out, "\n"); fwrite(buf.b, 1, buf.d, out); - fclose(out); buffree(buf); free(tmp); } +void simpleerror2(FILE *out, int code, char *msg, char *fmt, ...) +{ + va_list args; + + va_start(args, fmt); + simpleerror2v(out, code, msg, fmt, args); + va_end(args); +} + +void simpleerror(int fd, int code, char *msg, char *fmt, ...) +{ + va_list args; + FILE *out; + + va_start(args, fmt); + out = fdopen(dup(fd), "w"); + simpleerror2v(out, code, msg, fmt, args); + fclose(out); + va_end(args); +} + void stdredir(struct hthead *req, int fd, int code, char *dst) { FILE *out; diff --git a/lib/resp.h b/lib/resp.h index 6cb7ec3..8730584 100644 --- a/lib/resp.h +++ b/lib/resp.h @@ -6,6 +6,7 @@ char *urlquote(char *text); char *htmlquote(char *text); void simpleerror(int fd, int code, char *msg, char *fmt, ...); +void simpleerror2(FILE *out, int code, char *msg, char *fmt, ...); void stdredir(struct hthead *req, int fd, int code, char *dst); char *fmthttpdate(time_t time); time_t parsehttpdate(char *date);