From 087e17be1c9945f1a24d729fa321f3eaf4542f77 Mon Sep 17 00:00:00 2001 From: Giuseppe Scrivano Date: Mon, 9 Jun 2014 08:53:10 +0200 Subject: [PATCH] Do not use exit() with a magic number --- src/ChangeLog | 14 +++++++ src/exits.c | 18 --------- src/exits.h | 17 ++++++++ src/init.c | 14 +++---- src/log.c | 5 ++- src/main.c | 88 ++++++++++++++++++++--------------------- src/mswindows.c | 7 ++-- src/netrc.c | 10 ++--- src/utils.c | 8 ++-- src/warc.c | 13 +++--- tests/Test-stdouterr.px | 2 +- 11 files changed, 107 insertions(+), 89 deletions(-) diff --git a/src/ChangeLog b/src/ChangeLog index f93cd709..1cdcff55 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,17 @@ +2014-06-10 Giuseppe Scrivano + + * exits.c: Move WGET_EXIT_* definitions to... + * exits.h: ...here. Add WGET_EXIT_GENERIC_ERROR, WGET_EXIT_PARSE_ERROR. + Remove WGET_EXIT_MINIMUM. + * init.c: Fix calls to exit(). + * log.c: Likewise. + * main.c: Likewise. + * mswindows.c: Likewise. + * netrc.c: Likewise. + * utils.c: Likewise. + * warc.c: Likewise. + * Test-stdouterr.px: Likewise. + 2014-06-08 Giuseppe Scrivano * main.c: Make `program_name' not static. diff --git a/src/exits.c b/src/exits.c index b8230f8f..cb58941d 100644 --- a/src/exits.c +++ b/src/exits.c @@ -20,24 +20,6 @@ #include "wget.h" #include "exits.h" -/* Final exit code possibilities. Exit codes 1 and 2 are reserved - * for situations that lead to direct exits from Wget, not using the - * value of final_exit_status. */ -enum - { - WGET_EXIT_SUCCESS = 0, - - WGET_EXIT_MINIMUM = 3, - WGET_EXIT_IO_FAIL = WGET_EXIT_MINIMUM, - WGET_EXIT_NETWORK_FAIL = 4, - WGET_EXIT_SSL_AUTH_FAIL = 5, - WGET_EXIT_SERVER_AUTH_FAIL = 6, - WGET_EXIT_PROTOCOL_ERROR = 7, - WGET_EXIT_SERVER_ERROR = 8, - - WGET_EXIT_UNKNOWN - }; - static int final_exit_status = WGET_EXIT_SUCCESS; /* XXX: I don't like that newly-added uerr_t codes will doubtless fall diff --git a/src/exits.h b/src/exits.h index 98dde9a7..326b013d 100644 --- a/src/exits.h +++ b/src/exits.h @@ -21,6 +21,23 @@ along with Wget. If not, see . */ #include "wget.h" +/* Final exit code possibilities. Exit codes 1 and 2 are reserved + * for situations that lead to direct exits from Wget, not using the + * value of final_exit_status. */ +enum + { + WGET_EXIT_SUCCESS = 0, + WGET_EXIT_GENERIC_ERROR = 1, + WGET_EXIT_PARSE_ERROR = 2, + WGET_EXIT_IO_FAIL = 3, + WGET_EXIT_NETWORK_FAIL = 4, + WGET_EXIT_SSL_AUTH_FAIL = 5, + WGET_EXIT_SERVER_AUTH_FAIL = 6, + WGET_EXIT_PROTOCOL_ERROR = 7, + WGET_EXIT_SERVER_ERROR = 8, + + WGET_EXIT_UNKNOWN + }; void inform_exit_status (uerr_t err); diff --git a/src/init.c b/src/init.c index 9a6fd2b5..4c7bc8e2 100644 --- a/src/init.c +++ b/src/init.c @@ -488,7 +488,7 @@ wgetrc_env_file_name (void) { fprintf (stderr, _("%s: WGETRC points to %s, which doesn't exist.\n"), exec_name, env); - exit (1); + exit (WGET_EXIT_GENERIC_ERROR); } return xstrdup (env); } @@ -660,7 +660,7 @@ initialize (void) Parsing system wgetrc file (env SYSTEM_WGETRC) failed. Please check\n\ '%s',\n\ or specify a different file using --config.\n"), env_sysrc); - exit (2); + exit (WGET_EXIT_PARSE_ERROR); } } /* Otherwise, if SYSTEM_WGETRC is defined, use it. */ @@ -675,7 +675,7 @@ or specify a different file using --config.\n"), env_sysrc); Parsing system wgetrc file failed. Please check\n\ '%s',\n\ or specify a different file using --config.\n"), SYSTEM_WGETRC); - exit (2); + exit (WGET_EXIT_PARSE_ERROR); } #endif /* Override it with your own, if one exists. */ @@ -697,7 +697,7 @@ or specify a different file using --config.\n"), SYSTEM_WGETRC); /* If there were errors processing either `.wgetrc', abort. */ if (!ok) - exit (2); + exit (WGET_EXIT_PARSE_ERROR); xfree (file); return; @@ -856,7 +856,7 @@ setoptval (const char *com, const char *val, const char *optname) assert (val != NULL); if (!setval_internal (command_by_name (com), dd_optname, val)) - exit (2); + exit (WGET_EXIT_PARSE_ERROR); } /* Parse OPT into command and value and run it. For example, @@ -872,14 +872,14 @@ run_command (const char *cmdopt) { case line_ok: if (!setval_internal (comind, com, val)) - exit (2); + exit (WGET_EXIT_PARSE_ERROR); xfree (com); xfree (val); break; default: fprintf (stderr, _("%s: Invalid --execute command %s\n"), exec_name, quote (cmdopt)); - exit (2); + exit (WGET_EXIT_PARSE_ERROR); } } diff --git a/src/log.c b/src/log.c index 3bd58330..5334cb22 100644 --- a/src/log.c +++ b/src/log.c @@ -39,6 +39,7 @@ as that of the covered work. */ #include #include "utils.h" +#include "exits.h" #include "log.h" /* 2005-10-25 SMS. @@ -547,7 +548,7 @@ logprintf (enum log_options o, const char *fmt, ...) va_end (args); if (done && errno == EPIPE) - exit (1); + exit (WGET_EXIT_GENERIC_ERROR); } while (!done); } @@ -591,7 +592,7 @@ log_init (const char *file, bool appendp) if (!logfp) { fprintf (stderr, "%s: %s: %s\n", exec_name, file, strerror (errno)); - exit (1); + exit (WGET_EXIT_GENERIC_ERROR); } } else diff --git a/src/main.c b/src/main.c index 38a32ffb..8db80e49 100644 --- a/src/main.c +++ b/src/main.c @@ -783,15 +783,15 @@ Recursive accept/reject:\n"), if (printf (_("GNU Wget %s, a non-interactive network retriever.\n"), version_string) < 0) - exit (3); + exit (WGET_EXIT_IO_FAIL); if (print_usage (0) < 0) - exit (3); + exit (WGET_EXIT_IO_FAIL); for (i = 0; i < countof (help); i++) if (fputs (_(help[i]), stdout) < 0) - exit (3); + exit (WGET_EXIT_IO_FAIL); - exit (0); + exit (WGET_EXIT_SUCCESS); } /* Return a human-readable printed representation of INTERVAL, @@ -890,7 +890,7 @@ print_version (void) int i; if (printf (_("GNU Wget %s built on %s.\n\n"), version_string, OS_TYPE) < 0) - exit (3); + exit (WGET_EXIT_IO_FAIL); for (i = 0; compiled_features[i] != NULL; ) { @@ -898,83 +898,83 @@ print_version (void) while ((line_length > 0) && (compiled_features[i] != NULL)) { if (printf ("%s ", compiled_features[i]) < 0) - exit (3); + exit (WGET_EXIT_IO_FAIL); line_length -= strlen (compiled_features[i]) + 2; i++; } if (printf ("\n") < 0) - exit (3); + exit (WGET_EXIT_IO_FAIL); } if (printf ("\n") < 0) - exit (3); + exit (WGET_EXIT_IO_FAIL); /* Handle the case when $WGETRC is unset and $HOME/.wgetrc is absent. */ if (printf ("%s\n", wgetrc_title) < 0) - exit (3); + exit (WGET_EXIT_IO_FAIL); env_wgetrc = wgetrc_env_file_name (); if (env_wgetrc && *env_wgetrc) { if (printf (_(" %s (env)\n"), env_wgetrc) < 0) - exit (3); + exit (WGET_EXIT_IO_FAIL); xfree (env_wgetrc); } user_wgetrc = wgetrc_user_file_name (); if (user_wgetrc) { if (printf (_(" %s (user)\n"), user_wgetrc) < 0) - exit (3); + exit (WGET_EXIT_IO_FAIL); xfree (user_wgetrc); } #ifdef SYSTEM_WGETRC if (printf (_(" %s (system)\n"), SYSTEM_WGETRC) < 0) - exit (3); + exit (WGET_EXIT_IO_FAIL); #endif #ifdef ENABLE_NLS if (format_and_print_line (locale_title, LOCALEDIR, MAX_CHARS_PER_LINE) < 0) - exit (3); + exit (WGET_EXIT_IO_FAIL); #endif /* def ENABLE_NLS */ if (compilation_string != NULL) if (format_and_print_line (compile_title, compilation_string, MAX_CHARS_PER_LINE) < 0) - exit (3); + exit (WGET_EXIT_IO_FAIL); if (link_string != NULL) if (format_and_print_line (link_title, link_string, MAX_CHARS_PER_LINE) < 0) - exit (3); + exit (WGET_EXIT_IO_FAIL); if (printf ("\n") < 0) - exit (3); + exit (WGET_EXIT_IO_FAIL); /* TRANSLATORS: When available, an actual copyright character (circle-c) should be used in preference to "(C)". */ if (printf (_("\ Copyright (C) %s Free Software Foundation, Inc.\n"), "2014") < 0) - exit (3); + exit (WGET_EXIT_IO_FAIL); if (fputs (_("\ License GPLv3+: GNU GPL version 3 or later\n\ .\n\ This is free software: you are free to change and redistribute it.\n\ There is NO WARRANTY, to the extent permitted by law.\n"), stdout) < 0) - exit (3); + exit (WGET_EXIT_IO_FAIL); /* TRANSLATORS: When available, please use the proper diacritics for names such as this one. See en_US.po for reference. */ if (fputs (_("\nOriginally written by Hrvoje Niksic .\n"), stdout) < 0) - exit (3); + exit (WGET_EXIT_IO_FAIL); if (fputs (_("Please send bug reports and questions to .\n"), stdout) < 0) - exit (3); + exit (WGET_EXIT_IO_FAIL); - exit (0); + exit (WGET_EXIT_SUCCESS); } char *program_name; /* Needed by lib/error.c. */ @@ -1022,7 +1022,7 @@ main (int argc, char **argv) if (p == NULL) { fprintf (stderr, _("Memory allocation problem\n")); - exit (2); + exit (WGET_EXIT_PARSE_ERROR); } for (i = 1; i < argc; i++) { @@ -1073,7 +1073,7 @@ main (int argc, char **argv) else { fprintf (stderr, _("Exiting due to error in %s\n"), optarg); - exit (2); + exit (WGET_EXIT_PARSE_ERROR); } } } @@ -1103,7 +1103,7 @@ main (int argc, char **argv) fprintf (stderr, "\n"); fprintf (stderr, _("Try `%s --help' for more options.\n"), exec_name); - exit (2); + exit (WGET_EXIT_PARSE_ERROR); } /* Find the short option character in the mapping. */ longindex = optmap[ret - 32]; @@ -1173,7 +1173,7 @@ main (int argc, char **argv) fprintf (stderr, "\n"); fprintf (stderr, _("Try `%s --help' for more options.\n"), exec_name); - exit (1); + exit (WGET_EXIT_GENERIC_ERROR); } break; } @@ -1254,14 +1254,14 @@ main (int argc, char **argv) { fprintf (stderr, _("Can't be verbose and quiet at the same time.\n")); print_usage (1); - exit (1); + exit (WGET_EXIT_GENERIC_ERROR); } if (opt.timestamping && opt.noclobber) { fprintf (stderr, _("\ Can't timestamp and not clobber old files at the same time.\n")); print_usage (1); - exit (1); + exit (WGET_EXIT_GENERIC_ERROR); } #ifdef ENABLE_IPV6 if (opt.ipv4_only && opt.ipv6_only) @@ -1269,7 +1269,7 @@ Can't timestamp and not clobber old files at the same time.\n")); fprintf (stderr, _("Cannot specify both --inet4-only and --inet6-only.\n")); print_usage (1); - exit (1); + exit (WGET_EXIT_GENERIC_ERROR); } #endif if (opt.output_document) @@ -1281,7 +1281,7 @@ Can't timestamp and not clobber old files at the same time.\n")); Cannot specify both -k and -O if multiple URLs are given, or in combination\n\ with -p or -r. See the manual for details.\n\n"), stderr); print_usage (1); - exit (1); + exit (WGET_EXIT_GENERIC_ERROR); } if (opt.page_requisites || opt.recursive) @@ -1303,7 +1303,7 @@ for details.\n\n")); logprintf (LOG_VERBOSE, _("File `%s' already there; not retrieving.\n"), opt.output_document); - exit(1); + exit (WGET_EXIT_GENERIC_ERROR); } } @@ -1327,7 +1327,7 @@ for details.\n\n")); { fprintf (stderr, _("WARC output does not work with --spider.\n")); - exit (1); + exit (WGET_EXIT_GENERIC_ERROR); } if (opt.always_rest || opt.start_pos >= 0) { @@ -1354,7 +1354,7 @@ for details.\n\n")); fprintf (stderr, _("Cannot specify both --ask-password and --password.\n")); print_usage (1); - exit (1); + exit (WGET_EXIT_GENERIC_ERROR); } if (opt.start_pos >= 0 && opt.always_rest) @@ -1374,7 +1374,7 @@ for details.\n\n")); /* #### Something nicer should be printed here -- similar to the pre-1.5 `--help' page. */ fprintf (stderr, _("Try `%s --help' for more options.\n"), exec_name); - exit (1); + exit (WGET_EXIT_GENERIC_ERROR); } /* Compile the regular expressions. */ @@ -1397,26 +1397,26 @@ for details.\n\n")); { opt.acceptregex = opt.regex_compile_fun (opt.acceptregex_s); if (!opt.acceptregex) - exit (1); + exit (WGET_EXIT_GENERIC_ERROR); } if (opt.rejectregex_s) { opt.rejectregex = opt.regex_compile_fun (opt.rejectregex_s); if (!opt.rejectregex) - exit (1); + exit (WGET_EXIT_GENERIC_ERROR); } if (opt.post_data || opt.post_file_name) { if (opt.post_data && opt.post_file_name) { fprintf (stderr, _("You cannot specify both --post-data and --post-file.\n")); - exit (1); + exit (WGET_EXIT_GENERIC_ERROR); } else if (opt.method) { fprintf (stderr, _("You cannot use --post-data or --post-file along with --method. " "--method expects data through --body-data and --body-file options")); - exit (1); + exit (WGET_EXIT_GENERIC_ERROR); } } if (opt.body_data || opt.body_file) @@ -1425,12 +1425,12 @@ for details.\n\n")); { fprintf (stderr, _("You must specify a method through --method=HTTPMethod " "to use with --body-data or --body-file.\n")); - exit (1); + exit (WGET_EXIT_GENERIC_ERROR); } else if (opt.body_data && opt.body_file) { fprintf (stderr, _("You cannot specify both --body-data and --body-file.\n")); - exit (1); + exit (WGET_EXIT_GENERIC_ERROR); } } @@ -1482,7 +1482,7 @@ for details.\n\n")); { /* sXXXav : be more specific... */ fprintf (stderr, _("This version does not have support for IRIs\n")); - exit(1); + exit (WGET_EXIT_GENERIC_ERROR); } #endif @@ -1491,7 +1491,7 @@ for details.\n\n")); opt.passwd = prompt_for_password (); if (opt.passwd == NULL || opt.passwd[0] == '\0') - exit (1); + exit (WGET_EXIT_GENERIC_ERROR); } #ifdef USE_WATT32 @@ -1513,7 +1513,7 @@ for details.\n\n")); if (url == NULL) { fprintf (stderr, _("Memory allocation problem\n")); - exit (2); + exit (WGET_EXIT_PARSE_ERROR); } for (i = 0; i < nurl; i++, optind++) { @@ -1574,7 +1574,7 @@ for details.\n\n")); if (output_stream == NULL) { perror (opt.output_document); - exit (1); + exit (WGET_EXIT_GENERIC_ERROR); } if (fstat (fileno (output_stream), &st) == 0 && S_ISREG (st.st_mode)) output_stream_regular = true; @@ -1584,7 +1584,7 @@ for details.\n\n")); fprintf (stderr, _("-k can be used together with -O only if \ outputting to a regular file.\n")); print_usage (1); - exit(1); + exit (WGET_EXIT_GENERIC_ERROR); } } diff --git a/src/mswindows.c b/src/mswindows.c index 179773e0..0ed8c053 100644 --- a/src/mswindows.c +++ b/src/mswindows.c @@ -42,6 +42,7 @@ as that of the covered work. */ #include "utils.h" #include "url.h" +#include "exits.h" #ifndef ES_SYSTEM_REQUIRED #define ES_SYSTEM_REQUIRED 0x00000001 @@ -308,7 +309,7 @@ cleanup: /* We're the parent. If all is well, terminate. */ if (rv) - exit (0); + exit (WGET_EXIT_SUCCESS); /* We failed, return. */ } @@ -461,7 +462,7 @@ ws_startup (void) { fprintf (stderr, _("%s: Couldn't find usable socket driver.\n"), exec_name); - exit (1); + exit (WGET_EXIT_GENERIC_ERROR); } if (data.wVersion < requested) @@ -469,7 +470,7 @@ ws_startup (void) fprintf (stderr, _("%s: Couldn't find usable socket driver.\n"), exec_name); WSACleanup (); - exit (1); + exit (WGET_EXIT_GENERIC_ERROR); } atexit (ws_cleanup); diff --git a/src/netrc.c b/src/netrc.c index 1e12d1de..5fc34ccf 100644 --- a/src/netrc.c +++ b/src/netrc.c @@ -448,7 +448,7 @@ main (int argc, char **argv) if (argc < 2 || argc > 3) { fprintf (stderr, _("Usage: %s NETRC [HOSTNAME]\n"), argv[0]); - exit (1); + exit (WGET_EXIT_GENERIC_ERROR); } program_name = argv[0]; @@ -459,7 +459,7 @@ main (int argc, char **argv) { fprintf (stderr, _("%s: cannot stat %s: %s\n"), argv[0], file, strerror (errno)); - exit (1); + exit (WGET_EXIT_GENERIC_ERROR); } head = parse_netrc (file); @@ -498,14 +498,14 @@ main (int argc, char **argv) /* Exit if we found the target. */ if (target) - exit (0); + exit (WGET_EXIT_SUCCESS); a = a->next; } /* Exit with failure if we had a target, success otherwise. */ if (target) - exit (1); + exit (WGET_EXIT_GENERIC_ERROR); - exit (0); + exit (WGET_EXIT_SUCCESS); } #endif /* STANDALONE */ diff --git a/src/utils.c b/src/utils.c index 8168fb38..327d8c6f 100644 --- a/src/utils.c +++ b/src/utils.c @@ -100,6 +100,8 @@ as that of the covered work. */ #include "test.h" #endif +#include "exits.h" + static void memfatal (const char *context, long attempted_size) { @@ -123,7 +125,7 @@ memfatal (const char *context, long attempted_size) exec_name, context, attempted_size); } - exit (1); + exit (WGET_EXIT_GENERIC_ERROR); } /* Character property table for (re-)escaping VMS ODS5 extended file @@ -471,7 +473,7 @@ fork_to_background (void) { /* parent, error */ perror ("fork"); - exit (1); + exit (WGET_EXIT_GENERIC_ERROR); } else if (pid != 0) { @@ -479,7 +481,7 @@ fork_to_background (void) printf (_("Continuing in background, pid %d.\n"), (int) pid); if (logfile_changed) printf (_("Output will be written to %s.\n"), quote (opt.lfilename)); - exit (0); /* #### should we use _exit()? */ + exit (WGET_EXIT_SUCCESS); /* #### should we use _exit()? */ } /* child: give up the privileges and keep running. */ diff --git a/src/warc.c b/src/warc.c index 82b5bb35..dbfc16f1 100644 --- a/src/warc.c +++ b/src/warc.c @@ -56,6 +56,7 @@ as that of the covered work. */ #endif #include "warc.h" +#include "exits.h" #ifndef O_TEMPORARY #define O_TEMPORARY 0 @@ -1038,7 +1039,7 @@ warc_init (void) logprintf (LOG_NOTQUIET, _("Could not read CDX file %s for deduplication.\n"), quote (opt.warc_cdx_dedup_filename)); - exit(1); + exit (WGET_EXIT_GENERIC_ERROR); } } @@ -1047,7 +1048,7 @@ warc_init (void) { logprintf (LOG_NOTQUIET, _("Could not open temporary WARC manifest file.\n")); - exit(1); + exit (WGET_EXIT_GENERIC_ERROR); } if (opt.warc_keep_log) @@ -1057,7 +1058,7 @@ warc_init (void) { logprintf (LOG_NOTQUIET, _("Could not open temporary WARC log file.\n")); - exit(1); + exit (WGET_EXIT_GENERIC_ERROR); } log_set_warc_log_fp (warc_log_fp); } @@ -1066,7 +1067,7 @@ warc_init (void) if (! warc_start_new_file (false)) { logprintf (LOG_NOTQUIET, _("Could not open WARC file.\n")); - exit(1); + exit (WGET_EXIT_GENERIC_ERROR); } if (opt.warc_cdx_enabled) @@ -1075,7 +1076,7 @@ warc_init (void) { logprintf (LOG_NOTQUIET, _("Could not open CDX file for output.\n")); - exit(1); + exit (WGET_EXIT_GENERIC_ERROR); } } } @@ -1103,7 +1104,7 @@ warc_write_metadata (void) if (warc_tmp_fp == NULL) { logprintf (LOG_NOTQUIET, _("Could not open temporary WARC file.\n")); - exit(1); + exit (WGET_EXIT_GENERIC_ERROR); } fflush (warc_tmp_fp); fprintf (warc_tmp_fp, "%s\n", program_argstring); diff --git a/tests/Test-stdouterr.px b/tests/Test-stdouterr.px index d594ead0..e2914091 100755 --- a/tests/Test-stdouterr.px +++ b/tests/Test-stdouterr.px @@ -21,7 +21,7 @@ my %urls = ( ); unless(-e "/dev/full") { - exit(2); # skip + exit 2; # skip } my $cmdline = $WgetTest::WGETPATH . " -c http://localhost:{{port}}/somefile.txt -O /dev/full";