[PATCH] ed: add support for -s command-line option as mandated by POSIX

soeren at soeren-tempel.net soeren at soeren-tempel.net
Tue Dec 28 10:45:52 UTC 2021


From: Sören Tempel <soeren+git at soeren-tempel.net>

Apart from the -p option, POSIX also mandates an -s option which
suppresses the output of byte counts for the e, E, r, and w command.
>From these commands, Busybox ed presently only implements the r and w
commands. This commit ensures that these two command do not output any
bytes counts when the -s option is passed. The shell escape command,
also effected by the -s option, is not implemented by Busybox at the
moment.
---
 editors/ed.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/editors/ed.c b/editors/ed.c
index dfe0f1a77..a7595dc45 100644
--- a/editors/ed.c
+++ b/editors/ed.c
@@ -18,7 +18,7 @@
 
 //applet:IF_ED(APPLET(ed, BB_DIR_BIN, BB_SUID_DROP))
 
-//usage:#define ed_trivial_usage "[-p PROMPT] [FILE]"
+//usage:#define ed_trivial_usage "[-p PROMPT] [-s] [FILE]"
 //usage:#define ed_full_usage ""
 
 #include "libbb.h"
@@ -52,6 +52,7 @@ struct globals {
 	LINE lines;
 	smallint dirty;
 	int marks[26];
+	int silentMode;
 };
 #define G (*ptr_to_globals)
 #define curLine            (G.curLine           )
@@ -66,6 +67,7 @@ struct globals {
 #define dirty              (G.dirty             )
 #define lines              (G.lines             )
 #define marks              (G.marks             )
+#define silentMode         (G.silentMode        )
 #define INIT_G() do { \
 	setup_common_bufsiz(); \
 	SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
@@ -458,7 +460,8 @@ static int readLines(const char *file, int num)
 	 * in the following format:
 	 * "%d\n", <number of bytes read>
 	 */
-	printf("%u\n", charCount);
+	if (!silentMode)
+		printf("%u\n", charCount);
 	return TRUE;
 }
 
@@ -510,7 +513,8 @@ static int writeLines(const char *file, int num1, int num2)
 	 * unless the -s option was specified, in the following format:
 	 * "%d\n", <number of bytes written>
 	 */
-	printf("%u\n", charCount);
+	if (!silentMode)
+		printf("%u\n", charCount);
 	return TRUE;
 }
 
@@ -996,6 +1000,8 @@ static void doCommands(void)
 int ed_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int ed_main(int argc UNUSED_PARAM, char **argv)
 {
+	int opt;
+
 	INIT_G();
 
 	bufSize = INITBUF_SIZE;
@@ -1005,7 +1011,8 @@ int ed_main(int argc UNUSED_PARAM, char **argv)
 	lines.prev = &lines;
 
 	prompt = ""; /* no prompt by default */
-	getopt32(argv, "p:", &prompt);
+	opt = getopt32(argv, "sp:", &prompt);
+	silentMode = opt & 0x01;
 	argv += optind;
 
 	if (argv[0]) {


More information about the busybox mailing list