Removed trailing whitespaces from .h and .c files using the
[obnox/wireshark/wip.git] / editcap.c
index d0036338682904a8d661d7b630d16df7d90e3e16..e04f57d608948866f18bf13ab24da2e57da6871b 100644 (file)
--- a/editcap.c
+++ b/editcap.c
@@ -1,7 +1,7 @@
 /* Edit capture files.  We can delete records, adjust timestamps, or
  * simply convert from one format to another format.
  *
- * $Id: editcap.c,v 1.16 2001/07/12 08:16:44 guy Exp $
+ * $Id: editcap.c,v 1.25 2002/08/02 23:35:46 jmayer Exp $
  *
  * Originally written by Richard Sharpe.
  * Improved by Guy Harris.
 #include <sys/time.h>
 #endif
 
-#ifdef HAVE_WINSOCK_H
-#include <winsock.h>
-#endif
-
 #include <string.h>
 #include "wtap.h"
 
@@ -44,7 +40,7 @@ struct select_item {
   int inclusive;
   int first, second;
 
-} select_item;
+};
 
 #define ONE_MILLION 1000000
 
@@ -53,8 +49,8 @@ struct time_adjustment {
   int is_negative;
 };
 
-struct select_item selectfrm[100];
-int max_selected = -1;
+static struct select_item selectfrm[100];
+static int max_selected = -1;
 static int count = 1;
 static int keep_em = 0;
 static int out_file_type = WTAP_FILE_PCAP;   /* default to "libpcap"   */
@@ -65,7 +61,7 @@ static struct time_adjustment time_adj = {{0, 0}, 0}; /* no adjustment */
 
 /* Add a selection item, a simple parser for now */
 
-void add_selection(char *sel) 
+static void add_selection(char *sel) 
 {
   char *locn;
   char *next;
@@ -105,7 +101,7 @@ void add_selection(char *sel)
 
 /* Was the record selected? */
 
-int selected(int recno)
+static int selected(int recno)
 {
   int i = 0;
 
@@ -137,8 +133,8 @@ typedef struct {
  */
 
 static void
-edit_callback(u_char *user, const struct wtap_pkthdr *phdr, int offset,
-    union wtap_pseudo_header *pseudo_header, const u_char *buf) 
+edit_callback(guchar *user, const struct wtap_pkthdr *phdr, long offset _U_,
+    union wtap_pseudo_header *pseudo_header, const guchar *buf) 
 {
   callback_arg *argp = (callback_arg *)user;
   int err;
@@ -216,21 +212,38 @@ set_time_adjustment(char *optarg)
   if (!optarg)
     return;
 
-  /* first collect the whole seconds */
-  val = strtol(optarg, &frac, 10);
-  if (frac == NULL || frac == optarg || val == LONG_MIN || val == LONG_MAX) {
-    fprintf(stderr, "editcap: \"%s\" is not a valid time adjustment\n",
-            optarg);
-    exit(1);
+  /* skip leading whitespace */
+  while (*optarg == ' ' || *optarg == '\t') {
+      optarg++;
+  }
+
+  /* check for a negative adjustment */
+  if (*optarg == '-') {
+      time_adj.is_negative = 1;
+      optarg++;
   }
-  if (val < 0) {
-    time_adj.is_negative = 1;
-    val = -val;
+
+  /* collect whole number of seconds, if any */
+  if (*optarg == '.') {         /* only fractional (i.e., .5 is ok) */
+      val  = 0;
+      frac = optarg;
+  } else {
+      val = strtol(optarg, &frac, 10);
+      if (frac == NULL || frac == optarg || val == LONG_MIN || val == LONG_MAX) {
+          fprintf(stderr, "editcap: \"%s\" is not a valid time adjustment\n",
+                  optarg);
+          exit(1);
+      }
+      if (val < 0) {            /* implies '--' since we caught '-' above  */
+          fprintf(stderr, "editcap: \"%s\" is not a valid time adjustment\n",
+                  optarg);
+          exit(1);
+      }
   }
   time_adj.tv.tv_sec = val;
 
   /* now collect the partial seconds, if any */
-  if (*frac != '\0') {             /* have more to string, so more to */
+  if (*frac != '\0') {             /* chars left, so get fractional part */
     val = strtol(&(frac[1]), &end, 10);
     if (*frac != '.' || end == NULL || end == frac
         || val < 0 || val > ONE_MILLION || val == LONG_MIN || val == LONG_MAX) {
@@ -255,13 +268,14 @@ set_time_adjustment(char *optarg)
   time_adj.tv.tv_usec = val;
 }
 
-void usage()
+static void usage()
 {
   int i;
   const char *string;
 
   fprintf(stderr, "Usage: editcap [-r] [-h] [-v] [-T <encap type>] [-F <capture type>]\n");
-  fprintf(stderr, "               [-s <snaplen>] <infile> <outfile> [ <record#>[-<record#>] ... ]\n");
+  fprintf(stderr, "               [-s <snaplen>] [-t <time adjustment\n");
+  fprintf(stderr, "               <infile> <outfile> [ <record#>[-<record#>] ... ]\n");
   fprintf(stderr, "  where\t-r specifies that the records specified should be kept, not deleted, \n");
   fprintf(stderr, "                           default is to delete\n");
   fprintf(stderr, "       \t-v specifies verbose operation, default is silent\n");
@@ -296,12 +310,13 @@ int main(int argc, char *argv[])
   callback_arg args;
   extern char *optarg;
   extern int optind;
-  char opt;
+  int opt;
   char *p;
+  int snapshot_length;
 
   /* Process the options first */
 
-  while ((opt = getopt(argc, argv, "T:F:rvs:t:h")) != EOF) {
+  while ((opt = getopt(argc, argv, "T:F:rvs:t:h")) !=-1) {
 
     switch (opt) {
 
@@ -397,6 +412,11 @@ int main(int argc, char *argv[])
     if (out_frame_type == -2)
       out_frame_type = wtap_file_encap(wth);
 
+    snapshot_length = wtap_snapshot_length(wth);
+    if (snapshot_length == 0) {
+      /* Snapshot length of input file not known. */
+      snapshot_length = WTAP_MAX_PACKET_SIZE;
+    }
     args.pdh = wtap_dump_open(argv[optind + 1], out_file_type,
                              out_frame_type, wtap_snapshot_length(wth), &err);
     if (args.pdh == NULL) {