w3mail

program to send a web page by email
git clone https://logand.com/git/w3mail.git/
Log | Files | Refs | README | LICENSE

commit 72c8d65b36e3c25d0fed662124c2329b5be71423
parent 5ac3b1f94b6153033d9de6dff3fc31b68e8aed59
Author: Tomas Hlavaty <tom@logand.com>
Date:   Tue, 18 Jan 2011 02:16:59 +0100

dir and tmpf printf-like varargs added and a bit of style clean-up

Diffstat:
Mdirpop3d.c | 20++++++++------------
Mutils.c | 36+++++++++++++++++++++++-------------
Mutils.h | 7+++----
Mw3mail.c | 8+++-----
4 files changed, 37 insertions(+), 34 deletions(-)

diff --git a/dirpop3d.c b/dirpop3d.c @@ -43,7 +43,7 @@ static void tcp_server(int port, tcp_handler handler) { struct sockaddr_in sa; memset(&sa, 0, sizeof(sa)); sa.sin_family = AF_INET; - sa.sin_addr.s_addr = inet_addr ("127.0.0.1"); //htonl(INADDR_ANY); + sa.sin_addr.s_addr = inet_addr("127.0.0.1"); //htonl(INADDR_ANY); sa.sin_port = htons(port); if(bind(fds, (struct sockaddr *) &sa, sizeof(sa)) < 0) die(1, "bind() failed"); @@ -125,7 +125,8 @@ static struct flist *flist_add(struct flist *x, char *fname, int nbytes) { struct msg *y = &x->msg[x->n++]; y->deleted = 0; y->nbytes = nbytes; - y->fname = fname; + y->fname = malloc(strlen(fname) + 1); + strcpy(y->fname, fname); return x; } @@ -133,14 +134,11 @@ struct xlist { struct flist *flist; }; -static void list_cb(void *udata, char *path, struct dirent *e, struct stat *s) { +static int list_cb(void *udata, char *path, char *fname, struct dirent *e, struct stat *s) { struct xlist *x = udata; - if(!S_ISREG(s->st_mode)) return; - char buf[BLEN]; - snprintf(buf, BLEN, "%s/%s", path, e->d_name); - char *fname = malloc(strlen(buf) + 1); - strcpy(fname, buf); - x->flist = flist_add(x->flist, fname, s->st_size); + if(S_ISREG(s->st_mode)) + x->flist = flist_add(x->flist, fname, s->st_size); + return 1; } static char *rootdir; @@ -185,12 +183,10 @@ static void pop3_handler(int fd) { else if(0 == strcmp("STAT", cmd)) { if(state != PASS) pr(fd, "-ERR"); else { - char buf[BLEN]; - snprintf(buf, BLEN, "%s/%s", rootdir, usr); if(!flist) { flist = flist_new(100); struct xlist x = {flist}; - dir(&x, list_cb, buf); + dir(&x, list_cb, "%s/%s", rootdir, usr); flist = x.flist; } pr(fd, "+OK %d %d", flist->n, flist->nbytes); diff --git a/utils.c b/utils.c @@ -57,25 +57,35 @@ void out(void *udata, out_cb cb, char *cmd, ...) { va_end(v); } -void tmpf(void *udata, tmpf_cb cb, char *fname) { - int fd = mkstemp(fname); - if(fd == -1) die(errno, "tmpf(): cannot create temporary file '%s'", fname); +void tmpf(void *udata, tmpf_cb cb, char *fname, ...) { + va_list v; + va_start(v, fname); + char buf[BLEN]; + vsnprintf(buf, BLEN, fname, v); + int fd = mkstemp(buf); + if(fd == -1) die(errno, "tmpf(): cannot create temporary file '%s'", buf); close(fd); - cb(udata, fname); - unlink(fname); + cb(udata, buf); + unlink(buf); + va_end(v); } -void dir(void *udata, dir_cb cb, char *path) { - DIR *d = opendir (path); - if(!d) die(1, "dir(): cannot open dir '%s'", path); - struct dirent *e; +void dir(void *udata, dir_cb cb, char *path, ...) { + va_list v; + va_start(v, path); char buf[BLEN]; + snprintf(buf, BLEN, path, v); + DIR *d = opendir(buf); + if(!d) die(1, "dir(): cannot open dir '%s'", buf); + struct dirent *e; while((e = readdir(d))) { + char fname[BLEN]; + snprintf(fname, BLEN, "%s/%s", buf, e->d_name); struct stat s; - snprintf(buf, BLEN, "%s/%s", path, e->d_name); - if(lstat(buf, &s) < 0) - die(1, "stat_cb(): lstat('%s') failed", buf); - cb(udata, path, e, &s); + if(lstat(fname, &s) < 0) + die(1, "stat_cb(): lstat('%s') failed", fname); + if(!cb(udata, buf, fname, e, &s)) break; } closedir(d); + va_end(v); } diff --git a/utils.h b/utils.h @@ -15,12 +15,11 @@ extern void rtrim(char *s); typedef void (*in_cb)(void *udata, FILE* in); typedef void (*out_cb)(void *udata, FILE* out); typedef void (*tmpf_cb)(void *udata, char *fname); -typedef void (*dir_cb)(void *udata, char *path, struct dirent *e, struct stat *s); // TODO return bool +typedef int (*dir_cb)(void *udata, char *path, char *fname, struct dirent *e, struct stat *s); extern void in(void *udata, in_cb cb, char *cmd, ...); extern void out(void *udata, out_cb cb, char *cmd, ...); -extern void tmpf(void *udata, tmpf_cb cb, char *fname); - -extern void dir(void *udata, dir_cb cb, char *path); +extern void tmpf(void *udata, tmpf_cb cb, char *fname, ...); +extern void dir(void *udata, dir_cb cb, char *path, ...); #endif /* UTILS_H */ diff --git a/w3mail.c b/w3mail.c @@ -55,10 +55,10 @@ static void wget(char *url, char *fname) { else if(WIFSIGNALED(status)) fprintf(stderr, "wget '%s' killed by signal %d\n", url, WTERMSIG(status)); - else if (WIFSTOPPED(status)) + else if(WIFSTOPPED(status)) fprintf(stderr, "wget '%s' stopped by signal %d\n", url, WSTOPSIG(status)); - else if (WIFCONTINUED(status)) + else if(WIFCONTINUED(status)) fprintf(stderr, "wget '%s' continued\n", url); } while(!WIFEXITED(status) && !WIFSIGNALED(status)); } @@ -151,9 +151,7 @@ static void cb_config(void *udata, FILE* in) { } static void run(struct w3mail *x) { - char fname[BLEN]; - snprintf(fname, BLEN, "/tmp/w3mail-XXXXXX"); - tmpf(x, cb_w3mail, fname); + tmpf(x, cb_w3mail, "/tmp/w3mail-XXXXXX"); } int main(int argc, char *argv[]) {