diff options
Diffstat (limited to 'libjava/classpath/native/jni/java-lang')
8 files changed, 1370 insertions, 0 deletions
diff --git a/libjava/classpath/native/jni/java-lang/.cvsignore b/libjava/classpath/native/jni/java-lang/.cvsignore new file mode 100644 index 00000000000..e9f2658a694 --- /dev/null +++ b/libjava/classpath/native/jni/java-lang/.cvsignore @@ -0,0 +1,8 @@ +*.o +*.a +*.lo +*.la +.libs +.deps +Makefile +Makefile.in diff --git a/libjava/classpath/native/jni/java-lang/Makefile.am b/libjava/classpath/native/jni/java-lang/Makefile.am new file mode 100644 index 00000000000..34ffe00fba7 --- /dev/null +++ b/libjava/classpath/native/jni/java-lang/Makefile.am @@ -0,0 +1,16 @@ +pkglib_LTLIBRARIES = libjavalang.la libjavalangreflect.la + +libjavalang_la_SOURCES = java_lang_VMSystem.c \ + java_lang_VMFloat.c \ + java_lang_VMDouble.c \ + java_lang_Math.c \ + java_lang_VMProcess.c + +libjavalang_la_LIBADD = $(wildcard $(top_builddir)/native/fdlibm/*.lo) \ + $(top_builddir)/native/jni/classpath/jcl.lo + +libjavalangreflect_la_SOURCES = java_lang_reflect_Array.c + +AM_LDFLAGS = @CLASSPATH_MODULE@ +AM_CPPFLAGS = @CLASSPATH_INCLUDES@ -I$(top_srcdir)/native/fdlibm +AM_CFLAGS = @WARNING_CFLAGS@ @STRICT_WARNING_CFLAGS@ @ERROR_CFLAGS@ diff --git a/libjava/classpath/native/jni/java-lang/java_lang_Math.c b/libjava/classpath/native/jni/java-lang/java_lang_Math.c new file mode 100644 index 00000000000..b4b88a77537 --- /dev/null +++ b/libjava/classpath/native/jni/java-lang/java_lang_Math.c @@ -0,0 +1,161 @@ +/* Math.c - java.lang.Math native functions + Copyright (C) 1998, 1999, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +#include <config.h> +#include <java_lang_Math.h> +#include <fdlibm.h> + +JNIEXPORT jdouble JNICALL +Java_java_lang_Math_sin + (JNIEnv * env __attribute__ ((__unused__)), + jclass cls __attribute__ ((__unused__)), jdouble x) +{ + return sin (x); +} + +JNIEXPORT jdouble JNICALL +Java_java_lang_Math_cos + (JNIEnv * env __attribute__ ((__unused__)), + jclass cls __attribute__ ((__unused__)), jdouble x) +{ + return cos (x); +} + +JNIEXPORT jdouble JNICALL +Java_java_lang_Math_tan + (JNIEnv * env __attribute__ ((__unused__)), + jclass cls __attribute__ ((__unused__)), jdouble x) +{ + return tan (x); +} + +JNIEXPORT jdouble JNICALL +Java_java_lang_Math_asin + (JNIEnv * env __attribute__ ((__unused__)), + jclass cls __attribute__ ((__unused__)), jdouble x) +{ + return asin (x); +} + +JNIEXPORT jdouble JNICALL +Java_java_lang_Math_acos + (JNIEnv * env __attribute__ ((__unused__)), + jclass cls __attribute__ ((__unused__)), jdouble x) +{ + return acos (x); +} + +JNIEXPORT jdouble JNICALL +Java_java_lang_Math_atan + (JNIEnv * env __attribute__ ((__unused__)), + jclass cls __attribute__ ((__unused__)), jdouble x) +{ + return atan (x); +} + +JNIEXPORT jdouble JNICALL +Java_java_lang_Math_atan2 + (JNIEnv * env __attribute__ ((__unused__)), + jclass cls __attribute__ ((__unused__)), jdouble y, jdouble x) +{ + return atan2 (y, x); +} + +JNIEXPORT jdouble JNICALL +Java_java_lang_Math_exp + (JNIEnv * env __attribute__ ((__unused__)), + jclass cls __attribute__ ((__unused__)), jdouble x) +{ + return exp (x); +} + +JNIEXPORT jdouble JNICALL +Java_java_lang_Math_log + (JNIEnv * env __attribute__ ((__unused__)), + jclass cls __attribute__ ((__unused__)), jdouble x) +{ + return log (x); +} + +JNIEXPORT jdouble JNICALL +Java_java_lang_Math_sqrt + (JNIEnv * env __attribute__ ((__unused__)), + jclass cls __attribute__ ((__unused__)), jdouble x) +{ + return sqrt (x); +} + +JNIEXPORT jdouble JNICALL +Java_java_lang_Math_pow + (JNIEnv * env __attribute__ ((__unused__)), + jclass cls __attribute__ ((__unused__)), jdouble x, jdouble y) +{ + return pow (x, y); +} + +JNIEXPORT jdouble JNICALL +Java_java_lang_Math_IEEEremainder + (JNIEnv * env __attribute__ ((__unused__)), + jclass cls __attribute__ ((__unused__)), jdouble x, jdouble y) +{ + return remainder (x, y); +} + +JNIEXPORT jdouble JNICALL +Java_java_lang_Math_ceil + (JNIEnv * env __attribute__ ((__unused__)), + jclass cls __attribute__ ((__unused__)), jdouble x) +{ + return ceil (x); +} + +JNIEXPORT jdouble JNICALL +Java_java_lang_Math_floor + (JNIEnv * env __attribute__ ((__unused__)), + jclass cls __attribute__ ((__unused__)), jdouble x) +{ + return floor (x); +} + +JNIEXPORT jdouble JNICALL +Java_java_lang_Math_rint + (JNIEnv * env __attribute__ ((__unused__)), + jclass cls __attribute__ ((__unused__)), jdouble x) +{ + return rint (x); +} diff --git a/libjava/classpath/native/jni/java-lang/java_lang_VMDouble.c b/libjava/classpath/native/jni/java-lang/java_lang_VMDouble.c new file mode 100644 index 00000000000..6b4aed42d27 --- /dev/null +++ b/libjava/classpath/native/jni/java-lang/java_lang_VMDouble.c @@ -0,0 +1,381 @@ +/* VMDouble.c - java.lang.VMDouble native functions + Copyright (C) 1998, 1999, 2001, 2003, 2004i, 2005 + Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +#include <config.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +#include "mprec.h" +#include "fdlibm.h" +#include "jcl.h" + +#include "java_lang_VMDouble.h" + +static jclass clsDouble; +static jmethodID isNaNID; +static jdouble NEGATIVE_INFINITY; +static jdouble POSITIVE_INFINITY; +static jdouble NaN; + +/* + * Class: java_lang_VMDouble + * Method: initIDs + * Signature: ()V + */ +JNIEXPORT void JNICALL +Java_java_lang_VMDouble_initIDs (JNIEnv * env, jclass cls __attribute__ ((__unused__))) +{ + jfieldID negInfID; + jfieldID posInfID; + jfieldID nanID; + + clsDouble = (*env)->FindClass (env, "java/lang/Double"); + if (clsDouble == NULL) + { + DBG ("unable to get class java.lang.Double\n") return; + } + isNaNID = (*env)->GetStaticMethodID (env, clsDouble, "isNaN", "(D)Z"); + if (isNaNID == NULL) + { + DBG ("unable to determine method id of isNaN\n") return; + } + negInfID = (*env)->GetStaticFieldID (env, clsDouble, "NEGATIVE_INFINITY", "D"); + if (negInfID == NULL) + { + DBG ("unable to determine field id of NEGATIVE_INFINITY\n") return; + } + posInfID = (*env)->GetStaticFieldID (env, clsDouble, "POSITIVE_INFINITY", "D"); + if (posInfID == NULL) + { + DBG ("unable to determine field id of POSITIVE_INFINITY\n") return; + } + nanID = (*env)->GetStaticFieldID (env, clsDouble, "NaN", "D"); + if (posInfID == NULL) + { + DBG ("unable to determine field id of NaN\n") return; + } + POSITIVE_INFINITY = (*env)->GetStaticDoubleField (env, clsDouble, posInfID); + NEGATIVE_INFINITY = (*env)->GetStaticDoubleField (env, clsDouble, negInfID); + NaN = (*env)->GetStaticDoubleField (env, clsDouble, nanID); + +#ifdef DEBUG + fprintf (stderr, "java.lang.Double.initIDs() POSITIVE_INFINITY = %g\n", + POSITIVE_INFINITY); + fprintf (stderr, "java.lang.Double.initIDs() NEGATIVE_INFINITY = %g\n", + NEGATIVE_INFINITY); + fprintf (stderr, "java.lang.Double.initIDs() NaN = %g\n", NaN); +#endif +} + +/* + * Class: java_lang_VMDouble + * Method: doubleToLongBits + * Signature: (D)J + */ +JNIEXPORT jlong JNICALL +Java_java_lang_VMDouble_doubleToLongBits + (JNIEnv * env __attribute__ ((__unused__)), + jclass cls __attribute__ ((__unused__)), jdouble doubleValue) +{ + jvalue val; + jlong e, f; + val.d = doubleValue; + + e = val.j & 0x7ff0000000000000LL; + f = val.j & 0x000fffffffffffffLL; + + if (e == 0x7ff0000000000000LL && f != 0L) + val.j = 0x7ff8000000000000LL; + + return val.j; +} + +/* + * Class: java_lang_VMDouble + * Method: doubleToRawLongBits + * Signature: (D)J + */ +JNIEXPORT jlong JNICALL +Java_java_lang_VMDouble_doubleToRawLongBits + (JNIEnv * env __attribute__ ((__unused__)), + jclass cls __attribute__ ((__unused__)), jdouble doubleValue) +{ + jvalue val; + val.d = doubleValue; + return val.j; +} + +/* + * Class: java_lang_VMDouble + * Method: longBitsToDouble + * Signature: (J)D + */ +JNIEXPORT jdouble JNICALL +Java_java_lang_VMDouble_longBitsToDouble + (JNIEnv * env __attribute__ ((__unused__)), + jclass cls __attribute__ ((__unused__)), jlong longValue) +{ + jvalue val; + val.j = longValue; + return val.d; +} + +/* + * Class: java_lang_VMDouble + * Method: toString + * Signature: (DZ)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL +Java_java_lang_VMDouble_toString + (JNIEnv * env, jclass cls __attribute__ ((__unused__)), jdouble value, jboolean isFloat) +{ + char buffer[50], result[50]; + int decpt, sign; + char *s, *d; + int i; + +#ifdef DEBUG + fprintf (stderr, "java.lang.VMDouble.toString (%g)\n", value); +#endif + + if ((*env)->CallStaticBooleanMethod (env, clsDouble, isNaNID, value)) + return (*env)->NewStringUTF (env, "NaN"); + + if (value == POSITIVE_INFINITY) + return (*env)->NewStringUTF (env, "Infinity"); + + if (value == NEGATIVE_INFINITY) + return (*env)->NewStringUTF (env, "-Infinity"); + + _dtoa (value, 0, 20, &decpt, &sign, NULL, buffer, (int) isFloat); + + value = fabs (value); + + s = buffer; + d = result; + + if (sign) + *d++ = '-'; + + if ((value >= 1e-3 && value < 1e7) || (value == 0)) + { + if (decpt <= 0) + *d++ = '0'; + else + { + for (i = 0; i < decpt; i++) + if (*s) + *d++ = *s++; + else + *d++ = '0'; + } + + *d++ = '.'; + + if (*s == 0) + { + *d++ = '0'; + decpt++; + } + + while (decpt++ < 0) + *d++ = '0'; + + while (*s) + *d++ = *s++; + + *d = 0; + + return (*env)->NewStringUTF (env, result); + } + + *d++ = *s++; + decpt--; + *d++ = '.'; + + if (*s == 0) + *d++ = '0'; + + while (*s) + *d++ = *s++; + + *d++ = 'E'; + + if (decpt < 0) + { + *d++ = '-'; + decpt = -decpt; + } + + { + char exp[4]; + char *e = exp + sizeof exp; + + *--e = 0; + do + { + *--e = '0' + decpt % 10; + decpt /= 10; + } + while (decpt > 0); + + while (*e) + *d++ = *e++; + } + + *d = 0; + + return (*env)->NewStringUTF (env, result); +} + +/* + * Class: java_lang_VMDouble + * Method: parseDouble + * Signature: (Ljava/lang/String;)D + */ +JNIEXPORT jdouble JNICALL +Java_java_lang_VMDouble_parseDouble + (JNIEnv * env, jclass cls __attribute__ ((__unused__)), jstring str) +{ + jboolean isCopy; + const char *buf; + char *endptr; + jdouble val = 0.0; + + if (str == NULL) + { + JCL_ThrowException (env, "java/lang/NullPointerException", "null"); + return val; + } + + buf = (char *) (*env)->GetStringUTFChars (env, str, &isCopy); + if (buf == NULL) + { + /* OutOfMemoryError already thrown */ + } + else + { + const char *p = buf, *end, *last_non_ws, *temp; + int ok = 1; + +#ifdef DEBUG + fprintf (stderr, "java.lang.VMDouble.parseDouble (%s)\n", buf); +#endif + + /* Trim the buffer, similar to String.trim(). First the leading + characters. */ + while (*p && *p <= ' ') + ++p; + + /* Find the last non-whitespace character. This method is safe + even with multi-byte UTF-8 characters. */ + end = p; + last_non_ws = NULL; + while (*end) + { + if (*end > ' ') + last_non_ws = end; + ++end; + } + + if (last_non_ws == NULL) + last_non_ws = p + strlen (p); + else + { + /* Skip past the last non-whitespace character. */ + ++last_non_ws; + } + + /* Check for infinity and NaN */ + temp = p; + if (temp[0] == '+' || temp[0] == '-') + temp++; + if (strncmp ("Infinity", temp, (size_t) 8) == 0) + { + if (p[0] == '-') + return NEGATIVE_INFINITY; + return POSITIVE_INFINITY; + } + if (strncmp ("NaN", temp, (size_t) 3) == 0) + return NaN; + + /* Skip a trailing `f' or `d'. */ + if (last_non_ws > p + && (last_non_ws[-1] == 'f' + || last_non_ws[-1] == 'F' + || last_non_ws[-1] == 'd' || last_non_ws[-1] == 'D')) + --last_non_ws; + + if (last_non_ws > p) + { + struct _Jv_reent reent; + memset (&reent, 0, sizeof reent); + +#ifdef KISSME_LINUX_USER + /* FIXME: The libc strtod may not be reliable. */ + val = strtod (p, &endptr); +#else + val = _strtod_r (&reent, p, &endptr); +#endif + +#ifdef DEBUG + fprintf (stderr, "java.lang.VMDouble.parseDouble val = %g\n", val); + fprintf (stderr, "java.lang.VMDouble.parseDouble %i != %i ???\n", + endptr, last_non_ws); +#endif + if (endptr != last_non_ws) + ok = 0; + } + else + ok = 0; + + if (!ok) + { + val = 0.0; + JCL_ThrowException (env, + "java/lang/NumberFormatException", + "unable to parse double"); + } + + (*env)->ReleaseStringUTFChars (env, str, buf); + } + + return val; +} diff --git a/libjava/classpath/native/jni/java-lang/java_lang_VMFloat.c b/libjava/classpath/native/jni/java-lang/java_lang_VMFloat.c new file mode 100644 index 00000000000..7bff629fc89 --- /dev/null +++ b/libjava/classpath/native/jni/java-lang/java_lang_VMFloat.c @@ -0,0 +1,93 @@ +/* VMFloat.c - java.lang.VMFloat native functions + Copyright (C) 1998, 1999, 2003, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +#include <config.h> + +#include "java_lang_VMFloat.h" + +/* + * Class: java_lang_VMFloat + * Method: floatToIntBits + * Signature: (F)I + */ +JNIEXPORT jint JNICALL +Java_java_lang_VMFloat_floatToIntBits + (JNIEnv * env __attribute__ ((__unused__)), + jclass cls __attribute__ ((__unused__)), jfloat value) +{ + jvalue u; + jint e, f; + u.f = value; + e = u.i & 0x7f800000; + f = u.i & 0x007fffff; + + if (e == 0x7f800000 && f != 0) + u.i = 0x7fc00000; + + return u.i; +} + +/* + * Class: java_lang_VMFloat + * Method: floatToRawIntBits + * Signature: (F)I + */ +JNIEXPORT jint JNICALL +Java_java_lang_VMFloat_floatToRawIntBits + (JNIEnv * env __attribute__ ((__unused__)), + jclass cls __attribute__ ((__unused__)), jfloat value) +{ + jvalue u; + u.f = value; + return u.i; +} + +/* + * Class: java_lang_VMFloat + * Method: intBitsToFloat + * Signature: (I)F + */ +JNIEXPORT jfloat JNICALL +Java_java_lang_VMFloat_intBitsToFloat + (JNIEnv * env __attribute__ ((__unused__)), + jclass cls __attribute__ ((__unused__)), jint bits) +{ + jvalue u; + u.i = bits; + return u.f; +} diff --git a/libjava/classpath/native/jni/java-lang/java_lang_VMProcess.c b/libjava/classpath/native/jni/java-lang/java_lang_VMProcess.c new file mode 100644 index 00000000000..c1090ed3736 --- /dev/null +++ b/libjava/classpath/native/jni/java-lang/java_lang_VMProcess.c @@ -0,0 +1,494 @@ +/* java_lang_VMProcess.c -- native code for java.lang.VMProcess + Copyright (C) 1998, 1999, 2000, 2002, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +#include <config.h> + +#include "java_lang_VMProcess.h" +#include <sys/types.h> +#include <sys/wait.h> +#include <signal.h> +#include <stdlib.h> +#include <unistd.h> +#include <string.h> +#include <errno.h> +#include <fcntl.h> +#include <stdio.h> + +#include "target_native.h" +#include "target_native_misc.h" + +/* Internal functions */ +static char *copy_string (JNIEnv * env, jobject string); +static char *copy_elem (JNIEnv * env, jobject stringArray, jint i); + +/* Some O/S's don't declare 'environ' */ +#if HAVE_CRT_EXTERNS_H +/* Darwin does not have a variable named environ + but has a function which you can get the environ + variable with. */ +#include <crt_externs.h> +#define environ (*_NSGetEnviron()) +#else +extern char **environ; +#endif /* HAVE_CRT_EXTERNS_H */ + +/* + * Internal helper function to copy a String in UTF-8 format. + */ +static char * +copy_string (JNIEnv * env, jobject string) +{ + char errbuf[64]; + const char *utf; + jclass clazz; + char *copy; + + /* Check for null */ + if (string == NULL) + { + clazz = (*env)->FindClass (env, "java/lang/NullPointerException"); + if ((*env)->ExceptionOccurred (env)) + return NULL; + (*env)->ThrowNew (env, clazz, NULL); + (*env)->DeleteLocalRef (env, clazz); + return NULL; + } + + /* Extract UTF-8 */ + utf = (*env)->GetStringUTFChars (env, string, NULL); + if ((*env)->ExceptionOccurred (env)) + return NULL; + + /* Copy it */ + if ((copy = strdup (utf)) == NULL) + { + TARGET_NATIVE_MISC_FORMAT_STRING1 (errbuf, sizeof (errbuf), + "strdup: %s", strerror (errno)); + clazz = (*env)->FindClass (env, "java/lang/InternalError"); + if ((*env)->ExceptionOccurred (env)) + return NULL; + (*env)->ThrowNew (env, clazz, errbuf); + (*env)->DeleteLocalRef (env, clazz); + } + + /* Done */ + (*env)->ReleaseStringUTFChars (env, string, utf); + return copy; +} + +/* + * Internal helper function to copy a String[] element in UTF-8 format. + */ +static char * +copy_elem (JNIEnv * env, jobject stringArray, jint i) +{ + jobject elem; + char *rtn; + + elem = (*env)->GetObjectArrayElement (env, stringArray, i); + if ((*env)->ExceptionOccurred (env)) + return NULL; + if ((rtn = copy_string (env, elem)) == NULL) + return NULL; + (*env)->DeleteLocalRef (env, elem); + return rtn; +} + +/* + * private final native void nativeSpawn(String[], String[], File) + * throws java/io/IOException + */ +JNIEXPORT void JNICALL +Java_java_lang_VMProcess_nativeSpawn (JNIEnv * env, jobject this, + jobjectArray cmdArray, + jobjectArray envArray, jobject dirFile) +{ + int fds[3][2] = { {-1, -1}, {-1, -1}, {-1, -1} }; + jobject streams[3] = { NULL, NULL, NULL }; + jobject dirString = NULL; + char **newEnviron = NULL; + jsize cmdArrayLen = 0; + jsize envArrayLen = 0; + char **strings = NULL; + int num_strings = 0; + char *dir = NULL; + pid_t pid = -1; + char errbuf[64]; + jmethodID method; + jclass clazz; + int i; + + /* Check for null */ + if (cmdArray == NULL) + goto null_pointer_exception; + + /* Invoke dirFile.getPath() */ + if (dirFile != NULL) + { + clazz = (*env)->FindClass (env, "java/io/File"); + if ((*env)->ExceptionOccurred (env)) + return; + method = (*env)->GetMethodID (env, + clazz, "getPath", "()Ljava/lang/String;"); + if ((*env)->ExceptionOccurred (env)) + return; + dirString = (*env)->CallObjectMethod (env, dirFile, method); + if ((*env)->ExceptionOccurred (env)) + return; + (*env)->DeleteLocalRef (env, clazz); + } + + /* + * Allocate array of C strings. We put all the C strings we need to + * handle the command parameters, the new environment, and the new + * directory into a single array for simplicity of (de)allocation. + */ + cmdArrayLen = (*env)->GetArrayLength (env, cmdArray); + if (cmdArrayLen == 0) + goto null_pointer_exception; + if (envArray != NULL) + envArrayLen = (*env)->GetArrayLength (env, envArray); + if ((strings = malloc (((cmdArrayLen + 1) + + (envArray != NULL ? envArrayLen + 1 : 0) + + (dirString != + NULL ? 1 : 0)) * sizeof (*strings))) == NULL) + { + TARGET_NATIVE_MISC_FORMAT_STRING1 (errbuf, + sizeof (errbuf), "malloc: %s", + strerror (errno)); + goto out_of_memory; + } + + /* Extract C strings from the various String parameters */ + for (i = 0; i < cmdArrayLen; i++) + { + if ((strings[num_strings++] = copy_elem (env, cmdArray, i)) == NULL) + goto done; + } + strings[num_strings++] = NULL; /* terminate array with NULL */ + if (envArray != NULL) + { + newEnviron = strings + num_strings; + for (i = 0; i < envArrayLen; i++) + { + if ((strings[num_strings++] = copy_elem (env, envArray, i)) == NULL) + goto done; + } + strings[num_strings++] = NULL; /* terminate array with NULL */ + } + if (dirString != NULL) + { + if ((dir = copy_string (env, dirString)) == NULL) + goto done; + strings[num_strings++] = dir; + } + + /* Create inter-process pipes */ + for (i = 0; i < 3; i++) + { + if (pipe (fds[i]) == -1) + { + TARGET_NATIVE_MISC_FORMAT_STRING1 (errbuf, + sizeof (errbuf), "pipe: %s", + strerror (errno)); + goto system_error; + } + } + + /* Set close-on-exec flag for parent's ends of pipes */ + (void) fcntl (fds[0][1], F_SETFD, 1); + (void) fcntl (fds[1][0], F_SETFD, 1); + (void) fcntl (fds[2][0], F_SETFD, 1); + + /* Fork into parent and child processes */ + if ((pid = fork ()) == (pid_t) - 1) + { + TARGET_NATIVE_MISC_FORMAT_STRING1 (errbuf, + sizeof (errbuf), "fork: %s", + strerror (errno)); + goto system_error; + } + + /* Child becomes the new process */ + if (pid == 0) + { + char *const path = strings[0]; + + /* Move file descriptors to standard locations */ + if (fds[0][0] != 0) + { + if (dup2 (fds[0][0], 0) == -1) + { + fprintf (stderr, "dup2: %s", strerror (errno)); + exit (127); + } + close (fds[0][0]); + } + if (fds[1][1] != 1) + { + if (dup2 (fds[1][1], 1) == -1) + { + fprintf (stderr, "dup2: %s", strerror (errno)); + exit (127); + } + close (fds[1][1]); + } + if (fds[2][1] != 2) + { + if (dup2 (fds[2][1], 2) == -1) + { + fprintf (stderr, "dup2: %s", strerror (errno)); + exit (127); + } + close (fds[2][1]); + } + + /* Change into destination directory */ + if (dir != NULL && chdir (dir) == -1) + { + fprintf (stderr, "%s: %s", dir, strerror (errno)); + exit (127); + } + + /* Make argv[0] last component of executable pathname */ + /* XXX should use "file.separator" property here XXX */ + for (i = strlen (path); i > 0 && path[i - 1] != '/'; i--); + strings[0] = path + i; + + /* Set new environment */ + if (newEnviron != NULL) + environ = newEnviron; + + /* Execute new program (this will close the parent end of the pipes) */ + execvp (path, strings); + + /* Failed */ + fprintf (stderr, "%s: %s", path, strerror (errno)); + exit (127); + } + + /* Create Input/OutputStream objects around parent file descriptors */ + clazz = (*env)->FindClass (env, "gnu/java/nio/channels/FileChannelImpl"); + if ((*env)->ExceptionOccurred (env)) + goto done; + method = (*env)->GetMethodID (env, clazz, "<init>", "(II)V"); + if ((*env)->ExceptionOccurred (env)) + goto done; + for (i = 0; i < 3; i++) + { + /* Mode is WRITE (2) for in and READ (1) for out and err. */ + const int fd = fds[i][i == 0]; + const int mode = (i == 0) ? 2 : 1; + jclass sclazz; + jmethodID smethod; + + jobject channel = (*env)->NewObject (env, clazz, method, fd, mode); + if ((*env)->ExceptionOccurred (env)) + goto done; + + if (mode == 2) + sclazz = (*env)->FindClass (env, "java/io/FileOutputStream"); + else + sclazz = (*env)->FindClass (env, "java/io/FileInputStream"); + if ((*env)->ExceptionOccurred (env)) + goto done; + + smethod = (*env)->GetMethodID (env, sclazz, "<init>", + "(Lgnu/java/nio/channels/FileChannelImpl;)V"); + if ((*env)->ExceptionOccurred (env)) + goto done; + + streams[i] = (*env)->NewObject (env, sclazz, smethod, channel); + if ((*env)->ExceptionOccurred (env)) + goto done; + + (*env)->DeleteLocalRef (env, sclazz); + } + (*env)->DeleteLocalRef (env, clazz); + + /* Invoke VMProcess.setProcessInfo() to update VMProcess object */ + method = (*env)->GetMethodID (env, + (*env)->GetObjectClass (env, this), + "setProcessInfo", + "(Ljava/io/OutputStream;Ljava/io/InputStream;Ljava/io/InputStream;J)V"); + if ((*env)->ExceptionOccurred (env)) + goto done; + (*env)->CallVoidMethod (env, this, method, + streams[0], streams[1], streams[2], (jlong) pid); + if ((*env)->ExceptionOccurred (env)) + goto done; + (*env)->DeleteLocalRef (env, clazz); + +done: + /* + * We get here in both the success and failure cases in the + * parent process. Our goal is to clean up the mess we created. + */ + + /* Close child's ends of pipes */ + for (i = 0; i < 3; i++) + { + const int fd = fds[i][i != 0]; + + if (fd != -1) + close (fd); + } + + /* + * Close parent's ends of pipes if Input/OutputStreams never got created. + * This can only happen in a failure case. If a Stream object + * was created for a file descriptor, we don't close it because it + * will get closed when the Stream object is finalized. + */ + for (i = 0; i < 3; i++) + { + const int fd = fds[i][i == 0]; + + if (fd != -1 && streams[i] == NULL) + close (fd); + } + + /* Free C strings */ + while (num_strings > 0) + free (strings[--num_strings]); + free (strings); + + /* Done */ + return; + +null_pointer_exception: + clazz = (*env)->FindClass (env, "java/lang/NullPointerException"); + if ((*env)->ExceptionOccurred (env)) + goto done; + (*env)->ThrowNew (env, clazz, NULL); + (*env)->DeleteLocalRef (env, clazz); + goto done; + +out_of_memory: + clazz = (*env)->FindClass (env, "java/lang/InternalError"); + if ((*env)->ExceptionOccurred (env)) + goto done; + (*env)->ThrowNew (env, clazz, errbuf); + (*env)->DeleteLocalRef (env, clazz); + goto done; + +system_error: + clazz = (*env)->FindClass (env, "java/io/IOException"); + if ((*env)->ExceptionOccurred (env)) + goto done; + (*env)->ThrowNew (env, clazz, errbuf); + (*env)->DeleteLocalRef (env, clazz); + goto done; +} + +/* + * private static final native boolean nativeReap() + */ +JNIEXPORT jboolean JNICALL +Java_java_lang_VMProcess_nativeReap (JNIEnv * env, jclass clazz) +{ + char ebuf[64]; + jfieldID field; + jint status; + pid_t pid; + + /* Try to reap a child process, but don't block */ + if ((pid = waitpid ((pid_t) - 1, &status, WNOHANG)) == 0) + return JNI_FALSE; + + /* Check result from waitpid() */ + if (pid == (pid_t) - 1) + { + if (errno == ECHILD || errno == EINTR) + return JNI_FALSE; + TARGET_NATIVE_MISC_FORMAT_STRING2 (ebuf, + sizeof (ebuf), "waitpid(%ld): %s", + (long) pid, strerror (errno)); + clazz = (*env)->FindClass (env, "java/lang/InternalError"); + if ((*env)->ExceptionOccurred (env)) + return JNI_FALSE; + (*env)->ThrowNew (env, clazz, ebuf); + (*env)->DeleteLocalRef (env, clazz); + return JNI_FALSE; + } + + /* Get exit code; for signal termination return negative signal value XXX */ + if (WIFEXITED (status)) + status = (jint) (jbyte) WEXITSTATUS (status); + else if (WIFSIGNALED (status)) + status = -(jint) WTERMSIG (status); + else + return JNI_FALSE; /* process merely stopped; ignore */ + + /* Return process pid and exit status */ + field = (*env)->GetStaticFieldID (env, clazz, "reapedPid", "J"); + if ((*env)->ExceptionOccurred (env)) + return JNI_FALSE; + (*env)->SetStaticLongField (env, clazz, field, (jlong) pid); + if ((*env)->ExceptionOccurred (env)) + return JNI_FALSE; + field = (*env)->GetStaticFieldID (env, clazz, "reapedExitValue", "I"); + if ((*env)->ExceptionOccurred (env)) + return JNI_FALSE; + (*env)->SetStaticIntField (env, clazz, field, status); + if ((*env)->ExceptionOccurred (env)) + return JNI_FALSE; + + /* Done */ + return JNI_TRUE; +} + +/* + * private static final native void nativeKill(long) + */ +JNIEXPORT void JNICALL +Java_java_lang_VMProcess_nativeKill (JNIEnv * env, jclass clazz, jlong pid) +{ + char ebuf[64]; + + if (kill ((pid_t) pid, SIGKILL) == -1) + { + TARGET_NATIVE_MISC_FORMAT_STRING2 (ebuf, + sizeof (ebuf), "kill(%ld): %s", + (long) pid, strerror (errno)); + clazz = (*env)->FindClass (env, "java/lang/InternalError"); + if ((*env)->ExceptionOccurred (env)) + return; + (*env)->ThrowNew (env, clazz, ebuf); + (*env)->DeleteLocalRef (env, clazz); + } +} diff --git a/libjava/classpath/native/jni/java-lang/java_lang_VMSystem.c b/libjava/classpath/native/jni/java-lang/java_lang_VMSystem.c new file mode 100644 index 00000000000..0edbb2e965a --- /dev/null +++ b/libjava/classpath/native/jni/java-lang/java_lang_VMSystem.c @@ -0,0 +1,156 @@ +/* System.c -- native code for java.lang.System + Copyright (C) 1998, 1999, 2000, 2002, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +#include "java_lang_VMSystem.h" + +#include <jcl.h> + +#include <sys/time.h> +#include <stdlib.h> + +/* + * Class: java_lang_VMSystem + * Method: setIn0 + * Signature: (Ljava/io/InputStream;)V + */ +JNIEXPORT void JNICALL +Java_java_lang_VMSystem_setIn (JNIEnv * env, + jclass thisClass __attribute__ ((__unused__)), + jobject obj) +{ + jclass cls; + jfieldID field; + + cls = JCL_FindClass (env, "java/lang/System"); + if (!cls) + return; + + field = (*env)->GetStaticFieldID (env, cls, "in", "Ljava/io/InputStream;"); + if (!field) + return; + (*env)->SetStaticObjectField (env, cls, field, obj); +} + +/* + * Class: java_lang_VMSystem + * Method: setOut0 + * Signature: (Ljava/io/PrintStream;)V + */ +JNIEXPORT void JNICALL +Java_java_lang_VMSystem_setOut (JNIEnv * env, + jclass thisClass __attribute__ ((__unused__)), + jobject obj) +{ + jclass cls; + jfieldID field; + + cls = JCL_FindClass (env, "java/lang/System"); + if (!cls) + return; + + field = (*env)->GetStaticFieldID (env, cls, "out", "Ljava/io/PrintStream;"); + if (!field) + return; + (*env)->SetStaticObjectField (env, cls, field, obj); +} + +/* + * Class: java_lang_VMSystem + * Method: setErr0 + * Signature: (Ljava/io/PrintStream;)V + */ +JNIEXPORT void JNICALL +Java_java_lang_VMSystem_setErr (JNIEnv * env, + jclass thisClass __attribute__ ((__unused__)), + jobject obj) +{ + jclass cls; + jfieldID field; + + cls = JCL_FindClass (env, "java/lang/System"); + if (!cls) + return; + + field = (*env)->GetStaticFieldID (env, cls, "err", "Ljava/io/PrintStream;"); + if (!field) + return; + (*env)->SetStaticObjectField (env, cls, field, obj); +} + +/* + * Class: java_lang_VMSystem + * Method: currentTimeMillis + * Signature: ()J + */ +JNIEXPORT jlong JNICALL +Java_java_lang_VMSystem_currentTimeMillis + (JNIEnv * env __attribute__ ((__unused__)), + jclass thisClass __attribute__ ((__unused__))) +{ + /* Note: this implementation copied directly from Japhar's, by Chris Toshok. */ + jlong result; + struct timeval tp; + + if (gettimeofday (&tp, NULL) == -1) + (*env)->FatalError (env, "gettimeofday call failed."); + + result = (jlong) tp.tv_sec; + result *= 1000; + result += (tp.tv_usec / 1000); + + return result; +} + +JNIEXPORT jstring JNICALL +Java_java_lang_VMSystem_getenv (JNIEnv * env, + jclass klass __attribute__ ((__unused__)), + jstring jname) +{ + const char *cname; + const char *envname; + + cname = JCL_jstring_to_cstring (env, jname); + if (cname == NULL) + return NULL; + + envname = getenv (cname); + if (envname == NULL) + return NULL; + + JCL_free_cstring (env, jname, cname); + return (*env)->NewStringUTF (env, envname); +} diff --git a/libjava/classpath/native/jni/java-lang/java_lang_reflect_Array.c b/libjava/classpath/native/jni/java-lang/java_lang_reflect_Array.c new file mode 100644 index 00000000000..0bd23c16ffc --- /dev/null +++ b/libjava/classpath/native/jni/java-lang/java_lang_reflect_Array.c @@ -0,0 +1,61 @@ +/* java.lang.reflect.Array native functions + Copyright (C) 1998, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +/* + * java.lang.reflect.Array native functions. + * Author: John Keiser + * Version: 1.1.0 + * Date: 2 Jun 1998 + */ + +#include <stddef.h> + +#include "java_lang_reflect_Array.h" + +/* + * Class: java_lang_reflect_Array + * Method: createObjectArray + * Signature: (Ljava/lang/Class;I)Ljava/lang/Object; + */ +JNIEXPORT jobject JNICALL +Java_java_lang_reflect_Array_createObjectArray + (JNIEnv * env, + jclass thisClass __attribute__ ((__unused__)), + jclass arrayType, jint arrayLength) +{ + return (jobject) (*env)->NewObjectArray (env, arrayLength, arrayType, NULL); +} |