beans

simple pastebin server
git clone git://git.bitsmanent.org/beans
Log | Files | Refs | LICENSE

commit 8f1c773b1712ee11903ea276fb8bc1d0a8846e17
parent 1c03381d5bcbe3c8d409ff800417b14b4da85760
Author: Claudio Alessi <smoppy@gmail.com>
Date:   Sun, 27 Jan 2019 13:46:45 +0100

Move the main loop into a new run() function.

Diffstat:
Mbeans.c | 68+++++++++++++++++++++++++++++++++++++++++---------------------------
1 file changed, 41 insertions(+), 27 deletions(-)

diff --git a/beans.c b/beans.c @@ -13,14 +13,23 @@ #include "arg.h" -#define BACKLOG 32 /* XXX What's a reasonable value? */ +#define BACKLOG 32 -char *argv0; +/* function declarations */ +void die(const char *errstr, ...); +int bindon(char *port); +void *ecalloc(size_t nmemb, size_t size); +char *readall(int sd, int *len); +void run(void); +void sout(int sd, char *fmt, ...); +/* variables */ +char *argv0; char port[8] = "2023"; char base[256] = ""; char path[256] = "/tmp"; char mode[8] = "0600"; +int sockd; /* function implementations */ void @@ -90,36 +99,15 @@ readall(int sd, int *len) { } void -sout(int sd, char *fmt, ...) { - va_list ap; - char buf[4096]; - int sz; - - va_start(ap, fmt); - sz = vsnprintf(buf, sizeof buf, fmt, ap); - va_end(ap); - send(sd, buf, sz, 0); -} - -int -main(int argc, char *argv[]) { +run(void) { struct sockaddr_storage conn; socklen_t size; - int sd, csd, len, tmpsd; + int csd, len, tmpsd; char tmpfn[64] = {0}, *buf, *code; - ARGBEGIN { - case 'b': strncpy(base, EARGF(die("%s: missing base URL\n", argv0)), sizeof base); break; - case 'd': strncpy(path, EARGF(die("%s: missing path\n", argv0)), sizeof path); break; - case 'm': strncpy(mode, EARGF(die("%s: missing mode\n", argv0)), sizeof mode); break; - case 'p': strncpy(port, EARGF(die("%s: missing port\n", argv0)), sizeof port); break; - case 'v': die("beans-"VERSION"\n"); - } ARGEND - - sd = bindon(port); size = sizeof conn; while(1) { - csd = accept(sd, (struct sockaddr *)&conn, &size); + csd = accept(sockd, (struct sockaddr *)&conn, &size); if(csd == -1) { fprintf(stderr, "accept(): %s", strerror(errno)); continue; @@ -150,6 +138,32 @@ main(int argc, char *argv[]) { close(tmpsd); free(buf); } - close(sd); +} + +void +sout(int sd, char *fmt, ...) { + va_list ap; + char buf[4096]; + int sz; + + va_start(ap, fmt); + sz = vsnprintf(buf, sizeof buf, fmt, ap); + va_end(ap); + send(sd, buf, sz, 0); +} + +int +main(int argc, char *argv[]) { + ARGBEGIN { + case 'b': strncpy(base, EARGF(die("%s: missing base URL\n", argv0)), sizeof base); break; + case 'd': strncpy(path, EARGF(die("%s: missing path\n", argv0)), sizeof path); break; + case 'm': strncpy(mode, EARGF(die("%s: missing mode\n", argv0)), sizeof mode); break; + case 'p': strncpy(port, EARGF(die("%s: missing port\n", argv0)), sizeof port); break; + case 'v': die("beans-"VERSION"\n"); + } ARGEND + + sockd = bindon(port); + run(); + close(sockd); return 0; }