[git commit] ed: align output of read/write commands with POSIX-1.2008

Denys Vlasenko vda.linux at googlemail.com
Tue Oct 12 15:32:03 UTC 2021


commit: https://git.busybox.net/busybox/commit/?id=c33bbcb92fc255e4bb058e64874289cdeb0701f9
branch: https://git.busybox.net/busybox/commit/?id=refs/heads/master

POSIX.1-2008 mandates the following regarding the write command:

	If the command is successful, the number of bytes written shall
	be written to standard output, unless the -s option was
	specified, in the following format:

		"%d\n", <number of bytes written>

function                                             old     new   delta
readLines                                            447     409     -38
doCommands                                          1940    1889     -51
.rodata                                           104219  104163     -56
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/3 up/down: 0/-145)           Total: -145 bytes

Signed-off-by: Sören Tempel <soeren+git at soeren-tempel.net>
Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 editors/ed.c | 32 ++++++++++++++++++--------------
 1 file changed, 18 insertions(+), 14 deletions(-)

diff --git a/editors/ed.c b/editors/ed.c
index c50faeefa..14540e566 100644
--- a/editors/ed.c
+++ b/editors/ed.c
@@ -380,7 +380,8 @@ static void addLines(int num)
 static int readLines(const char *file, int num)
 {
 	int fd, cc;
-	int len, lineCount, charCount;
+	int len;
+	unsigned charCount;
 	char *cp;
 
 	if ((num < 1) || (num > lastNum + 1)) {
@@ -396,7 +397,6 @@ static int readLines(const char *file, int num)
 
 	bufPtr = bufBase;
 	bufUsed = 0;
-	lineCount = 0;
 	charCount = 0;
 	cc = 0;
 
@@ -415,7 +415,6 @@ static int readLines(const char *file, int num)
 			bufPtr += len;
 			bufUsed -= len;
 			charCount += len;
-			lineCount++;
 			num++;
 			continue;
 		}
@@ -449,15 +448,18 @@ static int readLines(const char *file, int num)
 			close(fd);
 			return -1;
 		}
-		lineCount++;
 		charCount += bufUsed;
 	}
 
 	close(fd);
 
-	printf("%d lines%s, %d chars\n", lineCount,
-		(bufUsed ? " (incomplete)" : ""), charCount);
-
+	/* https://pubs.opengroup.org/onlinepubs/9699919799/utilities/ed.html
+	 * "Read Command"
+	 * "...the number of bytes read shall be written to standard output
+	 * in the following format:
+	 * "%d\n", <number of bytes read>
+	 */
+	printf("%u\n", charCount);
 	return TRUE;
 }
 
@@ -468,12 +470,12 @@ static int readLines(const char *file, int num)
 static int writeLines(const char *file, int num1, int num2)
 {
 	LINE *lp;
-	int fd, lineCount, charCount;
+	int fd;
+	unsigned charCount;
 
 	if (bad_nums(num1, num2, "write"))
 		return FALSE;
 
-	lineCount = 0;
 	charCount = 0;
 
 	fd = creat(file, 0666);
@@ -482,9 +484,6 @@ static int writeLines(const char *file, int num1, int num2)
 		return FALSE;
 	}
 
-	printf("\"%s\", ", file);
-	fflush_all();
-
 	lp = findLine(num1);
 	if (lp == NULL) {
 		close(fd);
@@ -498,7 +497,6 @@ static int writeLines(const char *file, int num1, int num2)
 			return FALSE;
 		}
 		charCount += lp->len;
-		lineCount++;
 		lp = lp->next;
 	}
 
@@ -507,7 +505,13 @@ static int writeLines(const char *file, int num1, int num2)
 		return FALSE;
 	}
 
-	printf("%d lines, %d chars\n", lineCount, charCount);
+	/* https://pubs.opengroup.org/onlinepubs/9699919799/utilities/ed.html
+	 * "Write Command"
+	 * "...the number of bytes written shall be written to standard output,
+	 * unless the -s option was specified, in the following format:
+	 * "%d\n", <number of bytes written>
+	 */
+	printf("%u\n", charCount);
 	return TRUE;
 }
 


More information about the busybox-cvs mailing list