Revert "UNUSED enc_key_id_{equal,hash}"
[wireshark-sm.git] / wsutil / clopts_common.c
blobf361b4ef44f4c548bbcacdb9bce332e49e6dd609
1 /* clopts_common.c
2 * Handle command-line arguments common to various programs
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 1998 Gerald Combs
8 * SPDX-License-Identifier: GPL-2.0-or-later
9 */
11 #include "config.h"
13 #include "clopts_common.h"
15 #include <stdlib.h>
16 #include <errno.h>
18 #include <wsutil/strtoi.h>
19 #include <wsutil/cmdarg_err.h>
21 int
22 get_natural_int(const char *string, const char *name)
24 int32_t number;
26 if (!ws_strtoi32(string, NULL, &number)) {
27 if (errno == EINVAL) {
28 cmdarg_err("The specified %s \"%s\" isn't a decimal number", name, string);
29 exit(1);
31 if (number < 0) {
32 cmdarg_err("The specified %s \"%s\" is a negative number", name, string);
33 exit(1);
35 cmdarg_err("The specified %s \"%s\" is too large (greater than %d)",
36 name, string, number);
37 exit(1);
39 if (number < 0) {
40 cmdarg_err("The specified %s \"%s\" is a negative number", name, string);
41 exit(1);
43 return (int)number;
46 int
47 get_positive_int(const char *string, const char *name)
49 int number;
51 number = get_natural_int(string, name);
53 if (number == 0) {
54 cmdarg_err("The specified %s is zero", name);
55 exit(1);
58 return number;
61 uint32_t
62 get_uint32(const char *string, const char *name)
64 uint32_t number;
66 if (!ws_strtou32(string, NULL, &number)) {
67 if (errno == EINVAL) {
68 cmdarg_err("The specified %s \"%s\" isn't a decimal number", name, string);
69 exit(1);
71 cmdarg_err("The specified %s \"%s\" is too large (greater than %d)",
72 name, string, number);
73 exit(1);
75 return number;
78 uint32_t
79 get_nonzero_uint32(const char *string, const char *name)
81 uint32_t number;
83 number = get_uint32(string, name);
85 if (number == 0) {
86 cmdarg_err("The specified %s is zero", name);
87 exit(1);
90 return number;
93 uint64_t
94 get_uint64(const char *string, const char *name)
96 uint64_t number;
98 if (!ws_strtou64(string, NULL, &number)) {
99 if (errno == EINVAL) {
100 cmdarg_err("The specified %s \"%s\" isn't a decimal number", name, string);
101 exit(1);
103 cmdarg_err("The specified %s \"%s\" is too large (greater than %" PRIu64 ")",
104 name, string, number);
105 exit(1);
107 return number;
110 uint64_t
111 get_nonzero_uint64(const char *string, const char *name)
113 uint64_t number;
115 number = get_uint64(string, name);
117 if (number == 0) {
118 cmdarg_err("The specified %s is zero", name);
119 exit(1);
122 return number;
125 double
126 get_positive_double(const char *string, const char *name)
128 double number = g_ascii_strtod(string, NULL);
130 if (errno == EINVAL) {
131 cmdarg_err("The specified %s \"%s\" isn't a floating point number", name, string);
132 exit(1);
134 if (number < 0.0) {
135 cmdarg_err("The specified %s \"%s\" is a negative number", name, string);
136 exit(1);
139 return number;