1 /*
2 		    Language Independent Library
3 
4 	    Copyright 1983-2004 Green Hills Software,Inc.
5 
6  *  This program is the property of Green Hills Software, Inc,
7  *  its contents are proprietary information and no part of it
8  *  is to be disclosed to anyone except employees of Green Hills
9  *  Software, Inc., or as agreed in writing signed by the President
10  *  of Green Hills Software, Inc.
11 */
12 /* ind_syst.c: ANSI system() facility. */
13 
14 #include "indos.h"
15 
16 /* If an external ANSI-compliant libc.a is available, it will have system() */
17 #if !defined(LIBCISANSI)
18 
19 /******************************************************************************/
20 /*  int system(const char *string)					      */
21 /*  Execute the command pointed to by "string" as if it had been typed in at  */
22 /*  the terminal.							      */
23 /*  Return the status returned by the process.  A status of 0 is returned if  */
24 /*  the operation succeeds.  The operation is system implementation dependent */
25 /*  It may have no effect.						      */
26 /******************************************************************************/
system(const char * string)27 int system(const char *string) {
28 #if defined(ANYUNIX) || defined(MSW)
29 # if defined(ANYBSD)
30 #  define fork vfork
31 # endif
32 /* Under unix there is a system function which is almost right but not quite */
33     extern int fork();
34     int status, pid;
35 
36     if (string == NULL)        /* indicate that we can do the system function */
37 	return(1);
38 /* probably should protect parent by ignoring SIGINT, ... */
39     if ((pid = fork()) == -1)
40 	return(EXIT_FAILURE);
41     else if (pid == 0) {               /* in the child */
42 	extern char **environ;
43 	char *argv[4];
44 	argv[0] = "sh";
45 	argv[1] = "-c";
46 	argv[2] = (char *) string;
47 	argv[3] = 0;
48 #ifdef MSW
49 	execve("~bin/sh", argv, environ);
50 #else
51 	execve("/bin/sh", argv, environ);
52 #endif
53 	_Exit(EXIT_FAILURE);
54     }
55     /* wait can return -1, return an error */
56     while (wait(&status) != pid);
57     return(status);
58 #elif defined(EMBEDDED)
59 #pragma ghs nowarning 1547	/* Syscall prototypes might not match */
60     return (string) ? __ghs_syscall(SYSCALL_SYSTEM, string) : (1);
61 #pragma ghs endnowarning 1547
62 #else
63     /* If no other implementation provided, minimal compliance */
64     return(0);                      /* no system access */
65 #endif
66 }
67 #endif /* !LIBCISANSI */
68