From b65d350ab6cdc41d1ef09abbe08b093dfea92057 Mon Sep 17 00:00:00 2001 From: Tim Segers Date: Thu, 5 Jan 2023 09:49:18 +0100 Subject: Return errno.h error codes instead of -1 on fail --- src/opendeco-cli.c | 6 ++++++ src/opendeco-conf.c | 5 +++-- src/opendeco.c | 14 +++++++++++--- src/output.c | 5 +++-- 4 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/opendeco-cli.c b/src/opendeco-cli.c index 42e3398..579e013 100644 --- a/src/opendeco-cli.c +++ b/src/opendeco-cli.c @@ -46,6 +46,12 @@ static void print_xxd_arr(char *name, unsigned char *content, unsigned int len) { char *tmp = strndup((const char *) content, len); + if (!tmp) { + errno = ENOMEM; + perror(__func__); + exit(EXIT_FAILURE); + } + wprintf(L"--------\n\n"); wprintf(L"License for: %s\n\n", name); wprintf(L"%s", tmp); diff --git a/src/opendeco-conf.c b/src/opendeco-conf.c index 977bfc5..d385bb0 100644 --- a/src/opendeco-conf.c +++ b/src/opendeco-conf.c @@ -3,6 +3,7 @@ #include #include #include +#include #include "opendeco-conf.h" #include "toml/toml.h" @@ -16,14 +17,14 @@ int opendeco_conf_parse(const char *confpath, struct arguments *arguments) fp = fopen(confpath, "r"); if (!fp) - return -1; + return -ENOENT; /* parse config */ toml_table_t *od_conf = toml_parse_file(fp, errbuf, sizeof(errbuf)); fclose(fp); if (!od_conf) - return -1; + return -EINVAL; fwprintf(stderr, L"Picked up options from %s\n", confpath); diff --git a/src/opendeco.c b/src/opendeco.c index 79b1f98..8e22e4b 100644 --- a/src/opendeco.c +++ b/src/opendeco.c @@ -1,7 +1,9 @@ /* SPDX-License-Identifier: MIT-0 */ +#include #include #include +#include #include #include #include @@ -45,7 +47,7 @@ int register_gas_use(double depth, double time, const gas_t *gas, double rmv) } } - return -1; + return -ENOSPC; } void print_gas_use(void) @@ -129,7 +131,7 @@ int parse_gasses(gas_t **gasses, char *str) if (scan_gas(&deco_gasses[gas_idx], gas_str)) { wprintf(L"Invalid gas (%s). Aborting!\n", gas_str); - exit(-1); + exit(EXIT_FAILURE); } gas_idx++; @@ -158,6 +160,12 @@ int main(int argc, char *argv[]) char *gas_default = strdup("Air"); char *decogasses_default = strdup(""); + if (!gas_default || !decogasses_default) { + errno = ENOMEM; + perror(__func__); + exit(EXIT_FAILURE); + } + struct arguments arguments = { .depth = -1, .time = -1, @@ -196,7 +204,7 @@ int main(int argc, char *argv[]) if (scan_gas(&bottom_gas, arguments.gas)) { wprintf(L"Invalid gas (%s). Aborting!\n", arguments.gas); - exit(-1); + exit(EXIT_FAILURE); } /* override oxygen mod */ diff --git a/src/output.c b/src/output.c index 6d24b65..1d51a08 100644 --- a/src/output.c +++ b/src/output.c @@ -3,6 +3,7 @@ #include #include #include +#include #include "output.h" @@ -58,10 +59,10 @@ int scan_gas(gas_t *gas, char *str) } if (o2 < 0 || he < 0) - return -1; + return -EINVAL; if (o2 + he > 100) - return -1; + return -EINVAL; *gas = gas_new(o2, he, MOD_AUTO); return 0; -- cgit v1.2.3-70-g09d2