questionable patch: merge of a lot of stuff from the svg branch
[org.ibex.core.git] / src / org / ibex / plat / POSIX.cc
1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the GNU General Public License version 2 ("the License").
3 // You may not use this file except in compliance with the License.
4
5 // see below for copyright information on the second portion of this file
6
7 #include <java/lang/String.h>
8 #include <org/ibex/graphics/Surface.h>
9 #include <org/ibex/graphics/Picture.h>
10 #include <org/ibex/js/JS.h>
11 #include <org/ibex/core/Box.h>
12 #include <org/ibex/util/Semaphore.h>
13 #include <org/ibex/plat/Platform.h>
14 #include <java/lang/Long.h>
15 #include <java/util/Hashtable.h>
16 #include <org/ibex/util/Log.h>
17 #include <org/ibex/plat/POSIX.h>
18 #include <java/lang/System.h>
19 #include <java/io/PrintStream.h>
20
21 #include "GCJ.cc"
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <sys/ipc.h>
27 #include <sys/shm.h>
28 #include <sys/types.h>
29 #include <signal.h>
30
31 jstring org::ibex::plat::POSIX::_getEnv(jstring key) {
32     int len = JvGetStringUTFLength(key);
33     char buf[len + 1];
34     JvGetStringUTFRegion(key, 0, len, buf);
35     buf[len] = '\0';
36     char* envstr = getenv(buf);
37     return envstr == NULL ? NULL : JvNewStringLatin1(envstr);
38 }
39
40 void org::ibex::plat::POSIX::spawnChildProcess(JArray<jstring>* cmd) {
41     jstring* cmdstrings = elements(cmd);
42     char* cmd2[cmd->length + 1];
43     cmd2[cmd->length] = NULL;
44     for(int i=0; i<cmd->length; i++) {
45         cmd2[i] = (char*)malloc(JvGetStringUTFLength(cmdstrings[i]));
46         JvGetStringUTFRegion(cmdstrings[i], 0, JvGetStringUTFLength(cmdstrings[i]), cmd2[i]);
47     }
48
49     if (!fork()) {
50         signal(SIGHUP, SIG_IGN);
51         signal(SIGQUIT, SIG_IGN);
52         signal(SIGINT, SIG_IGN);
53         signal(SIGTERM, SIG_IGN);
54
55         // ignore SIGPIPE in case we were launched from a browser and the browser closed
56         signal(SIGPIPE, SIG_IGN);
57
58         execvp(cmd2[0], cmd2);
59     }
60 }
61