From 5105f55149abd5ccd384e149f4de0c27ad4c1c8d Mon Sep 17 00:00:00 2001 From: Peter Wu Date: Wed, 23 Oct 2013 21:53:42 +0200 Subject: slice: support omitting last byte --- slice.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) (limited to 'slice.c') diff --git a/slice.c b/slice.c index ec4abd2..119ecef 100644 --- a/slice.c +++ b/slice.c @@ -11,15 +11,22 @@ #include 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); -- cgit v1.2.1