aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorGravatar Tim Segers <tsegers@pm.me>2023-01-05 09:49:18 +0100
committerGravatar Tim Segers <tsegers@pm.me>2023-01-07 15:24:51 +0100
commitb65d350ab6cdc41d1ef09abbe08b093dfea92057 (patch)
treeba606511c6da378eabb6e904c72f8b4371829793
parent5b8235cb3cbf04e903ca0ab337ad57a52e450dd2 (diff)
downloadopendeco-b65d350ab6cdc41d1ef09abbe08b093dfea92057.tar.gz
Return errno.h error codes instead of -1 on fail
-rw-r--r--src/opendeco-cli.c6
-rw-r--r--src/opendeco-conf.c5
-rw-r--r--src/opendeco.c14
-rw-r--r--src/output.c5
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 <stdio.h>
#include <stdlib.h>
#include <wchar.h>
+#include <errno.h>
#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 <errno.h>
#include <locale.h>
#include <math.h>
+#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
@@ -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 <math.h>
#include <stdio.h>
#include <string.h>
+#include <errno.h>
#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;