From bcaeffd9fc73b359636ff32629fc07374c50cf16 Mon Sep 17 00:00:00 2001
From: Ralph Ronnquist <ralph.ronnquist@gmail.com>
Date: Sun, 17 Jul 2022 15:01:22 +1000
Subject: [PATCH] more argument polishing

---
 fusefile.c | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)

diff --git a/fusefile.c b/fusefile.c
index 9c4b964..0936da9 100644
--- a/fusefile.c
+++ b/fusefile.c
@@ -117,10 +117,10 @@ static int setup_sources(char **argv,int i,int n) {
 	    } else if ( RANGE( sscanf( range, "%d:%n", &a, &c ), 1 )) {
 		p->from = ( a < 0 )? ( p->to + a ) : a;
 	    } else if ( RANGE( sscanf( range, "%d%n", &a, &c ), 1 )) {
-		if ( a < 0 ) {
-		    p->from = p->to + a;
+		if ( a >= 0 ) {
+		    p->from = a;
 		} else {
-		    p->to = p->from + a;
+		    p->from = p->to + a;
 		}
 	    } else if ( RANGE( sscanf( range, ":%n", &c), 0 ) ) {
 		// to end from start
@@ -129,8 +129,16 @@ static int setup_sources(char **argv,int i,int n) {
 		return 1;
 	    }
 	}
-	if ( p->from >= p->to ||
-	     p->from >= filestat.st_size || p->to > filestat.st_size ) {
+	if ( ( filestat.st_mode &  S_IFMT ) == S_IFCHR ) {
+	    filestat.st_size = p->to; // Pretend size of character device
+	}
+	if ( p->from < 0 ) {
+	    p->from = 0;
+	}
+	if ( p->to > filestat.st_size ) {
+	    p->to = filestat.st_size;
+	}
+	if ( p->from >= p->to || p->from >= filestat.st_size ) {
 	    fprintf( stderr, "** BAD RANGE: %s [%ld:%ld]\n",
 		     argv[i], p->from, p->to );
 	    return 1;
-- 
2.39.5