]> git.ipfire.org Git - thirdparty/mtr.git/commitdiff
added --filename=FILE option
authorVaibhav Bajpai <contact@vaibhavbajpai.com>
Sun, 5 May 2013 15:09:12 +0000 (17:09 +0200)
committerR.E. Wolff <R.E.Wolff@BitWizard.nl>
Mon, 13 May 2013 15:22:54 +0000 (17:22 +0200)
service names read from this file are allocated in a linked list
multiple service names are only accepted with --csv flag
without --csv flag, the first service name is used

mtr.c
mtr.h
report.c

diff --git a/mtr.c b/mtr.c
index 7c6feeb782d9fcf5e09008c80059bd1ef9dcfcad..58671fb4b8715f63da20e0410eeca93593d977b8 100644 (file)
--- a/mtr.c
+++ b/mtr.c
@@ -3,7 +3,7 @@
     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 
+    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,
 #include <stdlib.h>
 #include <string.h>
 #include <netinet/in.h>
-#include <sys/socket.h> 
+#include <sys/socket.h>
 #include <unistd.h>
 #include <strings.h>
 #include <time.h>
+#include <errno.h>
+#include <string.h>
+#include <ctype.h>
+
 
 #include "mtr.h"
 #include "mtr-curses.h"
@@ -109,6 +113,67 @@ struct fields data_fields[MAXFLD] = {
   {'\0', NULL, NULL, NULL, 0, NULL}
 };
 
+typedef struct names {
+  char*                 name;
+  struct names*         next;
+} names_t;
+static names_t *names = NULL;
+
+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* progname, const char* item) {
+
+  names_t* name = calloc(1, sizeof(names_t));
+  if (name == NULL) {
+    fprintf(stderr, "%s: memory allocation failure\n", progname);
+    exit(EXIT_FAILURE);
+  }
+  name->name = strdup(item);
+  name->next = names;
+  names = name;
+}
+
+static void
+read_from_file(const char* progname, const char *filename) {
+
+  FILE *in;
+  char line[512];
+
+  if (! filename || strcmp(filename, "-") == 0) {
+    clearerr(stdin);
+    in = stdin;
+  } else {
+    in = fopen(filename, "r");
+    if (! in) {
+      fprintf(stderr, "%s: fopen: %s\n", progname, strerror(errno));
+      exit(EXIT_FAILURE);
+    }
+  }
+
+  while (fgets(line, sizeof(line), in)) {
+    char* name = trim(line);
+    append_to_names(progname, name);
+  }
+
+  if (ferror(in)) {
+    fprintf(stderr, "%s: ferror: %s\n", progname, strerror(errno));
+    exit(EXIT_FAILURE);
+  }
+
+  if (in != stdin) fclose(in);
+}
+
 
 void init_fld_options (void)
 {
@@ -125,7 +190,7 @@ void init_fld_options (void)
 }
 
 
-void parse_arg (int argc, char **argv) 
+void parse_arg (int argc, char **argv)
 {
   int opt;
   int i;
@@ -156,6 +221,7 @@ void parse_arg (int argc, char **argv)
     { "show-ips", 0, 0, 'b' },
     { "address", 1, 0, 'a' },
     { "first-ttl", 1, 0, 'f' },        /* -f & -m are borrowed from traceroute */
+    { "filename", 1, 0, 'F' },
     { "max-ttl", 1, 0, 'm' },
     { "udp", 0, 0, 'u' },      /* UDP (default is ICMP) */
     { "tcp", 0, 0, 'T' },      /* TCP (default is ICMP) */
@@ -248,6 +314,9 @@ void parse_arg (int argc, char **argv)
        fstTTL = 1;
       }
       break;
+    case 'F':
+      read_from_file(argv[0], optarg);
+      break;
     case 'm':
       maxTTL = atoi (optarg);
       if (maxTTL > (MaxHost - 1)) {
@@ -375,7 +444,7 @@ void parse_mtr_options (char *string)
 }
 
 
