aboutsummaryrefslogtreecommitdiffstats
path: root/wsutil/clopts_common.c
diff options
context:
space:
mode:
authorJoão Valverde <j@v6e.pt>2023-02-06 22:50:32 +0000
committerJoão Valverde <j@v6e.pt>2023-02-10 20:59:22 +0000
commitcf8107eb2a93ec91d36cdb6efbb6195a77616b41 (patch)
tree823c97bc8c3f9fd37f1f8243df2e52e8e05d3fcb /wsutil/clopts_common.c
parent0cea64a632167e497a8a56757b1e4a70e9202e3b (diff)
Move ui/clopts_common.[ch] to wsutil
Diffstat (limited to 'wsutil/clopts_common.c')
-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 0000000000..8145bfb8b5
--- /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 <stdlib.h>
+#include <errno.h>
+
+#include <wsutil/strtoi.h>
+#include <wsutil/cmdarg_err.h>
+
+#include "clopts_common.h"
+
+int
+get_natural_int(const char *string, const char *name)
+{
+ gint32 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;
+}
+
+guint32
+get_guint32(const char *string, const char *name)
+{
+ guint32 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;
+}
+
+guint32
+get_nonzero_guint32(const char *string, const char *name)
+{
+ guint32 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;
+}