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.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2018-11-16 10:39:57 +01:00
parent a974a6569e
commit 9a5cedc319
2 changed files with 17 additions and 8 deletions

View file

@ -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);

View file

@ -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"