-int main(int argc, char **argv) 
+int main(int argc, char **argv)
 {
   struct hostent *  host                = NULL;
   int               net_preopen_result;
@@ -409,10 +478,10 @@ int main(int argc, char **argv)
 
   /* reset the random seed */
   srand (getpid());
-  
+
   display_detect(&argc, &argv);
 
-  /* The field options are now in a static array all together, 
+  /* The field options are now in a static array all together,
      but that requires a run-time initialization. */
   init_fld_options ();
 
@@ -420,6 +489,11 @@ int main(int argc, char **argv)
 
   parse_arg (argc, argv);
 
+  while (optind < argc) {
+    char* name = argv[optind++];
+    append_to_names(argv[0], name);
+  }
+
   /* Now that we know mtrtype we can select which socket to use */
   if (net_selectsocket() != 0) {
     fprintf( stderr, "mtr: Couldn't determine raw socket type.\n" );
@@ -434,8 +508,8 @@ int main(int argc, char **argv)
   if (PrintHelp) {
     printf("usage: %s [-hvrwctglspniuT46] [--help] [--version] [--report]\n"
           "\t\t[--report-wide] [--report-cycles=COUNT] [--curses] [--gtk]\n"
-           "\t\t[--raw] [--split] [--mpls] [--no-dns] [--show-ips]\n"
-           "\t\t[--address interface]\n" /* BL */
+           "\t\t[--csv|-C] [--raw] [--split] [--mpls] [--no-dns] [--show-ips]\n"
+           "\t\t[--address interface] [--filename=FILE|-F]\n" /* BL */
 #ifndef NO_IPINFO
            "\t\t[--ipinfo=item_no|-y item_no]\n"
            "\t\t[--aslookup|-z]\n"
@@ -447,10 +521,10 @@ int main(int argc, char **argv)
   }
 
   time_t now = time(NULL);
+  names_t* head = names;
+  while (names != NULL) {
 
-  while (optind < argc ) {
-
-    Hostname = argv[optind++];
+    Hostname = names->name;
     if (Hostname == NULL) Hostname = "localhost";
     if (gethostname(LocalHostname, sizeof(LocalHostname))) {
     strcpy(LocalHostname, "UNKNOWNHOST");
@@ -512,8 +586,8 @@ int main(int argc, char **argv)
         }
 
     if (net_set_interfaceaddress (InterfaceAddress) != 0) {
-      fprintf( stderr, "mtr: Couldn't set interface address.\n" ); 
-      exit( EXIT_FAILURE ); 
+      fprintf( stderr, "mtr: Couldn't set interface address.\n" );
+      exit( EXIT_FAILURE );
     }
 
     display_open();
@@ -526,12 +600,19 @@ int main(int argc, char **argv)
     display_close(now);
 
     if ( DisplayMode != DisplayCSV ) break;
+    else names = names->next;
 
   }
 
   net_close();
 
+  while (head != NULL) {
+    names_t* item = head;
+    free(item->name); item->name = NULL;
+    head = head->next;
+    free(item); item = NULL;
+  }
+  head=NULL;
+
   return 0;
 }
-
-
diff --git a/mtr.h b/mtr.h
index ec76190813df220d69cb8cc606df98b60efbe6df..047d31df11b797161ad8bc22c2f5814f9b628be9 100644 (file)
--- a/mtr.h
+++ b/mtr.h
@@ -72,3 +72,6 @@ extern int use_dns;
 #ifndef HAVE_SOCKLEN_T
 typedef int socklen_t; 
 #endif
+
+char *
+trim(char * s);
index dd790511146ddfe1c4aa0ec0e53934938b75f881..83fe029ac600695686664609b9957abf9997dc50 100644 (file)
--- a/report.c
+++ b/report.c
@@ -25,7 +25,6 @@
 #include <string.h>
 #include <strings.h>
 #include <time.h>
-#include <ctype.h>
 
 #include "mtr.h"
 #include "version.h"
@@ -323,27 +322,6 @@ void csv_open(void)
 {
 }
 
-
-char *trimwhitespace(char *str) {
-  char *end;
-
-  /* Trim leading space*/
-  while(isspace(*str)) str++;
-
-  if(*str == 0)  // All spaces?
-    return str;
-
-   // Trim trailing space
-   end = str + strlen(str) - 1;
-   while(end > str && isspace(*end)) end--;
-
-   // Write new null terminator
-   *(end+1) = 0;
-
-   return str;
-}
-
-
 void csv_close(time_t now)
 {
   int i, j, at, max;
@@ -364,7 +342,7 @@ void csv_close(time_t now)
     int last = net_last(at);
     if(!ipinfo_no) {
       char* fmtinfo = fmt_ipinfo(addr);
-      if (fmtinfo != NULL) fmtinfo = trimwhitespace(fmtinfo);
+      if (fmtinfo != NULL) fmtinfo = trim(fmtinfo);
       printf("MTR.%s;%lu;%s;%s;%d;%s;%s;%d", MTR_VERSION, now, "OK", Hostname,
              at+1, name, fmtinfo, last);
     } else {