commit 3fe47cad77e8081153e2995cd1c369af405eedef
parent 6da6771b247ef295105099dd62297bad82bfd502
Author: Louis Burda <quent.burda@gmail.com>
Date: Thu, 22 Jun 2023 17:52:39 +0200
Add license and fix splicing
Diffstat:
2 files changed, 31 insertions(+), 12 deletions(-)
diff --git a/LICENSE b/LICENSE
@@ -0,0 +1,21 @@
+MIT License
+
+Copyright (c) 2023 Louis Burda
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
diff --git a/splice.c b/splice.c
@@ -3,6 +3,8 @@
#include <stdint.h>
#include <stdlib.h>
+#define MIN(a, b) ((a) < (b) ? (a) : (b))
+
int
main(int argc, const char **argv)
{
@@ -14,7 +16,7 @@ main(int argc, const char **argv)
int ret;
if (argc < 3 || argc > 4) {
- printf("USAGE: fwpatch MAIN START [END]\n");
+ fprintf(stderr, "USAGE: splice MAIN START [END]\n");
return 0;
}
@@ -28,27 +30,23 @@ main(int argc, const char **argv)
if (argc == 4) {
end = strtoll(argv[3], &endc, 0);
if (endc && *endc) err(1, "strtoll %s", argv[3]);
- if (start >= end) errx(1, "invalid length");
+ if (start >= end) errx(1, "invalid end");
} else {
end = -1;
}
pos = 0;
while (!feof(main_file) || !feof(stdin)) {
- if (pos >= start && !feof(stdin)) {
- nread = fread(buf, 1, BUFSIZ, stdin);
+ if (pos >= start && (pos < end || !feof(stdin))) {
+ if (feof(stdin)) errx(1, "input truncated");
+ nreq = end >= 0 ? MIN(BUFSIZ, end - start) : BUFSIZ;
+ nread = fread(buf, 1, nreq, stdin);
if (pos == start && end >= 0)
- fseek(main_file, end, SEEK_CUR);
+ fseek(main_file, end, SEEK_SET);
else if (end < 0)
fseek(main_file, nread, SEEK_CUR);
} else {
- if (start > pos) {
- nreq = start - pos;
- if (nreq > BUFSIZ)
- nreq = BUFSIZ;
- } else {
- nreq = BUFSIZ;
- }
+ nreq = pos < start ? MIN(BUFSIZ, start - pos) : BUFSIZ;
nread = fread(buf, 1, nreq, main_file);
}
if (fwrite(buf, 1, nread, stdout) != nread)