2004-09-10 22:47:02 +00:00
|
|
|
/* clopts_common.c
|
2014-06-29 21:37:21 +00:00
|
|
|
* Handle command-line arguments common to various programs
|
2004-09-10 22:47:02 +00:00
|
|
|
*
|
2006-05-21 05:12:17 +00:00
|
|
|
* Wireshark - Network traffic analyzer
|
|
|
|
* By Gerald Combs <gerald@wireshark.org>
|
2004-09-10 22:47:02 +00:00
|
|
|
* Copyright 1998 Gerald Combs
|
|
|
|
*
|
2018-02-07 11:26:45 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later
|
2004-09-10 22:47:02 +00:00
|
|
|
*/
|
|
|
|
|
2012-09-20 01:29:52 +00:00
|
|
|
#include "config.h"
|
2004-09-10 22:47:02 +00:00
|
|
|
|
2013-07-12 01:22:35 +00:00
|
|
|
#include <stdlib.h>
|
2016-09-06 02:23:36 +00:00
|
|
|
#include <errno.h>
|
2004-09-10 22:47:02 +00:00
|
|
|
|
2016-09-06 02:23:36 +00:00
|
|
|
#include <wsutil/strtoi.h>
|
2019-01-01 00:55:23 +00:00
|
|
|
#include <ui/cmdarg_err.h>
|
2014-06-29 21:37:21 +00:00
|
|
|
|
2019-01-01 00:55:23 +00:00
|
|
|
#include "clopts_common.h"
|
2005-02-27 21:15:30 +00:00
|
|
|
|
|
|
|
int
|
2005-11-17 05:59:21 +00:00
|
|
|
get_natural_int(const char *string, const char *name)
|
2005-02-27 21:15:30 +00:00
|
|
|
{
|
2016-09-06 02:23:36 +00:00
|
|
|
gint32 number;
|
2005-02-27 21:15:30 +00:00
|
|
|
|
2016-09-12 09:22:16 +00:00
|
|
|
if (!ws_strtoi32(string, NULL, &number)) {
|
|
|
|
if (errno == EINVAL) {
|
2016-09-06 02:23:36 +00:00
|
|
|
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);
|
2005-02-27 21:15:30 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (number < 0) {
|
2005-11-17 05:59:21 +00:00
|
|
|
cmdarg_err("The specified %s \"%s\" is a negative number", name, string);
|
2005-02-27 21:15:30 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2012-12-26 05:57:06 +00:00
|
|
|
return (int)number;
|
2005-02-27 21:15:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2005-11-17 05:59:21 +00:00
|
|
|
get_positive_int(const char *string, const char *name)
|
2005-02-27 21:15:30 +00:00
|
|
|
{
|
2012-12-26 05:57:06 +00:00
|
|
|
int number;
|
2005-02-27 21:15:30 +00:00
|
|
|
|
2005-11-17 05:59:21 +00:00
|
|
|
number = get_natural_int(string, name);
|
2005-02-27 21:15:30 +00:00
|
|
|
|
|
|
|
if (number == 0) {
|
2005-11-17 05:59:21 +00:00
|
|
|
cmdarg_err("The specified %s is zero", name);
|
2005-02-27 21:15:30 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return number;
|
|
|
|
}
|
2014-10-14 15:12:16 +00:00
|
|
|
|
2016-09-08 18:26:45 +00:00
|
|
|
guint32
|
|
|
|
get_guint32(const char *string, const char *name)
|
|
|
|
{
|
|
|
|
guint32 number;
|
|
|
|
|
2016-09-12 09:22:16 +00:00
|
|
|
if (!ws_strtou32(string, NULL, &number)) {
|
|
|
|
if (errno == EINVAL) {
|
2016-09-08 18:26:45 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-10-31 10:34:35 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-10-14 15:12:16 +00:00
|
|
|
/*
|
2019-07-26 18:43:17 +00:00
|
|
|
* Editor modelines - https://www.wireshark.org/tools/modelines.html
|
2014-10-14 15:12:16 +00:00
|
|
|
*
|
|
|
|
* Local Variables:
|
|
|
|
* c-basic-offset: 2
|
|
|
|
* tab-width: 8
|
|
|
|
* indent-tabs-mode: nil
|
|
|
|
* End:
|
|
|
|
*
|
|
|
|
* ex: set shiftwidth=2 tabstop=8 expandtab:
|
|
|
|
* :indentSize=2:tabSize=8:noTabs=true:
|
|
|
|
*/
|