2003/02/05 21:51:50
[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
11 jstring org::xwt::plat::POSIX::_getEnv(jstring key) {
12     int len = JvGetStringUTFLength(key);
13     char buf[len + 1];
14     JvGetStringUTFRegion(key, 0, len, buf);
15     buf[len] = '\0';
16     char* envstr = getenv(buf);
17     return envstr == NULL ? NULL : JvNewStringLatin1(envstr);
18 }
19
20 void org::xwt::plat::POSIX::spawnChildProcess(JArray<jstring>* cmd) {
21     jstring* cmdstrings = elements(cmd);
22     char* cmd2[cmd->length + 1];
23     cmd2[cmd->length] = NULL;
24     for(int i=0; i<cmd->length; i++) {
25         cmd2[i] = (char*)malloc(JvGetStringUTFLength(cmdstrings[i]));
26         JvGetStringUTFRegion(cmdstrings[i], 0, JvGetStringUTFLength(cmdstrings[i]), cmd2[i]);
27     }
28
29     if (!fork()) {
30         signal(SIGHUP, SIG_IGN);
31         signal(SIGQUIT, SIG_IGN);
32         signal(SIGINT, SIG_IGN);
33         signal(SIGTERM, SIG_IGN);
34
35         // ignore SIGPIPE in case we were launched from a browser and the browser closed
36         signal(SIGPIPE, SIG_IGN);
37
38         execvp(cmd2[0], cmd2);
39     }
40 }
41