tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[PATCH 1/1] fix system() behaviour when parameter is NULL



The ISO/IEC 9899:1999 describes in 7.20.4.6 behaviour of the system() when its
parameter is NULL. So, we should check a presence of a command interpreter
instead of returning 1. Also the manual page has been adjusted accordingly.
---
 lib/libc/stdlib/system.3 |    4 ++--
 lib/libc/stdlib/system.c |   11 +++++++++--
 2 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/lib/libc/stdlib/system.3 b/lib/libc/stdlib/system.3
index 6c1c733..ab154ea 100644
--- a/lib/libc/stdlib/system.3
+++ b/lib/libc/stdlib/system.3
@@ -67,8 +67,8 @@ is a
 .Dv NULL
 pointer,
 .Fn system
-will return non-zero.
-Otherwise,
+shall return non-zero to indicate that a command interpreter is available, or
+zero if none is available. Otherwise,
 .Fn system
 returns the termination status of the shell in the format specified by
 .Xr waitpid 2 .
diff --git a/lib/libc/stdlib/system.c b/lib/libc/stdlib/system.c
index 4cc3cbe..2b729b5 100644
--- a/lib/libc/stdlib/system.c
+++ b/lib/libc/stdlib/system.c
@@ -64,8 +64,15 @@ system(command)
        const char *argp[] = {"sh", "-c", NULL, NULL};
        argp[2] = command;
 
-       if (command == NULL)            /* just checking... */
-               return(1);
+       /*
+        * ISO/IEC 9899:1999 in 7.20.4.6 describes this special case.
+        * We need to check availability of a command interpreter.
+        */ 
+       if (command == NULL) {
+               if (access(_PATH_BSHELL, R_OK | X_OK) == 0)
+                       return 1;
+               return 0;
+       }
 
        sa.sa_handler = SIG_IGN;
        sigemptyset(&sa.sa_mask);
-- 
1.5.2.5



Home | Main Index | Thread Index | Old Index