diff options
Diffstat (limited to 'slice.c')
-rw-r--r-- | slice.c | 21 |
1 files changed, 14 insertions, 7 deletions
@@ -11,15 +11,22 @@ #include <stdbool.h> int main(int argc, char **argv) { - if (argc < 4) { - fprintf(stderr, "Usage: %s file offset last-byte|+length\n", *argv); + char *filename; + unsigned long seekpos, bytes_end; + if (argc < 3) { + fprintf(stderr, "Usage: %s file offset [last-exclusive-byte|+length]\n", *argv); return 1; } - char *filename = argv[1]; - unsigned long seekpos = strtoul(argv[2], NULL, 0); - unsigned long bytes_end = strtoul(argv[3], NULL, 0); + filename = argv[1]; + seekpos = strtoul(argv[2], NULL, 0); - if (*argv[3] == '+') { + if (argc >= 4) { + bytes_end = strtoul(argv[3], NULL, 0); + } else { + bytes_end = -1; + } + + if (argc >= 4 && *argv[3] == '+') { if (bytes_end + seekpos < bytes_end) { fprintf(stderr, "Integer overflow on the last byte\n"); return 1; @@ -64,7 +71,7 @@ int main(int argc, char **argv) { read_bytes += n; } while (read_bytes < bytes_end); - if (read_bytes != bytes_end) + if (bytes_end != -1UL && read_bytes != bytes_end) fprintf(stderr, "Output is truncated, missing %lu bytes.\n", bytes_end - read_bytes); |