POSIX generally requires normal output to go to stdout and
diagnostic (i.e. error) output to go to stderr.

When usage messages for BusyBox applets are specifically requested
by the user (e.g. 'find --help') they should go to stdout; when
they're emitted due to an error they should go to stderr.

function                                             old     new   delta
bb_show_usage                                        148     146      -2
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-2)               Total: -2 bytes

Signed-off-by: Avi Halachmi <avih...@yahoo.com>
Signed-off-by: Ron Yorston <r...@pobox.com>
---
 libbb/appletlib.c | 30 +++++++++++++++++-------------
 1 file changed, 17 insertions(+), 13 deletions(-)

diff --git a/libbb/appletlib.c b/libbb/appletlib.c
index ad373ae1c..d2e5900b5 100644
--- a/libbb/appletlib.c
+++ b/libbb/appletlib.c
@@ -127,17 +127,19 @@ static const char packed_usage[] ALIGN1 = { PACKED_USAGE 
};
 void FAST_FUNC bb_show_usage(void)
 {
        if (ENABLE_SHOW_USAGE) {
+               ssize_t FAST_FUNC (*full_write_fn)(const char *) =
+                               xfunc_error_retval ? full_write2_str : 
full_write1_str;
 #ifdef SINGLE_APPLET_STR
                /* Imagine that this applet is "true". Dont link in printf! */
                const char *usage_string = unpack_usage_messages();
 
                if (usage_string) {
                        if (*usage_string == '\b') {
-                               full_write2_str("No help available\n");
+                               full_write_fn("No help available\n");
                        } else {
-                               full_write2_str("Usage: "SINGLE_APPLET_STR" ");
-                               full_write2_str(usage_string);
-                               full_write2_str("\n");
+                               full_write_fn("Usage: "SINGLE_APPLET_STR" ");
+                               full_write_fn(usage_string);
+                               full_write_fn("\n");
                        }
                        if (ENABLE_FEATURE_CLEAN_UP)
                                dealloc_usage_messages((char*)usage_string);
@@ -153,19 +155,19 @@ void FAST_FUNC bb_show_usage(void)
                        while (*p++) continue;
                        ap--;
                }
-               full_write2_str(bb_banner);
-               full_write2_str(" multi-call binary.\n"); /* common string */
+               full_write_fn(bb_banner);
+               full_write_fn(" multi-call binary.\n"); /* common string */
                if (*p == '\b')
-                       full_write2_str("\nNo help available\n");
+                       full_write_fn("\nNo help available\n");
                else {
-                       full_write2_str("\nUsage: ");
-                       full_write2_str(applet_name);
+                       full_write_fn("\nUsage: ");
+                       full_write_fn(applet_name);
                        if (p[0]) {
                                if (p[0] != '\n')
-                                       full_write2_str(" ");
-                               full_write2_str(p);
+                                       full_write_fn(" ");
+                               full_write_fn(p);
                        }
-                       full_write2_str("\n");
+                       full_write_fn("\n");
                }
                if (ENABLE_FEATURE_CLEAN_UP)
                        dealloc_usage_messages((char*)usage_string);
@@ -268,8 +270,10 @@ void lbb_prepare(const char *applet
                 && !(ENABLE_TRUE && strcmp(applet_name, "true") == 0)
                 && !(ENABLE_FALSE && strcmp(applet_name, "false") == 0)
                 && !(ENABLE_ECHO && strcmp(applet_name, "echo") == 0)
-               )
+               ) {
+                       xfunc_error_retval = 0;
                        bb_show_usage();
+               }
        }
 #endif
 }
-- 
2.44.0

_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to