Fixed binary search: no more infinite loops when vendor is unknown.
[tangerine.git] / compiler / clib / system.c
blob7f3e402a1b79b153d853204c262145c8116878ec
1 /*
2 Copyright © 1995-2009, 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 <dos/filesystem.h>
12 #include <proto/dos.h>
13 //#include <utility/tagitem.h>
14 #include <unistd.h>
15 //#include <sys/types.h>
16 #include <sys/wait.h>
18 #include "__errno.h"
19 #include "__open.h"
20 #include "__upath.h"
22 #define DEBUG 0
23 #include <aros/debug.h>
25 static int system_sh(const char *string);
26 static int system_no_sh(const char *string);
28 /*****************************************************************************
30 NAME */
31 #include <stdlib.h>
33 int system (
35 /* SYNOPSIS */
36 const char *string)
38 /* FUNCTION
40 INPUTS
42 RESULT
44 NOTES
46 EXAMPLE
48 BUGS
50 SEE ALSO
52 INTERNALS
54 ******************************************************************************/
56 BPTR lock;
57 APTR old_proc_window;
58 struct Process *me;
60 if (string == NULL || string[0] == '\0')
62 D(bug("system(cmd=, args=)=1\n"));
63 return 1;
66 me = (struct Process*) FindTask(NULL);
67 old_proc_window = me->pr_WindowPtr;
68 me->pr_WindowPtr = (APTR) -1;
69 lock = Lock((STRPTR) "bin:sh", SHARED_LOCK);
70 me->pr_WindowPtr = old_proc_window;
72 if(lock)
74 UnLock(lock);
75 return system_sh(string);
77 else
79 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 struct arosc_privdata *pdata = __get_arosc_privdata();
111 const char *apath;
112 char *args, *cmd, *fullcmd;
113 fdesc *in, *out, *err;
114 int ret;
116 D(bug("system_no_sh(%s)\n", string));
118 args = strdup(string);
119 cmd = strsep(&args, " \t\n");
120 if (!args)
121 args = "";
123 D(bug("system(cmd=%s, args=%s)\n", cmd, args));
125 apath = __path_u2a(cmd);
127 fullcmd = malloc(strlen(apath) + strlen(args) + 2);
128 strcpy(fullcmd, apath);
129 strcat(fullcmd, " ");
130 strcat(fullcmd, args);
132 free(cmd);
134 in = pdata->acpd_fd_array[STDIN_FILENO];
135 out = pdata->acpd_fd_array[STDOUT_FILENO];
136 err = pdata->acpd_fd_array[STDERR_FILENO];
138 ret = (int)SystemTags
140 fullcmd,
141 SYS_Input, (IPTR)(in ? in->fcb->fh : NULL),
142 SYS_Output, (IPTR)(out ? out->fcb->fh : NULL),
143 SYS_Error, (IPTR)(err ? err->fcb->fh : NULL),
144 NULL
147 free(fullcmd);
149 if (ret == -1)
150 errno = IoErr2errno(IoErr());
152 return ret;
153 } /* system */