changeset 1074:4ed442cf25c0

misc: remove usage of BSD strtonum
author David Demelier <markand@malikania.fr>
date Tue, 13 Jul 2021 20:55:52 +0200
parents f18988efb96b
children 69b90a8d4d35
files GNUmakefile INSTALL.md irccd/peer.c irccdctl/irccdctl.c lib/irccd/util.c lib/irccd/util.h
diffstat 6 files changed, 41 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/GNUmakefile	Tue Jul 13 20:41:37 2021 +0200
+++ b/GNUmakefile	Tue Jul 13 20:55:52 2021 +0200
@@ -161,7 +161,6 @@
 INCS+=          -Iextern/libgreatest/
 INCS+=          -Iextern/libketopt/
 INCS+=          -Iextern/libutlist/
-INCS+=          $(shell pkg-config --silence-errors --cflags libbsd-overlay)
 
 ifeq (${JS},1)
 INCS+=          -Iextern/libduktape
@@ -176,8 +175,6 @@
 CFLAGS+=        -fPIC
 endif
 
-LIBS+=          $(shell pkg-config --silence-errors --libs libbsd-overlay)
-
 ifeq (${SSL},1)
 LIBS+=          -lssl -lcrypto
 endif
--- a/INSTALL.md	Tue Jul 13 20:41:37 2021 +0200
+++ b/INSTALL.md	Tue Jul 13 20:55:52 2021 +0200
@@ -6,10 +6,6 @@
 Requirements
 ------------
 
-Runtime dependencies:
-
-- [libbsd][]: some BSD extensions.
-
 Build dependencies:
 
 - C99 and few features from C11 (stdatomics.h, stdnoreturn.h).
--- a/irccd/peer.c	Tue Jul 13 20:41:37 2021 +0200
+++ b/irccd/peer.c	Tue Jul 13 20:55:52 2021 +0200
@@ -382,11 +382,10 @@
 static int
 cmd_rule_add(struct peer *p, char *line)
 {
-	const char *errstr;
 	char *token, *ptr, *dst, key;
 	enum irc_rule_action act;
 	struct irc_rule *rule;
-	size_t index = -1;
+	unsigned long long index = -1;
 
 	if (sscanf(line, "RULE-ADD %*s") == EOF)
 		return EINVAL;
@@ -424,7 +423,7 @@
 			dst = rule->events;
 			break;
 		case 'i':
-			if ((index = strtonum(token + 2, 0, LLONG_MAX, &errstr)) == 0 && errstr)
+			if (irc_util_stou(token + 2, &index) < 0)
 				goto fail;
 			break;
 		case 'o':
@@ -585,14 +584,14 @@
 static int
 cmd_rule_move(struct peer *p, char *line)
 {
-	const char *args[2], *errstr;
+	const char *args[2];
 	unsigned long long from, to;
 
 	if (parse(line, args, 2) != 2)
 		return EINVAL;
-	if ((from = strtonum(args[0], 0, LLONG_MAX, &errstr)) == 0 && errstr)
+	if (irc_util_stou(args[0], &from) < 0)
 		return ERANGE;
-	if ((to = strtonum(args[1], 0, LLONG_MAX, &errstr)) == 0 && errstr)
+	if (irc_util_stou(args[1], &to) < 0)
 		return ERANGE;
 	if (from >= irc_bot_rule_size())
 		return ERANGE;
@@ -609,14 +608,11 @@
 cmd_rule_remove(struct peer *p, char *line)
 {
 	const char *args[1] = {0};
-	size_t index;
+	unsigned long long index;
 
 	if (parse(line, args, 1) != 1)
 		return EINVAL;
-
-	index = strtoull(args[0], NULL, 10);
-
-	if (index >= irc_bot_rule_size())
+	if (irc_util_stou(args[0], &index) < 0 || index >= irc_bot_rule_size())
 		return ERANGE;
 
 	irc_bot_rule_remove(index);
--- a/irccdctl/irccdctl.c	Tue Jul 13 20:41:37 2021 +0200
+++ b/irccdctl/irccdctl.c	Tue Jul 13 20:55:52 2021 +0200
@@ -625,12 +625,11 @@
 	(void)argc;
 
 	long long from, to;
-	const char *errstr;
 
-	if ((from = strtonum(argv[0], 0, LLONG_MAX, &errstr)) == 0 && errstr)
-		irc_util_die("abort: %s: %s\n", argv[0], errstr);
-	if ((to = strtonum(argv[1], 0, LLONG_MAX, &errstr)) == 0 && errstr)
-		irc_util_die("abort: %s: %s\n", argv[1], errstr);
+	if (irc_util_stoi(argv[0], &from) < 0)
+		irc_util_die("abort: %s: %s\n", argv[0], errno);
+	if (irc_util_stoi(argv[1], &to) < 0)
+		irc_util_die("abort: %s: %s\n", argv[1], errno);
 
 	req("RULE-MOVE %lld %lld", from, to);
 	ok();
@@ -975,8 +974,6 @@
 {
 	ketopt_t ko = KETOPT_INIT;
 
-	setprogname("irccdctl");
-
 	--argc;
 	++argv;
 
--- a/lib/irccd/util.c	Tue Jul 13 20:41:37 2021 +0200
+++ b/lib/irccd/util.c	Tue Jul 13 20:55:52 2021 +0200
@@ -179,3 +179,27 @@
 	va_end(ap);
 	exit(1);
 }
+
+int
+irc_util_stoi(const char *s, long long *i)
+{
+	assert(s);
+	assert(i);
+
+	errno = 0;
+	*i = strtoll(s, NULL, 10);
+
+	return errno == 0 ? 0 : -1;
+}
+
+int
+irc_util_stou(const char *s, unsigned long long *u)
+{
+	assert(s);
+	assert(u);
+
+	errno = 0;
+	*u = strtoull(s, NULL, 10);
+
+	return errno == 0 ? 0 : -1;
+}
--- a/lib/irccd/util.h	Tue Jul 13 20:41:37 2021 +0200
+++ b/lib/irccd/util.h	Tue Jul 13 20:55:52 2021 +0200
@@ -67,6 +67,12 @@
 noreturn void
 irc_util_die(const char *, ...);
 
+int
+irc_util_stoi(const char *, long long *);
+
+int
+irc_util_stou(const char *, unsigned long long *);
+
 #if defined(__cplusplus)
 }
 #endif