This is an automated email from the ASF dual-hosted git repository.

acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git


The following commit(s) were added to refs/heads/master by this push:
     new e53a147e5 nshlib: Let exit take an exit status.
e53a147e5 is described below

commit e53a147e55ed68f7173d398208dd2a5ae8985b52
Author: Marco Casaroli <[email protected]>
AuthorDate: Sun Oct 18 16:53:48 2026 +0200

    nshlib: Let exit take an exit status.
    
    The exit command took no argument and always exited with 0, so a
    script or "sh -c" could not report a failure with exit.
    
    Accept an optional status from 0 to 255, as POSIX does.  Without an
    argument, exit still uses 0.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 nshlib/nsh_command.c | 24 ++++++++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)

diff --git a/nshlib/nsh_command.c b/nshlib/nsh_command.c
index 14a375ebc..45fa3f9f5 100644
--- a/nshlib/nsh_command.c
+++ b/nshlib/nsh_command.c
@@ -235,7 +235,7 @@ static const struct cmdmap_s g_cmdmap[] =
 #endif
 
 #ifndef CONFIG_NSH_DISABLE_EXIT
-  CMD_MAP("exit",     cmd_exit,     1, 1, NULL),
+  CMD_MAP("exit",     cmd_exit,     1, 2, "[<status>]"),
 #endif
 
 #ifndef CONFIG_NSH_DISABLE_EXPR
@@ -1161,10 +1161,26 @@ static int cmd_false(FAR struct nsh_vtbl_s *vtbl, int 
argc, FAR char **argv)
 #ifndef CONFIG_NSH_DISABLE_EXIT
 static int cmd_exit(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
 {
-  UNUSED(argc);
-  UNUSED(argv);
+  FAR char *endptr;
+  long status = 0;
+
+  if (argc > 1)
+    {
+      status = strtol(argv[1], &endptr, 10);
+      if (*argv[1] == '\0' || *endptr != '\0')
+        {
+          nsh_error(vtbl, g_fmtarginvalid, argv[0]);
+          return ERROR;
+        }
+
+      if (status < 0 || status > 255)
+        {
+          nsh_error(vtbl, g_fmtargrange, argv[0]);
+          return ERROR;
+        }
+    }
 
-  nsh_exit(vtbl, 0);
+  nsh_exit(vtbl, (int)status);
   return OK;
 }
 #endif

Reply via email to