summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Wu <lekensteyn@gmail.com>2013-10-23 21:53:42 +0200
committerPeter Wu <lekensteyn@gmail.com>2013-10-23 21:53:42 +0200
commit5105f55149abd5ccd384e149f4de0c27ad4c1c8d (patch)
treef37e25f77ec0a6a83e07fa0d97259be554917337
parent736cc9c6e9e872a4a8689ff02d1b5dd4d920b98b (diff)
downloadc-files-5105f55149abd5ccd384e149f4de0c27ad4c1c8d.tar.gz
slice: support omitting last byte
-rw-r--r--slice.c21
1 files changed, 14 insertions, 7 deletions
diff --git a/slice.c b/slice.c
index ec4abd2..119ecef 100644
--- a/slice.c
+++ b/slice.c
@@ -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);