3 <<system>>---execute command string
12 int system(char *<[s]>);
14 int _system_r(void *<[reent]>, char *<[s]>);
18 Use <<system>> to pass a command string <<*<[s]>>> to <</bin/sh>> on
19 your system, and wait for it to finish executing.
21 Use ``<<system(NULL)>>'' to test whether your system has <</bin/sh>>
24 The alternate function <<_system_r>> is a reentrant version. The
25 extra argument <[reent]> is a pointer to a reentrancy structure.
28 <<system(NULL)>> returns a non-zero value if <</bin/sh>> is available, and
31 With a command argument, the result of <<system>> is the exit status
32 returned by <</bin/sh>>.
35 ANSI C requires <<system>>, but leaves the nature and effects of a
36 command processor undefined. ANSI C does, however, specify that
37 <<system(NULL)>> return zero or nonzero to report on the existence of
40 POSIX.2 requires <<system>>, and requires that it invoke a <<sh>>.
41 Where <<sh>> is found is left unspecified.
43 Supporting OS subroutines required: <<_exit>>, <<_execve>>, <<_fork_r>>,
55 #if defined (unix) || defined (__CYGWIN__)
56 static int do_system (struct _reent
*ptr
, const char *s
);
60 _system_r (struct _reent
*ptr
,
63 #if defined(HAVE_SYSTEM)
66 #elif defined(NO_EXEC)
73 /* ??? How to handle (s == NULL) here is not exactly clear.
74 If _fork_r fails, that's not really a justification for returning 0.
75 For now we always return 0 and leave it to each target to explicitly
76 handle otherwise (this can always be relaxed in the future). */
78 #if defined (unix) || defined (__CYGWIN__)
81 return do_system (ptr
, s
);
95 system (const char *s
)
97 return _system_r (_REENT
, s
);
102 #if defined (unix) && !defined (__CYGWIN__) && !defined(__rtems__)
103 extern char **environ
;
105 /* Only deal with a pointer to environ, to work around subtle bugs with shared
106 libraries and/or small data systems where the user declares his own
108 static char ***p_environ
= &environ
;
111 do_system (struct _reent
*ptr
,
119 argv
[2] = (char *) s
;
122 if ((pid
= _fork_r (ptr
)) == 0)
124 _execve ("/bin/sh", argv
, *p_environ
);
131 int rc
= _wait_r (ptr
, &status
);
134 status
= (status
>> 8) & 0xff;
140 #if defined (__CYGWIN__)
142 do_system (struct _reent
*ptr
,
150 argv
[2] = (char *) s
;
153 if ((pid
= vfork ()) == 0)
155 /* ??? It's not clear what's the right path to take (pun intended :-).
156 There won't be an "sh" in any fixed location so we need each user
157 to be able to say where to find "sh". That suggests using an
158 environment variable, but after a few more such situations we may
159 have too many of them. */
160 char *sh
= getenv ("SH_PATH");
163 _execve (sh
, argv
, environ
);
170 extern int _wait (int *);
171 int rc
= _wait (&status
);
174 status
= (status
>> 8) & 0xff;