]> The Tcpdump Group git mirrors - tcpdump/commitdiff
Improve the handling of size suffixes for -C.
authorGuy Harris <[email protected]>
Sat, 10 Jul 2021 21:01:18 +0000 (14:01 -0700)
committerGuy Harris <[email protected]>
Sat, 10 Jul 2021 21:01:18 +0000 (14:01 -0700)
Support upper-case suffixes (K/M/G) as well as lower-case suffixes.

Make sure nothing *follows* the suffix.

We don't need to check for the suffix before parsing the number; the
parsing routines stop if they see a non-digit character.

tcpdump.1.in
tcpdump.c

index a6c00307356911abb09189e16d4173839bfc6afd..68564d946756f2a7d9f4d26c633a788c8d568a62 100644 (file)
@@ -268,7 +268,7 @@ flag, with a number after it, starting at 1 and continuing upward.
 The default unit of \fIfile_size\fP is millions of bytes (1,000,000 bytes,
 not 1,048,576 bytes).
 .IP
 The default unit of \fIfile_size\fP is millions of bytes (1,000,000 bytes,
 not 1,048,576 bytes).
 .IP
-By adding a suffix of k, m or g to the value, the unit
+By adding a suffix of k/K, m/M or g/G to the value, the unit
 can be changed to 1,024 (KiB), 1,048,576 (MiB), or 1,073,741,824 (GiB)
 respectively.
 .TP
 can be changed to 1,024 (KiB), 1,048,576 (MiB), or 1,073,741,824 (GiB)
 respectively.
 .TP
index 2efb549b5226faf7b8d9b2462dfe267f8d48af8d..95162ecc18165f9a4518354684698e783b94ebfd 100644 (file)
--- a/tcpdump.c
+++ b/tcpdump.c
@@ -1484,7 +1484,7 @@ main(int argc, char **argv)
        int yflag_dlt = -1;
        const char *yflag_dlt_name = NULL;
        int print = 0;
        int yflag_dlt = -1;
        const char *yflag_dlt_name = NULL;
        int print = 0;
-       long Cflagmult = 1000000;
+       long Cflagmult;
 
        netdissect_options Ndo;
        netdissect_options *ndo = &Ndo;
 
        netdissect_options Ndo;
        netdissect_options *ndo = &Ndo;
@@ -1561,26 +1561,72 @@ main(int argc, char **argv)
 
                case 'C':
                        errno = 0;
 
                case 'C':
                        errno = 0;
-                       if (optarg[strlen(optarg)-1] == 'k') {
-                               Cflagmult = 1024;
-                               optarg[strlen(optarg)-1] = '\0';
-                       }
-                       if (optarg[strlen(optarg)-1] == 'm') {
-                               Cflagmult = 1024*1024;
-                               optarg[strlen(optarg)-1] = '\0';
-                       }
-                       if (optarg[strlen(optarg)-1] == 'g') {
-                               Cflagmult = 1024*1024*1024;
-                               optarg[strlen(optarg)-1] = '\0';
-                       }
 #ifdef HAVE_PCAP_DUMP_FTELL64
                        Cflag = strtoint64_t(optarg, &endp, 10);
 #else
                        Cflag = strtol(optarg, &endp, 10);
 #endif
 #ifdef HAVE_PCAP_DUMP_FTELL64
                        Cflag = strtoint64_t(optarg, &endp, 10);
 #else
                        Cflag = strtol(optarg, &endp, 10);
 #endif
-                       if (endp == optarg || *endp != '\0' || errno != 0
-                           || Cflag <= 0)
+                       if (endp == optarg || errno != 0 || Cflag <= 0)
                                error("invalid file size %s", optarg);
                                error("invalid file size %s", optarg);
+
+                       if (*endp == '\0') {
+                               /*
+                                * There's nothing after the file size,
+                                * so the size is in units of 1 MB
+                                * (1,000,000 bytes).
+                                */
+                               Cflagmult = 1000000;
+                       } else {
+                               /*
+                                * There's something after the file
+                                * size.
+                                *
+                                * If it's a single letter, then:
+                                *
+                                *   if the letter is k or K, the size
+                                *   is in units of 1 KiB (1024 bytes);
+                                *
+                                *   if the letter is m or M, the size
+                                *   is in units of 1 MiB (1,048,576 bytes);
+                                *
+                                *   if the letter is g or G, the size
+                                *   is in units of 1 GiB (1,073,741,824 bytes).
+                                *
+                                * Otherwise, it's an error.
+                                */
+                               switch (*endp) {
+
+                               case 'k':
+                               case 'K':
+                                       Cflagmult = 1024;
+                                       break;
+
+                               case 'm':
+                               case 'M':
+                                       Cflagmult = 1024*1024;
+                                       break;
+
+                               case 'g':
+                               case 'G':
+                                       Cflagmult = 1024*1024*1024;
+                                       break;
+
+                               default:
+                                       error("invalid file size %s", optarg);
+                               }
+
+                               /*
+                                * OK, there was a letter that we treat
+                                * as a units indication; was there
+                                * anything after it?
+                                */
+                               endp++;
+                               if (*endp != '\0') {
+                                       /* Yes - error */
+                                       error("invalid file size %s", optarg);
+                               }
+                       }
+
                        /*
                         * Will multiplying it by multiplier overflow?
                         */
                        /*
                         * Will multiplying it by multiplier overflow?
                         */