source: trunk/src/gcc/libjava/java/lang/natSystem.cc@ 1389

Last change on this file since 1389 was 2, checked in by bird, 23 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 13.6 KB
Line 
1// natSystem.cc - Native code implementing System class.
2
3/* Copyright (C) 1998, 1999, 2000, 2001 , 2002 Free Software Foundation
4
5 This file is part of libgcj.
6
7This software is copyrighted work licensed under the terms of the
8Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9details. */
10
11#include <config.h>
12
13#include <stdio.h>
14#include <string.h>
15#include <stdlib.h>
16
17#include "platform.h"
18
19#ifdef HAVE_PWD_H
20#include <pwd.h>
21#endif
22#include <errno.h>
23
24#ifdef HAVE_UNAME
25#include <sys/utsname.h>
26#endif
27
28#ifdef HAVE_LOCALE_H
29#include <locale.h>
30#endif
31
32#ifdef HAVE_LANGINFO_H
33#include <langinfo.h>
34#endif
35
36#if TIME_WITH_SYS_TIME
37# include <sys/time.h>
38# include <time.h>
39#else
40# if HAVE_SYS_TIME_H
41# include <sys/time.h>
42# else
43# include <time.h>
44# endif
45#endif
46
47#include <gcj/cni.h>
48#include <jvm.h>
49#include <java-props.h>
50#include <java/lang/System.h>
51#include <java/lang/Class.h>
52#include <java/lang/ArrayStoreException.h>
53#include <java/lang/ArrayIndexOutOfBoundsException.h>
54#include <java/lang/NullPointerException.h>
55#include <java/lang/StringBuffer.h>
56#include <java/util/Properties.h>
57#include <java/util/TimeZone.h>
58#include <java/io/PrintStream.h>
59#include <java/io/InputStream.h>
60
61
62
63
64void
65java::lang::System::setErr (java::io::PrintStream *newErr)
66{
67 checkSetIO ();
68 // This violates `final' semantics. Oh well.
69 err = newErr;
70}
71
72void
73java::lang::System::setIn (java::io::InputStream *newIn)
74{
75 checkSetIO ();
76 // This violates `final' semantics. Oh well.
77 in = newIn;
78}
79
80void
81java::lang::System::setOut (java::io::PrintStream *newOut)
82{
83 checkSetIO ();
84 // This violates `final' semantics. Oh well.
85 out = newOut;
86}
87
88void
89java::lang::System::arraycopy (jobject src, jint src_offset,
90 jobject dst, jint dst_offset,
91 jint count)
92{
93 if (! src || ! dst)
94 throw new NullPointerException;
95
96 jclass src_c = src->getClass();
97 jclass dst_c = dst->getClass();
98 jclass src_comp = src_c->getComponentType();
99 jclass dst_comp = dst_c->getComponentType();
100
101 if (! src_c->isArray() || ! dst_c->isArray()
102 || src_comp->isPrimitive() != dst_comp->isPrimitive()
103 || (src_comp->isPrimitive() && src_comp != dst_comp))
104 throw new ArrayStoreException;
105
106 __JArray *src_a = (__JArray *) src;
107 __JArray *dst_a = (__JArray *) dst;
108 if (src_offset < 0 || dst_offset < 0 || count < 0
109 || src_offset + count > src_a->length
110 || dst_offset + count > dst_a->length)
111 throw new ArrayIndexOutOfBoundsException;
112
113 // Do-nothing cases.
114 if ((src == dst && src_offset == dst_offset)
115 || ! count)
116 return;
117
118 // If both are primitive, we can optimize trivially. If DST
119 // components are always assignable from SRC components, then we
120 // will never need to raise an error, and thus can do the
121 // optimization. If source and destinations are the same, then we
122 // know that the assignability premise always holds.
123 const bool prim = src_comp->isPrimitive();
124 if (prim || dst_comp->isAssignableFrom(src_comp) || src == dst)
125 {
126 const size_t size = (prim ? src_comp->size()
127 : sizeof elements((jobjectArray)src)[0]);
128
129 char *src_elts = _Jv_GetArrayElementFromElementType (src, src_comp);
130 src_elts += size * src_offset;
131
132 char *dst_elts = _Jv_GetArrayElementFromElementType (dst, dst_comp);
133 dst_elts += size * dst_offset;
134
135#if HAVE_MEMMOVE
136 // We don't bother trying memcpy. It can't be worth the cost of
137 // the check.
138 // Don't cast to (void*), as memmove may expect (char*)
139 memmove (dst_elts, src_elts, count * size);
140#else
141 bcopy (src_elts, dst_elts, count * size);
142#endif
143 }
144 else
145 {
146 jobject *src_elts = elements ((jobjectArray) src_a) + src_offset;
147 jobject *dst_elts = elements ((jobjectArray) dst_a) + dst_offset;
148
149 for (int i = 0; i < count; ++i)
150 {
151 if (*src_elts
152 && ! dst_comp->isAssignableFrom((*src_elts)->getClass()))
153 throw new ArrayStoreException;
154 *dst_elts++ = *src_elts++;
155 }
156 }
157}
158
159jlong
160java::lang::System::currentTimeMillis (void)
161{
162 return _Jv_platform_gettimeofday ();
163}
164
165jint
166java::lang::System::identityHashCode (jobject obj)
167{
168 return _Jv_HashCode (obj);
169}
170
171#if ! defined (DEFAULT_FILE_ENCODING) && defined (HAVE_ICONV) \
172 && defined (HAVE_NL_LANGINFO)
173
174static char *
175file_encoding ()
176{
177 setlocale (LC_CTYPE, "");
178 char *e = nl_langinfo (CODESET);
179 if (e == NULL || *e == '\0')
180 e = "8859_1";
181 return e;
182}
183
184#define DEFAULT_FILE_ENCODING file_encoding ()
185
186#endif
187
188#ifndef DEFAULT_FILE_ENCODING
189#define DEFAULT_FILE_ENCODING "8859_1"
190#endif
191
192static char *default_file_encoding = DEFAULT_FILE_ENCODING;
193
194#if HAVE_GETPWUID_R
195/* Use overload resolution to find out the signature of getpwuid_r. */
196
197 /* This is Posix getpwuid_r. */
198template <typename T_uid, typename T_passwd, typename T_buf, typename T_len>
199static inline int
200getpwuid_adaptor(int (*getpwuid_r)(T_uid user_id, T_passwd *pwd_r,
201 T_buf *buf_r, T_len len_r,
202 T_passwd **pwd_entry_ptr),
203 uid_t user_id, struct passwd *pwd_r,