2 Copyright © 1995-2013, The AROS Development Team. All rights reserved.
5 POSIX.1-2008 function system().
8 #include "__posixc_intbase.h"
11 #include <proto/dos.h>
20 #include <aros/debug.h>
22 static int system_sh(const char *string
);
23 static int system_no_sh(const char *string
);
25 /*****************************************************************************
36 Execute a command string. If string is NULL then 1 will be returned.
39 string - command to execute or NULL
42 Return value of command executed. If value < 0 errno indicates error.
43 1 is return if string is NULL.
46 The system() version of posixc.library will translate UNIX<>Amiga
47 if applicable as well as use a shell for executing text batch
58 ******************************************************************************/
60 struct PosixCIntBase
*PosixCBase
=
61 (struct PosixCIntBase
*)__aros_getbase_PosixCBase();
66 if (!PosixCBase
->doupath
)
67 return system_no_sh(string
);
69 if (string
== NULL
|| string
[0] == '\0')
71 D(bug("system(cmd=, args=)=1\n"));
75 me
= (struct Process
*) FindTask(NULL
);
76 old_proc_window
= me
->pr_WindowPtr
;
77 me
->pr_WindowPtr
= (APTR
) -1;
78 lock
= Lock((STRPTR
) "bin:sh", SHARED_LOCK
);
79 me
->pr_WindowPtr
= old_proc_window
;
84 return system_sh(string
);
87 return system_no_sh(string
);
91 static int system_sh(const char *string
)
93 struct PosixCIntBase
*PosixCBase
=
94 (struct PosixCIntBase
*)__aros_getbase_PosixCBase();
98 D(bug("system_sh(%s)\n", string
));
102 if(waitpid(pid
, &status
, 0) == -1)
108 execl((PosixCBase
->doupath
? "/bin/sh" : "bin:sh"), "sh", "-c", string
, (char *) NULL
);
117 static int system_no_sh(const char *string
)
120 char *args
, *cmd
, *fullcmd
;
121 fdesc
*in
, *out
, *err
;
122 BPTR infh
, outfh
, errfh
;
125 D(bug("system_no_sh(%s)\n", string
));
127 args
= strdup(string
);
128 cmd
= strsep(&args
, " \t\n");
132 D(bug("system(cmd=%s, args=%s)\n", cmd
, args
));
134 apath
= __path_u2a(cmd
);
136 fullcmd
= malloc(strlen(apath
) + strlen(args
) + 2);
137 strcpy(fullcmd
, apath
);
138 strcat(fullcmd
, " ");
139 strcat(fullcmd
, args
);
143 in
= __getfdesc(STDIN_FILENO
);
144 out
= __getfdesc(STDOUT_FILENO
);
145 err
= __getfdesc(STDERR_FILENO
);
147 D(bug("[system_no_sh]in: %p, in->fcb->fh: %p\n", in
, BADDR(in
->fcb
->handle
)));
148 D(bug("[system_no_sh]out: %p, out->fcb->fh: %p\n", out
, BADDR(out
->fcb
->handle
)));
149 D(bug("[system_no_sh]err: %p, err->fcb->fh: %p\n", err
, BADDR(err
->fcb
->handle
)));
151 infh
= in
? in
->fcb
->handle
: BNULL
;
152 outfh
= out
? out
->fcb
->handle
: BNULL
;
153 errfh
= err
? err
->fcb
->handle
: BNULL
;
157 D(bug("[system_no_sh]infh: %p, outfh: %p, errfh %p\n",
158 BADDR(infh
), BADDR(outfh
), BADDR(errfh
)
161 ret
= (int)SystemTags
164 SYS_Input
, (IPTR
)(infh
),
165 SYS_Output
, (IPTR
)(outfh
),
166 SYS_Error
, (IPTR
)(errfh
),
173 errno
= __stdc_ioerr2errno(IoErr());