more argument polishing
[rrq/fusefile.git] / fusefile.c
index 5a51066232251a3db09dc310df5abdd4fbb6c043..0936da92219a0e5877679c1bdbae2fc91d1680e6 100644 (file)
@@ -63,8 +63,8 @@ static void print_source(struct Source *p) {
 
 static char *range;
 static unsigned int c;
-static int RANGE(int SCAN,int N ) {
-    return ( SCAN == N ) && *(range+c) == 0;
+static int RANGE(int s,int n ) {
+    return ( s == n ) && *(range+c) == 0;
 }
 
 static int setup_sources(char **argv,int i,int n) {
@@ -102,28 +102,47 @@ static int setup_sources(char **argv,int i,int n) {
        p->to = filestat.st_size;
        // Process any range variation
        if ( range && *(++range) ) {
-           unsigned int a,b;
-           if ( RANGE( sscanf( range, "%u-%u%n", &a, &b, &c ), 2 ) ) {
-               p->from = a;
-               p->to = b;
-           } else if ( RANGE( sscanf( range, "%u--%u%n", &a, &b, &c ), 2 ) ) {
-               p->from = a;
-               p->to -= b;
-           } else if ( RANGE( sscanf( range, "%u-%n", &a, &c ), 1 ) ) {
-               p->from = a;
-           } else if ( RANGE( sscanf( range, "%u%n", &a, &c ), 1 ) ) {
-               p->from = a;
-           } else if ( RANGE( sscanf( range, "-%u%n", &b, &c ), 1 ) ) {
-               p->to = b;
-           } else if ( RANGE( sscanf( range, "--%u%n", &b, &c ), 1 ) ) {
-               p->to -= b;
-           } else if ( RANGE( sscanf( range, "-%n", &c), 0 ) ) {
-               // Acceptable as "start-end" range
+           int a,b;
+           if ( 0 ) {
+           } else if ( RANGE( sscanf( range, "%d:%d%n", &a, &b, &c ), 2 )) {
+               p->from = ( a < 0 )? ( p->to + a ) : a;
+               p->to = ( b < 0 )? ( p->to + b ) : b;
+           } else if ( RANGE( sscanf( range, "%d+%d%n", &a, &b, &c ), 2 )) {
+               p->from = ( a < 0 )? ( p->to + a ) : a;
+               p->to = ( ( b < 0 )? p->to : p->from ) + b;
+           } 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", &b, &c ), 1 )) {
+               p->to = ( b < 0 )? ( p->to + b ) : b;
+           } 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 = a;
+               } else {
+                   p->from = p->to + a;
+               }
+           } else if ( RANGE( sscanf( range, ":%n", &c), 0 ) ) {
+               // to end from start
            } else {
                fprintf( stderr, "** BAD RANGE: %s\n", argv[i] );
                return 1;
            }
        }
+       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;
+       }
        p->start = sources.size; // the fusefile position of fragment
        sources.size += p->to - p->from;
 #if DEBUG