w3mail

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

commit 463c035f38af82db2a7c26b227976f28b81b452f
parent 286d4af13220e8ab4817183b8c66edbd47a6de53
Author: Tomas Hlavaty <tom@logand.com>
Date:   Tue, 18 Jan 2011 22:01:45 +0100

wget moved to utils

Diffstat:
Mutils.c | 39+++++++++++++++++++++++++++++++++++++++
Mutils.h | 2++
Mw3mail.c | 39---------------------------------------
3 files changed, 41 insertions(+), 39 deletions(-)

diff --git a/utils.c b/utils.c @@ -6,6 +6,7 @@ #include <string.h> #include <stdlib.h> #include <unistd.h> +#include <sys/wait.h> const int BLEN = 4 * 1024; @@ -89,3 +90,41 @@ void dir(void *udata, dir_cb cb, char *path, ...) { closedir(d); va_end(v); } + +void wget(char *url, char *fname) { + // http://www.gnu.org/software/wget/manual/html_node/Exit-Status.html + static char *wgetmsg[] = { + "No problems occurred", + "Generic error code", + "Parse error, e.g. when parsing command-line options, .wgetrc or .netrc", + "File I/O error", + "Network failure", + "SSL verification failure", + "Username/password authentication failure", + "Protocol errors", + "Server issued an error response" + }; + int pid = fork(); + if(pid < 0) die(pid, "wget(): fork failed"); + else if(pid == 0) execlp("wget", "wget", "-q", "-O", fname, "--", url, NULL); + else { + int status; + do { + pid_t w = waitpid(pid, &status, WUNTRACED | WCONTINUED); + if(w == -1) die(1, "wget(): waitpid failed on wget '%s'", url); + if(WIFEXITED(status) && WEXITSTATUS(status)) { + int n = WEXITSTATUS(status); + fprintf(stderr, "wget '%s' failed with status %d: %s\n", url, + n, (0 <= n && n <= 8) ? wgetmsg[n] : "unexpected status"); + } + else if(WIFSIGNALED(status)) + fprintf(stderr, "wget '%s' killed by signal %d\n", url, + WTERMSIG(status)); + else if(WIFSTOPPED(status)) + fprintf(stderr, "wget '%s' stopped by signal %d\n", url, + WSTOPSIG(status)); + else if(WIFCONTINUED(status)) + fprintf(stderr, "wget '%s' continued\n", url); + } while(!WIFEXITED(status) && !WIFSIGNALED(status)); + } +} diff --git a/utils.h b/utils.h @@ -22,4 +22,6 @@ 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 wget(char *url, char *fname); + #endif /* UTILS_H */ diff --git a/w3mail.c b/w3mail.c @@ -20,50 +20,11 @@ */ -#include <sys/wait.h> #include <unistd.h> #include <time.h> #include "utils.h" -static void wget(char *url, char *fname) { - // http://www.gnu.org/software/wget/manual/html_node/Exit-Status.html - static char *wgetmsg[] = { - "No problems occurred", - "Generic error code", - "Parse error, e.g. when parsing command-line options, .wgetrc or .netrc", - "File I/O error", - "Network failure", - "SSL verification failure", - "Username/password authentication failure", - "Protocol errors", - "Server issued an error response" - }; - int pid = fork(); - if(pid < 0) die(pid, "wget(): fork failed"); - else if(pid == 0) execlp("wget", "wget", "-q", "-O", fname, "--", url, NULL); - else { - int status; - do { - pid_t w = waitpid(pid, &status, WUNTRACED | WCONTINUED); - if(w == -1) die(1, "wget(): waitpid failed on wget '%s'", url); - if(WIFEXITED(status) && WEXITSTATUS(status)) { - int n = WEXITSTATUS(status); - fprintf(stderr, "wget '%s' failed with status %d: %s\n", url, - n, (0 <= n && n <= 8) ? wgetmsg[n] : "unexpected status"); - } - else if(WIFSIGNALED(status)) - fprintf(stderr, "wget '%s' killed by signal %d\n", url, - WTERMSIG(status)); - else if(WIFSTOPPED(status)) - fprintf(stderr, "wget '%s' stopped by signal %d\n", url, - WSTOPSIG(status)); - else if(WIFCONTINUED(status)) - fprintf(stderr, "wget '%s' continued\n", url); - } while(!WIFEXITED(status) && !WIFSIGNALED(status)); - } -} - static void cb_md5sum(void *udata, FILE* in) { char *sum = udata; sum = fgets(sum, 33, in);