arch/m68k-amiga: Define the gcc symbol 'start' instead of using .bss
[AROS.git] / compiler / clib / system.c
blobfe2bb2254930642978b2a7642176d915081c4b66
1 /*
2 Copyright © 1995-2011, The AROS Development Team. All rights reserved.
3 $Id$
5 ANSI C function system().
6 */
8 #include "__arosc_privdata.h"
10 #include <dos/dos.h>
11 #include <proto/dos.h>
12 //#include <utility/tagitem.h>
13 #include <unistd.h>
14 //#include <sys/types.h>
15 #include <sys/wait.h>
17 #include "__errno.h"
18 #include "__fdesc.h"
19 #include "__upath.h"
21 #define DEBUG 0
22 #include <aros/debug.h>
24 static int system_sh(const char *string);
25 static int system_no_sh(const char *string);
27 /*****************************************************************************
29 NAME */
30 #include <stdlib.h>
32 int system (
34 /* SYNOPSIS */
35 const char *string)
37 /* FUNCTION
39 INPUTS
41 RESULT
43 NOTES
45 EXAMPLE
47 BUGS
49 SEE ALSO
51 INTERNALS
53 ******************************************************************************/
55 BPTR lock;
56 APTR old_proc_window;
57 struct Process *me;
59 if (!__doupath)
60 return system_no_sh(string);
62 if (string == NULL || string[0] == '\0')
64 D(bug("system(cmd=, args=)=1\n"));
65 return 1;
68 me = (struct Process*) FindTask(NULL);
69 old_proc_window = me->pr_WindowPtr;
70 me->pr_WindowPtr = (APTR) -1;
71 lock = Lock((STRPTR) "bin:sh", SHARED_LOCK);
72 me->pr_WindowPtr = old_proc_window;
74 if (lock)
76 UnLock(lock);
77 return system_sh(string);
80 return system_no_sh(string);
81 } /* system */
84 static int system_sh(const char *string)
86 pid_t pid = vfork();
87 int status;
89 D(bug("system_sh(%s)\n", string));
91 if(pid > 0)
93 if(waitpid(pid, &status, 0) == -1)
94 return -1;
95 return status;
97 else if(pid == 0)
99 execl((__doupath ? "/bin/sh" : "bin:sh"), "sh", "-c", string, (char *) NULL);
100 _exit(127);
102 else
104 return -1;
108 static int system_no_sh(const char *string)
110 const char *apath;
111 char *args, *cmd, *fullcmd;
112 fdesc *in, *out, *err;
113 int ret;
115 D(bug("system_no_sh(%s)\n", string));
117 args = strdup(string);
118 cmd = strsep(&args, " \t\n");
119 if (!args)
120 args = "";
122 D(bug("system(cmd=%s, args=%s)\n", cmd, args));
124 apath = __path_u2a(cmd);
126 fullcmd = malloc(strlen(apath) + strlen(args) + 2);
127 strcpy(fullcmd, apath);
128 strcat(fullcmd, " ");
129 strcat(fullcmd, args);
131 free(cmd);
133 in = __getfdesc(STDIN_FILENO);
134 out = __getfdesc(STDOUT_FILENO);
135 err = __getfdesc(STDERR_FILENO);
137 ret = (int)SystemTags
139 fullcmd,
140 SYS_Input, (IPTR)(in ? in->fcb->fh : BNULL),
141 SYS_Output, (IPTR)(out ? out->fcb->fh : BNULL),
142 SYS_Error, (IPTR)(err ? err->fcb->fh : BNULL),
143 NULL
146 free(fullcmd);
148 if (ret == -1)
149 errno = IoErr2errno(IoErr());
151 return ret;
152 } /* system */