diff options
-rw-r--r-- | editcap.c | 41 | ||||
-rw-r--r-- | wsutil/clopts_common.c | 34 | ||||
-rw-r--r-- | wsutil/clopts_common.h | 6 |
3 files changed, 50 insertions, 31 deletions
@@ -286,10 +286,7 @@ add_selection(char *sel, guint* max_selection) fprintf(stderr, "Not inclusive ..."); selectfrm[max_selected].inclusive = FALSE; - if (!ws_strtou32(sel, NULL, &selectfrm[max_selected].first)) { - fprintf(stderr, "editcap: invalid integer conversion: %s\n", sel); - return FALSE; - } + selectfrm[max_selected].first = get_guint32(sel, "packet number"); if (selectfrm[max_selected].first > *max_selection) *max_selection = selectfrm[max_selected].first; @@ -301,14 +298,8 @@ add_selection(char *sel, guint* max_selection) next = locn + 1; selectfrm[max_selected].inclusive = TRUE; - if (!ws_strtou32(sel, NULL, &selectfrm[max_selected].first)) { - fprintf(stderr, "editcap: invalid integer conversion: %s\n", sel); - return FALSE; - } - if (!ws_strtou32(next, NULL, &selectfrm[max_selected].first)) { - fprintf(stderr, "editcap: invalid integer conversion: %s\n", sel); - return FALSE; - } + selectfrm[max_selected].first = get_guint32(sel, "beginning of packet range"); + selectfrm[max_selected].second = get_guint32(sel, "end of packet range"); if (selectfrm[max_selected].second == 0) { @@ -1113,11 +1104,7 @@ main(int argc, char *argv[]) } case 'c': - if (!ws_strtou32(optarg, NULL, &split_packet_count) || split_packet_count == 0) { - fprintf(stderr, "editcap: \"%s\" isn't a valid packet count\n", - optarg); - exit(1); - } + split_packet_count = get_nonzero_guint32(optarg, "packet count"); break; case 'C': @@ -1165,7 +1152,8 @@ main(int argc, char *argv[]) case 'D': dup_detect = TRUE; dup_detect_by_time = FALSE; - if (!ws_strtou32(optarg, NULL, &dup_window) || dup_window > MAX_DUP_DEPTH) { + dup_window = get_guint32(optarg, "duplicate window"); + if (dup_window > MAX_DUP_DEPTH) { fprintf(stderr, "editcap: \"%d\" duplicate window value must be between 0 and %d inclusive.\n", dup_window, MAX_DUP_DEPTH); exit(1); @@ -1214,10 +1202,7 @@ main(int argc, char *argv[]) break; case 'o': - if (!ws_strtou32(optarg, NULL, &change_offset) || change_offset == 0) { - fprintf(stderr, "editcap: invalid offset %s\n", optarg); - exit(1); - } + change_offset = get_nonzero_guint32(optarg, "change offset"); break; case 'r': @@ -1225,11 +1210,7 @@ main(int argc, char *argv[]) break; case 's': - if (!ws_strtou32(optarg, NULL, &snaplen)) { - fprintf(stderr, "editcap: \"%s\" isn't a valid snapshot length\n", - optarg); - exit(1); - } + snaplen = get_nonzero_guint32(optarg, "snapshot length"); break; case 'S': @@ -1413,7 +1394,7 @@ main(int argc, char *argv[]) if (nstime_is_unset(&block_start)) { block_start = phdr->ts; } - if (secs_per_block > 0) { + if (secs_per_block != 0) { while ((phdr->ts.secs - block_start.secs > secs_per_block) || (phdr->ts.secs - block_start.secs == secs_per_block && phdr->ts.nsecs >= block_start.nsecs )) { /* time for the next file */ @@ -1444,9 +1425,9 @@ main(int argc, char *argv[]) } } /* time stamp handling */ - if (split_packet_count > 0) { + if (split_packet_count != 0) { /* time for the next file? */ - if (written_count > 0 && written_count % split_packet_count == 0) { + if (written_count > 0 && (written_count % split_packet_count) == 0) { if (!wtap_dump_close(pdh, &write_err)) { fprintf(stderr, "editcap: Error writing to %s: %s\n", filename, wtap_strerror(write_err)); diff --git a/wsutil/clopts_common.c b/wsutil/clopts_common.c index 1967014c82..78763328d5 100644 --- a/wsutil/clopts_common.c +++ b/wsutil/clopts_common.c @@ -56,7 +56,6 @@ get_natural_int(const char *string, const char *name) return (int)number; } - int get_positive_int(const char *string, const char *name) { @@ -72,6 +71,39 @@ get_positive_int(const char *string, const char *name) return number; } +guint32 +get_guint32(const char *string, const char *name) +{ + const char *end; + guint32 number; + + if (!ws_strtou32(string, &end, &number)) { + if (errno == EINVAL || *end != '\0') { + cmdarg_err("The specified %s \"%s\" isn't a decimal number", name, string); + exit(1); + } + cmdarg_err("The specified %s \"%s\" is too large (greater than %d)", + name, string, number); + exit(1); + } + return number; +} + +guint32 +get_nonzero_guint32(const char *string, const char *name) +{ + guint32 number; + + number = get_guint32(string, name); + + if (number == 0) { + cmdarg_err("The specified %s is zero", name); + exit(1); + } + + return number; +} + /* * Editor modelines - http://www.wireshark.org/tools/modelines.html * diff --git a/wsutil/clopts_common.h b/wsutil/clopts_common.h index c409e8957a..ca0bb7bc33 100644 --- a/wsutil/clopts_common.h +++ b/wsutil/clopts_common.h @@ -35,6 +35,12 @@ get_natural_int(const char *string, const char *name); WS_DLL_PUBLIC int get_positive_int(const char *string, const char *name); +WS_DLL_PUBLIC guint32 +get_guint32(const char *string, const char *name); + +WS_DLL_PUBLIC guint32 +get_nonzero_guint32(const char *string, const char *name); + #ifdef __cplusplus } #endif /* __cplusplus */ |