]> The Tcpdump Group git mirrors - tcpdump/blobdiff - tcpdump.c
Address Michael's comments.
[tcpdump] / tcpdump.c
index 9ddaeaf8af821f7c60d3435148ee32a9e3a5b73b..59aae52764ac6750def856e052e381c041d6199f 100644 (file)
--- a/tcpdump.c
+++ b/tcpdump.c
@@ -630,6 +630,40 @@ static int tcpdump_printf(netdissect_options *ndo _U_,
   return ret;
 }
 
+struct print_info get_print_info(int type) {
+       struct print_info printinfo;
+
+       printinfo.ndo_type = 1;
+       printinfo.ndo = gndo;
+       printinfo.p.ndo_printer = lookup_ndo_printer(type);
+       if (printinfo.p.ndo_printer == NULL) {
+               printinfo.p.printer = lookup_printer(type);
+               printinfo.ndo_type = 0;
+               if (printinfo.p.printer == NULL) {
+                       gndo->ndo_dltname = pcap_datalink_val_to_name(type);
+                       if (gndo->ndo_dltname != NULL)
+                               error("packet printing is not supported for link type %s: use -w",
+                                     gndo->ndo_dltname);
+                       else
+                               error("packet printing is not supported for link type %d: use -w", type);
+               }
+       }
+       return (printinfo);
+}
+
+char *get_next_file(FILE *VFile, char *ptr) {
+       char *ret;
+
+       ret = fgets(ptr, NAME_MAX, VFile);
+       if (!ret)
+               return NULL;
+
+       if (ptr[strlen(ptr) - 1] == '\n')
+               ptr[strlen(ptr) - 1] = '\0';
+
+       return ret;
+}
+
 int
 main(int argc, char **argv)
 {
@@ -1117,19 +1151,16 @@ main(int argc, char **argv)
 #endif /* WIN32 */
                if (VFileName != NULL) {
                        if (VFileName[0] == '-' && VFileName[1] == '\0')
-                               VFile = fopen("/dev/stdin", "r");
+                               VFile = stdin;
                        else
                                VFile = fopen(VFileName, "r");
 
                        if (VFile == NULL)
                                error("Unable to open file: %s\n", strerror(errno));
 
-                       ret = fgets(VFileLine, NAME_MAX, VFile);
+                       ret = get_next_file(VFile, VFileLine);
                        if (!ret)
-                               error("Nothing in %s\n", VFile);
-
-                       if (VFileLine[strlen(VFileLine) - 1] == '\n')
-                               VFileLine[strlen(VFileLine) - 1] = '\0';
+                               error("Nothing in %s\n", VFileName);
                        RFileName = VFileLine;
                }
 
@@ -1414,21 +1445,7 @@ main(int argc, char **argv)
 #endif
        } else {
                type = pcap_datalink(pd);
-                printinfo.ndo_type = 1;
-                printinfo.ndo = gndo;
-               printinfo.p.ndo_printer = lookup_ndo_printer(type);
-                if (printinfo.p.ndo_printer == NULL) {
-                        printinfo.p.printer = lookup_printer(type);
-                        printinfo.ndo_type = 0;
-                        if (printinfo.p.printer == NULL) {
-                                gndo->ndo_dltname = pcap_datalink_val_to_name(type);
-                                if (gndo->ndo_dltname != NULL)
-                                        error("packet printing is not supported for link type %s: use -w",
-                                              gndo->ndo_dltname);
-                                else
-                                        error("packet printing is not supported for link type %d: use -w", type);
-                        }
-                }
+               printinfo = get_print_info(type);
                callback = print_packet;
                pcap_userdata = (u_char *)&printinfo;
        }
@@ -1512,10 +1529,8 @@ main(int argc, char **argv)
                }
                pcap_close(pd);
                if (VFileName != NULL) {
-                       ret = fgets(VFileLine, NAME_MAX, VFile);
+                       ret = get_next_file(VFile, VFileLine);
                        if (ret) {
-                               if (VFileLine[strlen(VFileLine) - 1] == '\n')
-                                       VFileLine[strlen(VFileLine) - 1] = '\0';
                                RFileName = VFileLine;
                                pd = pcap_open_offline(RFileName, ebuf);
                                if (pd == NULL)
@@ -1523,6 +1538,7 @@ main(int argc, char **argv)
                                new_dlt = pcap_datalink(pd);
                                if (WFileName && new_dlt != dlt)
                                        error("%s: new dlt does not match original", RFileName);
+                               printinfo = get_print_info(new_dlt);
                                dlt_name = pcap_datalink_val_to_name(new_dlt);
                                if (dlt_name == NULL) {
                                        fprintf(stderr, "reading from file %s, link-type %u\n",