summaryrefslogtreecommitdiffstats
path: root/wsutil/clopts_common.c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-10 20:34:10 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-10 20:34:10 +0000
commite4ba6dbc3f1e76890b22773807ea37fe8fa2b1bc (patch)
tree68cb5ef9081156392f1dd62a00c6ccc1451b93df /wsutil/clopts_common.c
parentInitial commit. (diff)
downloadwireshark-e4ba6dbc3f1e76890b22773807ea37fe8fa2b1bc.tar.xz
wireshark-e4ba6dbc3f1e76890b22773807ea37fe8fa2b1bc.zip
Adding upstream version 4.2.2.upstream/4.2.2
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r--wsutil/clopts_common.c108
1 files changed, 108 insertions, 0 deletions
diff --git a/wsutil/clopts_common.c b/wsutil/clopts_common.c
new file mode 100644
index 00000000..fa17f8e3
--- /dev/null
+++ b/wsutil/clopts_common.c
@@ -0,0 +1,108 @@
+/* clopts_common.c
+ * Handle command-line arguments common to various programs
+ *
+ * Wireshark - Network traffic analyzer
+ * By Gerald Combs <gerald@wireshark.org>
+ * Copyright 1998 Gerald Combs
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "config.h"
+
+#include "clopts_common.h"
+
+#include <stdlib.h>
+#include <errno.h>
+
+#include <wsutil/strtoi.h>
+#include <wsutil/cmdarg_err.h>
+
+int
+get_natural_int(const char *string, const char *name)
+{
+ int32_t number;
+
+ if (!ws_strtoi32(string, NULL, &number)) {
+ if (errno == EINVAL) {
+ cmdarg_err("The specified %s \"%s\" isn't a decimal number", name, string);
+ exit(1);
+ }
+ if (number < 0) {
+ cmdarg_err("The specified %s \"%s\" is a negative number", name, string);
+ exit(1);
+ }
+ cmdarg_err("The specified %s \"%s\" is too large (greater than %d)",
+ name, string, number);
+ exit(1);
+ }
+ if (number < 0) {
+ cmdarg_err("The specified %s \"%s\" is a negative number", name, string);
+ exit(1);
+ }
+ return (int)number;
+}
+
+int
+get_positive_int(const char *string, const char *name)
+{
+ int number;
+
+ number = get_natural_int(string, name);
+
+ if (number == 0) {
+ cmdarg_err("The specified %s is zero", name);
+ exit(1);
+ }
+
+ return number;
+}
+
+uint32_t
+get_guint32(const char *string, const char *name)
+{
+ uint32_t number;
+
+ if (!ws_strtou32(string, NULL, &number)) {
+ if (errno == EINVAL) {
+ cmdarg_err("The specified %s \"%s\" isn't a decimal number", name, string);
+ exit(1);
+ }
+ cmdarg_err("The specified %s \"%s\" is too large (greater than %d)",
+ name, string, number);
+ exit(1);
+ }
+ return number;
+}
+
+uint32_t
+get_nonzero_guint32(const char *string, const char *name)
+{
+ uint32_t number;
+
+ number = get_guint32(string, name);
+
+ if (number == 0) {
+ cmdarg_err("The specified %s is zero", name);
+ exit(1);
+ }
+
+ return number;
+}
+
+double
+get_positive_double(const char *string, const char *name)
+{
+ double number = g_ascii_strtod(string, NULL);
+
+ if (errno == EINVAL) {
+ cmdarg_err("The specified %s \"%s\" isn't a floating point number", name, string);
+ exit(1);
+ }
+ if (number < 0.0) {
+ cmdarg_err("The specified %s \"%s\" is a negative number", name, string);
+ exit(1);
+ }
+
+ return number;
+}