2003/02/12 06:44:43
[org.ibex.core.git] / src / org / xwt / plat / POSIX.cc
1 // Copyright 2002 Adam Megacz, see the COPYING file for licensing [LGPL]
2 // see below for copyright information on the second portion of this file
3
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <unistd.h>
7 #include <sys/types.h>
8 #include <gcj/cni.h>
9 #include <signal.h>
10 #include <java/lang/String.h>
11
12 // FIXME: we don't need all these
13 #include <java/lang/String.h>
14 #include <org/xwt/Surface.h>
15 #include <org/xwt/Picture.h>
16 #include <org/xwt/Box.h>
17 #include <org/xwt/util/Semaphore.h>
18 #include <org/xwt/Platform.h>
19 #include <java/lang/Long.h>
20 #include <java/util/Hashtable.h>
21 #include <org/xwt/util/Log.h>
22 #include <org/xwt/plat/POSIX.h>
23 #include <java/lang/System.h>
24 #include <java/io/PrintStream.h>
25
26 jstring org::xwt::plat::POSIX::_getEnv(jstring key) {
27     int len = JvGetStringUTFLength(key);
28     char buf[len + 1];
29     JvGetStringUTFRegion(key, 0, len, buf);
30     buf[len] = '\0';
31     char* envstr = getenv(buf);
32     return envstr == NULL ? NULL : JvNewStringLatin1(envstr);
33 }
34
35 void org::xwt::plat::POSIX::spawnChildProcess(JArray<jstring>* cmd) {
36     jstring* cmdstrings = elements(cmd);
37     char* cmd2[cmd->length + 1];
38     cmd2[cmd->length] = NULL;
39     for(int i=0; i<cmd->length; i++) {
40         cmd2[i] = (char*)malloc(JvGetStringUTFLength(cmdstrings[i]));
41         JvGetStringUTFRegion(cmdstrings[i], 0, JvGetStringUTFLength(cmdstrings[i]), cmd2[i]);
42     }
43
44     if (!fork()) {
45         signal(SIGHUP, SIG_IGN);
46         signal(SIGQUIT, SIG_IGN);
47         signal(SIGINT, SIG_IGN);
48         signal(SIGTERM, SIG_IGN);
49
50         // ignore SIGPIPE in case we were launched from a browser and the browser closed
51         signal(SIGPIPE, SIG_IGN);
52
53         execvp(cmd2[0], cmd2);
54     }
55 }
56