[hackers] sbase: od: properly concatenate multiple file arguments

2015-10-02 Thread Greg Reagle

Relative to master.

>From 475f7b2f33e8e4a4b9ec06a455a1e970f21e99b8 Mon Sep 17 00:00:00 2001
From: Greg Reagle 
Date: Fri, 2 Oct 2015 10:26:04 -0400
Subject: [PATCH] od: properly concatenate multiple file arguments

---
 od.c | 22 --
 1 file changed, 12 insertions(+), 10 deletions(-)

diff --git a/od.c b/od.c
index 31f8179..9e382d1 100644
--- a/od.c
+++ b/od.c
@@ -68,13 +68,13 @@ printchar(FILE *f, unsigned char c)
 }
 
 static void
-od(FILE *in, char *in_name, FILE *out, char *out_name)
+od(FILE *in, char *in_name, FILE *out, char *out_name, int lastarg)
 {
-	off_t addr;
+	static off_t addr = 0;
 	size_t i, chunklen;
 	unsigned char buf[BUFSIZ];
 
-	for (addr = 0; (chunklen = fread(buf, 1, BUFSIZ, in)); ) {
+	for ( ; (chunklen = fread(buf, 1, BUFSIZ, in)); ) {
 		for (i = 0; i < chunklen && (maxbytes == -1 ||
 		 (addr - skip) < maxbytes); ++i, ++addr) {
 			if (addr - skip < 0)
@@ -89,11 +89,13 @@ od(FILE *in, char *in_name, FILE *out, char *out_name)
 		if (feof(in) || ferror(in) || ferror(out))
 			break;
 	}
-	if (addr - skip > 0)
-		fputc('\n', out);
-	if (radix != 'n') {
-		printaddress(out, MAX(addr, skip));
-		fputc('\n', out);
+	if (lastarg) {
+		if (addr - skip > 0)
+			fputc('\n', out);
+		if (radix != 'n') {
+			printaddress(out, MAX(addr, skip));
+			fputc('\n', out);
+		}
 	}
 }
 
@@ -139,7 +141,7 @@ main(int argc, char *argv[])
 	} ARGEND;
 
 	if (!argc) {
-		od(stdin, "", stdout, "");
+		od(stdin, "", stdout, "", 1);
 	} else {
 		for (; *argv; argc--, argv++) {
 			if (!strcmp(*argv, "-")) {
@@ -150,7 +152,7 @@ main(int argc, char *argv[])
 ret = 1;
 continue;
 			}
-			od(fp, *argv, stdout, "");
+			od(fp, *argv, stdout, "", *(argv + 1) ? 0 : 1);
 			if (fp != stdin && fshut(fp, *argv))
 ret = 1;
 		}
-- 
1.9.1



Re: [hackers] sbase: od: properly concatenate multiple file arguments

2015-10-02 Thread Greg Reagle

On 10/02/2015 11:35 AM, FRIGN wrote:

On Fri, 2 Oct 2015 11:16:24 -0400
Greg Reagle  wrote:


But I don't want it initialized every time the for loop is entered.
That's why I made it static.  It needs to preserve its value between calls.


Ah yeah, of course. Sorry, I overlooked that.


No problem.


Let's focus one one single thing first, or you end up rewriting all your
patches anyway.
We first need to get the length-handling properly first.


If you're referring to supporting variable length multi-btye values, I 
have given up on writing that functionality.  I will leave that to more 
talented C programmers than myself.


Concatenating multiple file arguments properly is correcting a defect in 
the program, a bug fix on existing, already committed functionality. 
That seems like high priority to me.  Also, I don't plan on any more 
patches to od, so it is the last thing I am focusing on in od for a while.




Re: [hackers] sbase: od: properly concatenate multiple file arguments

2015-10-02 Thread FRIGN
On Fri, 2 Oct 2015 10:40:12 -0400
Greg Reagle  wrote:

> Relative to master.

Better rename it to "lastfile". Then it's clearer ;)

Also:
-   off_t addr;
+   static off_t addr = 0;

please just keep the initialization inside the for-loop.
It's a slightly better style than setting the variable
directly at the declaration block.

Cheers

FRIGN

-- 
FRIGN 



Re: [hackers] sbase: od: properly concatenate multiple file arguments

2015-10-02 Thread FRIGN
On Fri, 2 Oct 2015 11:16:24 -0400
Greg Reagle  wrote:

> But I don't want it initialized every time the for loop is entered. 
> That's why I made it static.  It needs to preserve its value between calls.

Ah yeah, of course. Sorry, I overlooked that.

Let's focus one one single thing first, or you end up rewriting all your
patches anyway.
We first need to get the length-handling properly first.

Cheers

FRIGN

-- 
FRIGN 



Re: [hackers] sbase: od: properly concatenate multiple file arguments

2015-10-02 Thread Greg Reagle

On 10/02/2015 11:03 AM, FRIGN wrote:

Also:
-   off_t addr;
+   static off_t addr = 0;

please just keep the initialization inside the for-loop.
It's a slightly better style than setting the variable
directly at the declaration block.


But I don't want it initialized every time the for loop is entered. 
That's why I made it static.  It needs to preserve its value between calls.