]> git.ipfire.org Git - thirdparty/mtr.git/commitdiff
cleanup: move generic utility functions to a separate file
authorSami Kerola <kerolasa@iki.fi>
Mon, 29 Aug 2016 17:02:15 +0000 (18:02 +0100)
committerSami Kerola <kerolasa@iki.fi>
Mon, 29 Aug 2016 17:10:16 +0000 (18:10 +0100)
The utils.c is basically an internal general purpose function library.

Makefile.am
mtr.c
utils.c [new file with mode: 0644]
utils.h [new file with mode: 0644]

index 1a37a97316d0e7b87d03f48331534269c249bfd8..2ba496a3c8ad639a718e234d97312ecf6c27c9aa 100644 (file)
@@ -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 e210593c1ac0dff226937b6122170e857200b178..2f6ea2135f6857f910691826f09e39836da0b92f 100644 (file)
--- 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 <getopt.h>
@@ -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 (file)
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 <ctype.h>
+#include <errno.h>
+#include <limits.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+
+#ifdef HAVE_ERROR_H
+# include <error.h>
+#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 (file)
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);