clean up bcm bases and add primecell peripheral defines
[AROS.git] / compiler / clib / system.c
blob49cd26dfd6cf7741069f77d6a1ba9729f7ac051c
1 /*
2 Copyright © 1995-2012, The AROS Development Team. All rights reserved.
3 $Id$
5 C99 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 <errno.h>
15 //#include <sys/types.h>
16 #include <sys/wait.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 struct aroscbase *aroscbase = __aros_getbase_aroscbase();
56 BPTR lock;
57 APTR old_proc_window;
58 struct Process *me;
60 if (!aroscbase->acb_doupath)
61 return system_no_sh(string);
63 if (string == NULL || string[0] == '\0')
65 D(bug("system(cmd=, args=)=1\n"));
66 return 1;
69 me = (struct Process*) FindTask(NULL);
70 old_proc_window = me->pr_WindowPtr;
71 me->pr_WindowPtr = (APTR) -1;
72 lock = Lock((STRPTR) "bin:sh", SHARED_LOCK);
73 me->pr_WindowPtr = old_proc_window;
75 if (lock)
77 UnLock(lock);
78 return system_sh(string);
81 return system_no_sh(string);
82 } /* system */
85 static int system_sh(const char *string)
87 struct aroscbase *aroscbase = __aros_getbase_aroscbase();
88 pid_t pid = vfork();
89 int status;
91 D(bug("system_sh(%s)\n", string));
93 if(pid > 0)
95 if(waitpid(pid, &status, 0) == -1)
96 return -1;
97 return status;
99 else if(pid == 0)
101 execl((aroscbase->acb_doupath ? "/bin/sh" : "bin:sh"), "sh", "-c", string, (char *) NULL);
102 _exit(127);
104 else
106 return -1;
110 static int system_no_sh(const char *string)
112 const char *apath;
113 char *args, *cmd, *fullcmd;
114 fdesc *in, *out, *err;
115 int ret;
117 D(bug("system_no_sh(%s)\n", string));
119 args = strdup(string);
120 cmd = strsep(&args, " \t\n");
121 if (!args)
122 args = "";
124 D(bug("system(cmd=%s, args=%s)\n", cmd, args));
126 apath = __path_u2a(cmd);
128 fullcmd = malloc(strlen(apath) + strlen(args) + 2);
129 strcpy(fullcmd, apath);
130 strcat(fullcmd, " ");
131 strcat(fullcmd, args);
133 free(cmd);
135 in = __getfdesc(STDIN_FILENO);
136 out = __getfdesc(STDOUT_FILENO);
137 err = __getfdesc(STDERR_FILENO);
139 ret = (int)SystemTags
141 fullcmd,
142 SYS_Input, (IPTR)(in ? in->fcb->fh : BNULL),
143 SYS_Output, (IPTR)(out ? out->fcb->fh : BNULL),
144 SYS_Error, (IPTR)(err ? err->fcb->fh : BNULL),
145 NULL
148 free(fullcmd);
150 if (ret == -1)
151 errno = __arosc_ioerr2errno(IoErr());
153 return ret;
154 } /* system */