From: Zbigniew Jędrzejewski-Szmek Date: Fri, 16 Nov 2018 09:39:57 +0000 (+0100) Subject: ac-power: define main through macro X-Git-Tag: v240~280^2~28 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=9a5cedc319d56bbf3e65046e8e73ee94612f0fef;p=thirdparty%2Fsystemd.git ac-power: define main through macro I decided to use a separate definition for this because it's too easy to return positive from functions which don't need this distinction and only return negative on error and success otherwise. --- diff --git a/src/ac-power/ac-power.c b/src/ac-power/ac-power.c index 7ca72ef5f59..2d8cbb985fe 100644 --- a/src/ac-power/ac-power.c +++ b/src/ac-power/ac-power.c @@ -63,7 +63,7 @@ static int parse_argv(int argc, char *argv[]) { return 1; } -int main(int argc, char *argv[]) { +static int run(int argc, char *argv[]) { int r; /* This is mostly intended to be used for scripts which want @@ -74,17 +74,16 @@ int main(int argc, char *argv[]) { r = parse_argv(argc, argv); if (r <= 0) - goto finish; + return r; r = on_ac_power(); - if (r < 0) { - log_error_errno(r, "Failed to read AC status: %m"); - goto finish; - } + if (r < 0) + return log_error_errno(r, "Failed to read AC status: %m"); if (arg_verbose) puts(yes_no(r)); -finish: - return r < 0 ? EXIT_FAILURE : !r; + return r == 0; } + +DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run); diff --git a/src/basic/macro.h b/src/basic/macro.h index cf41a5ee8a1..dedcba22e7e 100644 --- a/src/basic/macro.h +++ b/src/basic/macro.h @@ -515,4 +515,14 @@ static inline int __coverity_check__(int condition) { return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; \ } +/* Zero is mapped to EXIT_SUCCESS, and both negative and positive values + * are mapped to EXIT_FAILURE. + * Note: this means "true" maps to EXIT_FAILURE. */ +#define DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(impl) \ + int main(int argc, char *argv[]) { \ + int r; \ + r = impl(argc, argv); \ + return r != 0 ? EXIT_FAILURE : EXIT_SUCCESS; \ + } + #include "log.h"