commit 6774dc90e7defb6f2007d7919c5ebdf582c854ea
parent 8329a259f0b14f05be860d53b59530da91c7f6b8
Author: Tomas Hlavaty <tom@logand.com>
Date: Thu, 18 Nov 2010 23:12:44 +0100
wget exit/wait status checking
Diffstat:
M | w3mail.c | | | 43 | ++++++++++++++++++++++++++++++++++++++++++- |
1 file changed, 42 insertions(+), 1 deletion(-)
diff --git a/w3mail.c b/w3mail.c
@@ -77,11 +77,52 @@ static void fpr(FILE *out, ...) {
va_end(v);
}
+// TODO display readable error message instead of just exit status
+//
+// http://www.gnu.org/software/wget/manual/html_node/Exit-Status.html
+//
+// 0 No problems occurred.
+// 1 Generic error code.
+// 2 Parse error, e.g. when parsing command-line options, .wgetrc or .netrc
+// 3 File I/O error.
+// 4 Network failure.
+// 5 SSL verification failure.
+// 6 Username/password authentication failure.
+// 7 Protocol errors.
+// 8 Server issued an error response.
+//
+// With the exceptions of 0 and 1, the lower-numbered exit codes take
+// precedence over higher-numbered ones, when multiple types of errors
+// are encountered.
+//
+// In versions of Wget prior to 1.12, Wget's exit status tended to be
+// unhelpful and inconsistent. Recursive downloads would virtually always
+// return 0 (success), regardless of any issues encountered, and
+// non-recursive fetches only returned the status corresponding to the
+// most recently-attempted download.
+
static void wget(char *url, char *fname) {
int pid = fork();
if(pid < 0) die(pid, "wget(): fork failed");
else if(pid == 0) execlp("wget", "wget", "-q", "-O", fname, "--", url, NULL);
- else wait(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))
+ fprintf(stderr, "wget '%s' failed with status %d\n", url,
+ WEXITSTATUS(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) {