changeset 623:f51beec92738

New infrastructure for od (oops).
author Rob Landley <rob@landley.net>
date Sun, 15 Jul 2012 16:56:20 -0500
parents 93ef2516f15a
children 1e8b9acdafeb
files lib/lib.c lib/lib.h
diffstat 2 files changed, 36 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/lib/lib.c	Sat Jul 14 00:59:32 2012 -0500
+++ b/lib/lib.c	Sun Jul 15 16:56:20 2012 -0500
@@ -286,6 +286,27 @@
 	return offset;
 }
 
+off_t lskip(int fd, off_t offset)
+{
+	off_t and = lseek(fd, offset, SEEK_CUR);
+
+	if (and != -1 && offset >= lseek(fd, offset, SEEK_END)
+		&& offset+and == lseek(fd, offset+and, SEEK_SET)) return 0;
+	else {
+		char buf[4096];
+		while (offset>0) {
+			int try = offset>sizeof(buf) ? sizeof(buf) : offset, or;
+
+			or = readall(fd, buf, try);
+			if (or < 0) perror_msg("lskip to %lld", (long long)offset);
+			else offset -= try;
+			if (or < try) break;
+		}
+
+		return offset;
+	}
+}
+
 char *xgetcwd(void)
 {
 	char *buf = getcwd(NULL, 0);
@@ -503,8 +524,7 @@
 	long val = strtol(numstr, &c, 0);
 
 	if (*c) {
-		end = strchr(suffixes, tolower(*c));
-		if (end) {
+		if (c != numstr && (end = strchr(suffixes, tolower(*c)))) {
 			int shift = end-suffixes;
 			if (shift--) val *= 1024L<<(shift*10);
 		} else {
@@ -526,6 +546,17 @@
     return len;
 }
 
+int stridx(char *haystack, char needle)
+{
+	char *off;
+
+	if (!needle) return -1;
+	off = strchr(haystack, needle);
+	if (!off) return -1;
+
+	return off-haystack;
+}
+
 // Return how long the file at fd is, if there's any way to determine it.
 off_t fdlength(int fd)
 {
--- a/lib/lib.h	Sat Jul 14 00:59:32 2012 -0500
+++ b/lib/lib.h	Sun Jul 15 16:56:20 2012 -0500
@@ -65,9 +65,9 @@
 struct dirtree {
 	struct dirtree *next, *parent, *child;
 	long extra; // place for user to store their stuff (can be pointer)
-	long data;  // dirfd for directory, linklen for symlink, -1 = comeagain
 	struct stat st;
 	char *symlink;
+	int data;  // dirfd for directory, linklen for symlink, -1 = comeagain
 	char name[];
 };
 
@@ -112,6 +112,7 @@
 void xreadall(int fd, void *buf, size_t len);
 void xwrite(int fd, void *buf, size_t len);
 off_t xlseek(int fd, off_t offset, int whence);
+off_t lskip(int fd, off_t offset);
 char *readfile(char *name);
 char *xreadfile(char *name);
 char *xgetcwd(void);
@@ -128,6 +129,7 @@
 char *itoa(int n);
 long atolx(char *c);
 int numlen(long l);
+int stridx(char *haystack, char needle);
 off_t fdlength(int fd);
 char *xreadlink(char *name);
 void loopfiles_rw(char **argv, int flags, int permissions, int failok,