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

commit 412cfd8a6fb84620d99e24bc5eeb1c215233a2c4
Author: Marco Casaroli <[email protected]>
AuthorDate: Sun Oct 18 15:40:49 2026 +0200

    system/popen: Return the termination status from pclose().
    
    pclose() returned the result of fclose().  The close function of the
    stream waited for the command, but it returned OK for any termination
    status, so pclose() returned 0 also when the command failed.
    
    Let the close function only close the descriptor.  pclose() keeps the
    process ID of the command, closes the stream and then returns the
    termination status from dpclose(), as POSIX requires.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 system/popen/popen.c | 25 ++++++++++++++++++++-----
 1 file changed, 20 insertions(+), 5 deletions(-)

diff --git a/system/popen/popen.c b/system/popen/popen.c
index 3706a5776..012ca808d 100644
--- a/system/popen/popen.c
+++ b/system/popen/popen.c
@@ -91,9 +91,9 @@ static int popen_file_close(FAR void *cookie)
   FAR struct popen_file_s *filep = (FAR struct popen_file_s *)cookie;
   int ret;
 
-  ret = dpclose(filep->fd, filep->shell);
+  ret = close(filep->fd);
   free(filep);
-  return ret < 0 ? ERROR : OK;
+  return ret;
 }
 
 /****************************************************************************
@@ -251,12 +251,27 @@ FILE *popen(FAR const char *command, FAR const char *mode)
  *   stream - The stream reference returned by a previous call to popen()
  *
  * Returned Value:
- *   Zero (OK) is returned on success; otherwise -1 (ERROR) is returned and
- *   errno is set appropriately.
+ *   The termination status of the command is returned on success;
+ *   otherwise -1 (ERROR) is returned and errno is set appropriately.
  *
  ****************************************************************************/
 
 int pclose(FILE *stream)
 {
-  return fclose(stream);
+  FAR struct popen_file_s *filep;
+  pid_t shell;
+
+  if (stream == NULL || stream->fs_iofunc.close != popen_file_close)
+    {
+      set_errno(EINVAL);
+      return ERROR;
+    }
+
+  /* fclose() frees the cookie, so keep the process ID */
+
+  filep = stream->fs_cookie;
+  shell = filep->shell;
+  fclose(stream);
+
+  return dpclose(-1, shell);
 }

Reply via email to