revert between 56095 -> 55830 in arch
[AROS.git] / compiler / posixc / system.c
blobba8e0ad1ee39fc6c2cd1c978f72208d585e57612
1 /*
2 Copyright © 1995-2013, The AROS Development Team. All rights reserved.
3 $Id$
5 POSIX.1-2008 function system().
6 */
8 #include "__posixc_intbase.h"
10 #include <dos/dos.h>
11 #include <proto/dos.h>
12 #include <unistd.h>
13 #include <errno.h>
14 #include <sys/wait.h>
16 #include "__fdesc.h"
17 #include "__upath.h"
19 #define DEBUG 0
20 #include <aros/debug.h>
22 static int system_sh(const char *string);
23 static int system_no_sh(const char *string);
25 /*****************************************************************************
27 NAME */
28 #include <stdlib.h>
30 int system (
32 /* SYNOPSIS */
33 const char *string)
35 /* FUNCTION
36 Execute a command string. If string is NULL then 1 will be returned.
38 INPUTS
39 string - command to execute or NULL
41 RESULT
42 Return value of command executed. If value < 0 errno indicates error.
43 1 is return if string is NULL.
45 NOTES
46 The system() version of posixc.library will translate UNIX<>Amiga
47 if applicable as well as use a shell for executing text batch
48 commands.
50 EXAMPLE
52 BUGS
54 SEE ALSO
56 INTERNALS
58 ******************************************************************************/
60 struct PosixCIntBase *PosixCBase =
61 (struct PosixCIntBase *)__aros_getbase_PosixCBase();
62 BPTR lock;
63 APTR old_proc_window;
64 struct Process *me;
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"));
72 return 1;
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;
81 if (lock)
83 UnLock(lock);
84 return system_sh(string);
87 return system_no_sh(string);
88 } /* system */
91 static int system_sh(const char *string)
93 struct PosixCIntBase *PosixCBase =
94 (struct PosixCIntBase *)__aros_getbase_PosixCBase();
95 pid_t pid = vfork();
96 int status;
98 D(bug("system_sh(%s)\n", string));
100 if(pid > 0)
102 if(waitpid(pid, &status, 0) == -1)
103 return -1;
104 return status;
106 else if(pid == 0)
108 execl((PosixCBase->doupath ? "/bin/sh" : "bin:sh"), "sh", "-c", string, (char *) NULL);
109 _exit(127);
111 else
113 return -1;
117 static int system_no_sh(const char *string)
119 const char *apath;
120 char *args, *cmd, *fullcmd;
121 fdesc *in, *out, *err;
122 BPTR infh, outfh, errfh;
123 int ret;
125 D(bug("system_no_sh(%s)\n", string));
127 args = strdup(string);
128 cmd = strsep(&args, " \t\n");
129 if (!args)
130 args = "";
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);
141 free(cmd);
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;
154 if (outfh == errfh)
155 errfh = 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
163 fullcmd,
164 SYS_Input, (IPTR)(infh),
165 SYS_Output, (IPTR)(outfh),
166 SYS_Error, (IPTR)(errfh),
167 NULL
170 free(fullcmd);
172 if (ret == -1)
173 errno = __stdc_ioerr2errno(IoErr());
175 return ret;
176 } /* system */