diff lib/lib.c @ 1070:16167a7c1b5a draft

Fix -t c0 and -J as reported by heehooman at gmail on the list. Also fix up help text, and hook up -c.
author Rob Landley <rob@landley.net>
date Mon, 16 Sep 2013 23:41:51 -0500
parents acf7bb2b99e2
children 54b2776de2f4
line wrap: on
line diff
--- a/lib/lib.c	Wed Sep 18 10:50:38 2013 -0500
+++ b/lib/lib.c	Mon Sep 16 23:41:51 2013 -0500
@@ -89,24 +89,31 @@
   return count;
 }
 
+// skip this many bytes of input. Return 0 for success, >0 means this much
+// left after input skipped.
 off_t lskip(int fd, off_t offset)
 {
-  off_t and = lseek(fd, offset, SEEK_CUR);
+  off_t cur = lseek(fd, 0, SEEK_CUR);
+
+  if (cur != -1) {
+    off_t end = lseek(fd, 0, SEEK_END) - cur;
 
-  if (and != -1 && offset >= lseek(fd, offset, SEEK_END)
-    && offset+and == lseek(fd, offset+and, SEEK_SET)) return 0;
-  else {
-    while (offset>0) {
-      int try = offset>sizeof(libbuf) ? sizeof(libbuf) : offset, or;
+    if (end > 0 && end < offset) return offset - end;
+    end = offset+cur;
+    if (end == lseek(fd, end, SEEK_SET)) return 0;
+    perror_exit("lseek");
+  }
 
-      or = readall(fd, libbuf, try);
-      if (or < 0) perror_msg("lskip to %lld", (long long)offset);
-      else offset -= try;
-      if (or < try) break;
-    }
+  while (offset>0) {
+    int try = offset>sizeof(libbuf) ? sizeof(libbuf) : offset, or;
 
-    return offset;
+    or = readall(fd, libbuf, try);
+    if (or < 0) perror_exit("lskip to %lld", (long long)offset);
+    else offset -= try;
+    if (or < try) break;
   }
+
+  return offset;
 }
 
 // Split a path into linked list of components, tracking head and tail of list.