2 Copyright © 1995-2009, The AROS Development Team. All rights reserved.
5 ANSI C function system().
8 #include "__arosc_privdata.h"
11 #include <dos/filesystem.h>
12 #include <proto/dos.h>
13 #include <utility/tagitem.h>
15 #include <sys/types.h>
24 #include <aros/debug.h>
30 struct arosc_privdata
*ppriv
;
33 static AROS_UFP3(LONG
, wait_entry
,
34 AROS_UFPA(char *, argstr
,A0
),
35 AROS_UFPA(ULONG
, argsize
,D0
),
36 AROS_UFPA(struct ExecBase
*,SysBase
,A6
)
38 static int system_sh(const char *string
);
39 static int system_no_sh(const char *string
);
41 /*****************************************************************************
67 ******************************************************************************/
73 if (string
== NULL
|| string
[0] == '\0')
75 D(bug("system(cmd=, args=)=1\n"));
79 me
= (struct Process
*) FindTask(NULL
);
80 old_proc_window
= me
->pr_WindowPtr
;
81 me
->pr_WindowPtr
= (APTR
) -1;
82 lock
= Lock((STRPTR
) "bin:sh", SHARED_LOCK
);
83 me
->pr_WindowPtr
= old_proc_window
;
88 return system_sh(string
);
92 return system_no_sh(string
);
97 static AROS_UFH3(LONG
, wait_entry
,
98 AROS_UFHA(char *, argstr
,A0
),
99 AROS_UFHA(ULONG
, argsize
,D0
),
100 AROS_UFHA(struct ExecBase
*,SysBase
,A6
)
105 struct DosLibrary
*DOSBase
;
106 struct Library
*aroscbase
;
108 childdata_t
*childdata
= (childdata_t
*)FindTask(NULL
)->tc_UserData
;
109 struct CommandLineInterface
*cli
;
111 fdesc
*in
, *out
, *err
;
112 fdesc
*newin
, *newout
, *newerr
;
114 DOSBase
= (struct DosLibrary
*)OpenLibrary(DOSNAME
, 39);
118 aroscbase
= OpenLibrary("arosc.library", 0);
119 if (aroscbase
== NULL
)
122 newin
= __alloc_fdesc();
123 newout
= __alloc_fdesc();
124 newerr
= __alloc_fdesc();
125 if(!newin
|| !newout
|| !newerr
)
129 #define privdata __get_arosc_privdata()
130 D(bug("privdata: %p, ppriv: %p\n", privdata
, childdata
->ppriv
));
131 privdata
->acpd_parent_does_upath
= childdata
->ppriv
->acpd_doupath
;
132 __get_arosc_privdata()->acpd_flags
|= KEEP_OLD_ACPD
| DO_NOT_CLONE_ENV_VARS
;
136 stacksize
= cli
->cli_DefaultStack
* CLI_DEFAULTSTACK_UNIT
;
138 stacksize
= AROS_STACKSIZE
;
140 if(__fd_array
[STDIN_FILENO
])
142 if(__fd_array
[STDOUT_FILENO
])
143 close(STDOUT_FILENO
);
144 if(__fd_array
[STDERR_FILENO
])
145 close(STDERR_FILENO
);
147 in
= childdata
->ppriv
->acpd_fd_array
[STDIN_FILENO
];
148 out
= childdata
->ppriv
->acpd_fd_array
[STDOUT_FILENO
];
149 err
= childdata
->ppriv
->acpd_fd_array
[STDERR_FILENO
];
152 SelectInput(in
->fcb
->fh
);
154 SelectOutput(out
->fcb
->fh
);
156 SelectError(err
->fcb
->fh
);
158 in
->fcb
->opencount
++;
159 out
->fcb
->opencount
++;
160 err
->fcb
->opencount
++;
164 newin
->fcb
= in
->fcb
;
165 newout
->fcb
= out
->fcb
;
166 newerr
->fcb
= err
->fcb
;
167 __fd_array
[STDIN_FILENO
] = newin
;
168 __fd_array
[STDOUT_FILENO
] = newout
;
169 __fd_array
[STDERR_FILENO
] = newerr
;
171 rc
= RunCommand(childdata
->command
, stacksize
, argstr
, argsize
);
173 CloseLibrary(aroscbase
);
176 CloseLibrary((struct Library
*)DOSBase
);
179 childdata
->returncode
= rc
;
186 static int system_sh(const char *string
)
193 if(waitpid(pid
, &status
, 0) == -1)
199 execl((__doupath
? "/bin/sh" : "bin:sh"), "sh", "-c", string
, (char *) NULL
);
208 static int system_no_sh(const char *string
)
215 cmd
= strdup(string
);
242 D(bug("system(cmd=%s, args=%s)\n", cmd
, args
? args
: ""));
244 apath
= (STRPTR
) __path_u2a(cmd
);
245 seg
= LoadSeg(apath
);
246 if (seg
== MKBADDR(NULL
))
248 struct CommandLineInterface
*cli
= Cli();
249 BPTR oldCurDir
= CurrentDir(NULL
);
250 BPTR
*paths
= cli
? BADDR(cli
->cli_CommandDir
) : NULL
;
252 for (; seg
== MKBADDR(NULL
) && paths
; paths
= BADDR(paths
[0]))
254 CurrentDir(paths
[1]);
255 seg
= LoadSeg(apath
);
258 if (seg
== MKBADDR(NULL
))
260 errno
= IoErr2errno(IoErr());
261 D(bug("system(cmd=%s, args=%s)=-1, errno=%d\n",
262 cmd
, args
? args
: "", errno
));
263 CurrentDir(oldCurDir
);
270 CurrentDir(oldCurDir
);
276 childdata_t childdata
;
278 struct TagItem tags
[] =
280 { NP_Entry
, (IPTR
)wait_entry
},
281 { NP_Arguments
, (IPTR
)args
},
282 { NP_CloseInput
, FALSE
},
283 { NP_CloseOutput
, FALSE
},
284 { NP_CloseError
, FALSE
},
285 { NP_FreeSeglist
, FALSE
},
287 { NP_Synchronous
, TRUE
},
288 { NP_Name
, (IPTR
)cmd
},
289 { NP_UserData
, (IPTR
)&childdata
},
293 childdata
.command
= seg
;
294 childdata
.ppriv
= __get_arosc_privdata();
296 if (CreateNewProc(tags
) != NULL
)
297 ret
= childdata
.returncode
;
301 errno
= IoErr2errno(IoErr());
307 D(bug("system(cmd=%s, args=%s)=%d, errno=%d\n",
308 cmd
, args
? args
: "", ret
, errno
));