diff options
author | Peter Wu <lekensteyn@gmail.com> | 2013-10-23 21:53:42 +0200 |
---|---|---|
committer | Peter Wu <lekensteyn@gmail.com> | 2013-10-23 21:53:42 +0200 |
commit | 5105f55149abd5ccd384e149f4de0c27ad4c1c8d (patch) | |
tree | f37e25f77ec0a6a83e07fa0d97259be554917337 | |
parent | 736cc9c6e9e872a4a8689ff02d1b5dd4d920b98b (diff) | |
download | c-files-5105f55149abd5ccd384e149f4de0c27ad4c1c8d.tar.gz |
slice: support omitting last byte
-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); |