From: Sami Kerola Date: Mon, 29 Aug 2016 17:02:15 +0000 (+0100) Subject: cleanup: move generic utility functions to a separate file X-Git-Tag: v0.88~26^2~22 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5226f921d7e71b8120c8c7f0429e3da2ddfebb15;p=thirdparty%2Fmtr.git cleanup: move generic utility functions to a separate file The utils.c is basically an internal general purpose function library. --- diff --git a/Makefile.am b/Makefile.am index 1a37a97..2ba496a 100644 --- a/Makefile.am +++ b/Makefile.am @@ -31,6 +31,7 @@ mtr_SOURCES = mtr.c mtr.h \ display.c display.h \ report.c report.h \ select.c select.h \ + utils.c utils.h \ mtr-curses.h \ img/mtr_icon.xpm \ mtr-gtk.h diff --git a/mtr.c b/mtr.c index e210593..2f6ea21 100644 --- a/mtr.c +++ b/mtr.c @@ -51,6 +51,7 @@ #include "report.h" #include "net.h" #include "asn.h" +#include "utils.h" #ifdef HAVE_GETOPT #include @@ -145,70 +146,6 @@ static void __attribute__((__noreturn__)) usage(FILE *out) exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS); } -enum { - STRTO_INT, - STRTO_U32INT -}; - -/* Parse string, and return positive signed int. */ -static int -strtonum_or_err (const char *str, const char *errmesg, const int type) -{ - unsigned long int num; - char *end = NULL; - - if (str != NULL && *str != '\0') { - errno = 0; - num = strtoul (str, &end, 10); - if (errno == 0 && str != end && end != NULL && *end == '\0') { - switch (type) { - case STRTO_INT: - if (num < INT_MAX) - return num; - break; - case STRTO_U32INT: - if (num < UINT32_MAX) - return num; - break; - } - } - } - error (EXIT_FAILURE, errno, "%s: '%s'", errmesg, str); - return 0; -} - -static float -strtofloat_or_err (const char *str, const char *errmesg) -{ - double num; - char *end = NULL; - - if (str != NULL && *str != '\0') { - errno = 0; - num = strtod (str, &end); - if (errno == 0 && str != end && end != NULL && *end == '\0' -#ifdef FLT_MAX - && num < FLT_MAX -#endif - ) - return num; - } - error (EXIT_FAILURE, errno, "%s: '%s'", errmesg, str); - return 0; -} - - -extern char * -trim(char * s) { - - char * p = s; - int l = strlen(p); - - while(isspace(p[l-1]) && l) p[--l] = 0; - while(*p && isspace(*p) && l) ++p, --l; - - return p; -} static void append_to_names(const char* item) { diff --git a/utils.c b/utils.c new file mode 100644 index 0000000..b1fef50 --- /dev/null +++ b/utils.c @@ -0,0 +1,90 @@ +/* + mtr -- a network diagnostic tool + Copyright (C) 1997,1998 Matt Kimball + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License version 2 as + published by the Free Software Foundation. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +#include +#include +#include +#include +#include +#include + +#ifdef HAVE_ERROR_H +# include +#else +# include "portability/error.h" +#endif + +#include "utils.h" + +extern char *trim(char *s) +{ + char *p = s; + int l = strlen(p); + + while (isspace(p[l - 1]) && l) + p[--l] = 0; + while (*p && isspace(*p) && l) + ++p, --l; + + return p; +} + +/* Parse string, and return positive signed int. */ +extern int strtonum_or_err(const char *str, const char *errmesg, const int type) +{ + unsigned long int num; + char *end = NULL; + + if (str != NULL && *str != '\0') { + errno = 0; + num = strtoul(str, &end, 10); + if (errno == 0 && str != end && end != NULL && *end == '\0') { + switch (type) { + case STRTO_INT: + if (num < INT_MAX) + return num; + break; + case STRTO_U32INT: + if (num < UINT32_MAX) + return num; + break; + } + } + } + error(EXIT_FAILURE, errno, "%s: '%s'", errmesg, str); + return 0; +} + +extern float strtofloat_or_err(const char *str, const char *errmesg) +{ + double num; + char *end = NULL; + + if (str != NULL && *str != '\0') { + errno = 0; + num = strtod(str, &end); + if (errno == 0 && str != end && end != NULL && *end == '\0' +#ifdef FLT_MAX + && num < FLT_MAX +#endif + ) + return num; + } + error(EXIT_FAILURE, errno, "%s: '%s'", errmesg, str); + return 0; +} diff --git a/utils.h b/utils.h new file mode 100644 index 0000000..4afd412 --- /dev/null +++ b/utils.h @@ -0,0 +1,27 @@ +/* + mtr -- a network diagnostic tool + Copyright (C) 1997,1998 Matt Kimball + Copyright (C) 2005 R.E.Wolff@BitWizard.nl + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License version 2 as + published by the Free Software Foundation. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +enum { + STRTO_INT, + STRTO_U32INT +}; + +extern char *trim(char *s); +extern int strtonum_or_err(const char *str, const char *errmesg, const int type); +extern float strtofloat_or_err(const char *str, const char *errmesg);