* Merged twisti branch to default. This merge introduces C++ wrapper
[cacao.git] / src / vmcore / suck.c
index 21987f0d6a3571267842e28b853a74920c20e98b..b3c05cfe497614d6634e8479dfdb12e329822b4d 100644 (file)
@@ -1,9 +1,7 @@
 /* src/vmcore/suck.c - functions to read LE ordered types from a buffer
 
-   Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
-   C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
-   E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
-   J. Wenninger, Institut f. Computersprachen - TU Wien
+   Copyright (C) 1996-2005, 2006, 2007, 2008
+   CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
 
    This file is part of CACAO.
 
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
 
-   $Id: suck.c 7272 2007-02-01 15:35:09Z twisti $
-
 */
 
 
 #include "config.h"
 
 #include <assert.h>
-#include <dirent.h>
-#include <sys/stat.h>
 #include <stdlib.h>
 
 #include "vm/types.h"
 
 #include "mm/memory.h"
 
-#if defined(ENABLE_THREADS)
-# include "threads/native/lock.h"
-#else
-# include "threads/none/lock.h"
-#endif
+#include "threads/lock-common.h"
 
 #include "toolbox/list.h"
 #include "toolbox/logging.h"
 #include "toolbox/util.h"
 
-#include "vm/exceptions.h"
+#include "vm/exceptions.hpp"
 #include "vm/properties.h"
-#include "vm/vm.h"
+#include "vm/vm.hpp"
 
 #include "vmcore/loader.h"
 #include "vmcore/options.h"
 #include "vmcore/suck.h"
+#include "vmcore/os.hpp"
 #include "vmcore/zip.h"
 
 
 /* global variables ***********************************************************/
 
-list *list_classpath_entries;
+list_t *list_classpath_entries;
 
 
 /* suck_init *******************************************************************
@@ -74,6 +65,8 @@ list *list_classpath_entries;
 
 bool suck_init(void)
 {
+       TRACESUBSYSTEMINITIALIZATION("suck_init");
+
        list_classpath_entries = list_create(OFFSET(list_classpath_entry, linkage));
 
        /* everything's ok */
@@ -88,11 +81,7 @@ bool suck_init(void)
 
 *******************************************************************************/
 
-#if defined(__LINUX__)
 static int scandir_filter(const struct dirent *a)
-#else
-static int scandir_filter(struct dirent *a)
-#endif
 {
        s4 namlen;
 
@@ -237,18 +226,19 @@ void suck_add(char *classpath)
 
 *******************************************************************************/
 
-void suck_add_from_property(char *key)
+void suck_add_from_property(const char *key)
 {
-       char           *value;
-       char           *start;
-       char           *end;
+       const char     *value;
+       const char     *start;
+       const char     *end;
        char           *path;
        s4              pathlen;
        struct dirent **namelist;
        s4              n;
        s4              i;
        s4              namlen;
-       char           *tmpbootclasspath;
+       char           *boot_class_path;
+       char           *p;
 
        /* get the property value */
 
@@ -285,7 +275,7 @@ void suck_add_from_property(char *key)
 
                        /* scan the directory found for zip/jar files */
 
-                       n = scandir(path, &namelist, scandir_filter, alphasort);
+                       n = os_scandir(path, &namelist, scandir_filter, alphasort);
 
                        /* On error, just continue, this should be ok. */
 
@@ -297,30 +287,29 @@ void suck_add_from_property(char *key)
                                        namlen = strlen(namelist[i]->d_name);
 #endif
 
-                                       /* reallocate memory for bootclasspath */
-
-                                       tmpbootclasspath = MNEW(char,
-                                                                                       pathlen + strlen("/") + namlen +
-                                                                                       strlen(":") +
-                                                                                       strlen(_Jv_bootclasspath) +
-                                                                                       strlen("0"));
-
-                                       /* prepend the file found to bootclasspath */
+                                       /* Allocate memory for bootclasspath. */
 
-                                       strcpy(tmpbootclasspath, path);
-                                       strcat(tmpbootclasspath, "/");
-                                       strcat(tmpbootclasspath, namelist[i]->d_name);
-                                       strcat(tmpbootclasspath, ":");
+                                       // FIXME Make boot_class_path const char*.
+                                       boot_class_path = (char*) properties_get("sun.boot.class.path");
 
-                                       strcat(tmpbootclasspath, _Jv_bootclasspath);
+                                       p = MNEW(char,
+                                                        pathlen + strlen("/") + namlen +
+                                                        strlen(":") +
+                                                        strlen(boot_class_path) +
+                                                        strlen("0"));
 
-                                       /* free old bootclasspath memory */
+                                       /* Prepend the file found to the bootclasspath. */
 
-                                       MFREE(_Jv_bootclasspath, u1, strlen(_Jv_bootclasspath));
+                                       strcpy(p, path);
+                                       strcat(p, "/");
+                                       strcat(p, namelist[i]->d_name);
+                                       strcat(p, ":");
+                                       strcat(p, boot_class_path);
 
-                                       /* and set the new bootclasspath */
+                                       properties_add("sun.boot.class.path", p);
+                                       properties_add("java.boot.class.path", p);
 
-                                       _Jv_bootclasspath = tmpbootclasspath;
+                                       MFREE(boot_class_path, char, strlen(boot_class_path));
 
                                        /* free the memory allocated by scandir */
                                        /* (We use `free` as the memory came from the C library.) */
@@ -361,7 +350,7 @@ bool suck_check_classbuffer_size(classbuffer *cb, s4 len)
 {
 #ifdef ENABLE_VERIFIER
        if (len < 0 || ((cb->data + cb->size) - cb->pos) < len) {
-               exceptions_throw_classformaterror((cb)->class, "Truncated class file");
+               exceptions_throw_classformaterror(cb->clazz, "Truncated class file");
                return false;
        }
 #endif /* ENABLE_VERIFIER */
@@ -563,12 +552,12 @@ classbuffer *suck_start(classinfo *c)
                        strcpy(path, lce->path);
                        strcat(path, filename);
 
-                       classfile = fopen(path, "r");
+                       classfile = os_fopen(path, "r");
 
                        if (classfile) {                                   /* file exists */
-                               if (!stat(path, &buffer)) {            /* read classfile data */
+                               if (!os_stat(path, &buffer)) {     /* read classfile data */
                                        cb = NEW(classbuffer);
-                                       cb->class = c;
+                                       cb->clazz = c;
                                        cb->size  = buffer.st_size;
                                        cb->data  = MNEW(u1, cb->size);
                                        cb->pos   = cb->data;
@@ -576,7 +565,8 @@ classbuffer *suck_start(classinfo *c)
 
                                        /* read class data */
 
-                                       len = fread(cb->data, 1, cb->size, classfile);
+                                       len = os_fread((void *) cb->data, 1, cb->size,
+                                                                          classfile);
 
                                        if (len != buffer.st_size) {
                                                suck_stop(cb);
@@ -586,7 +576,7 @@ classbuffer *suck_start(classinfo *c)
 
                                        /* close the class file */
 
-                                       fclose(classfile);
+                                       os_fclose(classfile);
                                }
                        }