load_image(image);
init_stacks();
init_io();
+
+#ifdef WIN32
init_signals();
+#endif
+
init_compiler();
init_errors();
gc_time = 0;
+++ /dev/null
-#include "factor.h"
-
-void primitive_open_file(void)
-{
- bool write = unbox_boolean();
- bool read = unbox_boolean();
-
- char* path;
- int mode, fd;
-
- maybe_garbage_collection();
-
- path = unbox_c_string();
-
- if(read && write)
- mode = O_RDWR | O_CREAT;
- else if(read)
- mode = O_RDONLY;
- else if(write)
- mode = O_WRONLY | O_CREAT | O_TRUNC;
- else
- mode = 0;
-
- fd = open(path,mode,FILE_MODE);
- if(fd < 0)
- io_error(__FUNCTION__);
-
- dpush(read ? tag_object(port(PORT_READ,fd)) : F);
- dpush(write ? tag_object(port(PORT_WRITE,fd)) : F);
-}
-
-void primitive_stat(void)
-{
- struct stat sb;
- F_STRING* path;
-
- maybe_garbage_collection();
-
- path = untag_string(dpop());
- if(stat(to_c_string(path),&sb) < 0)
- dpush(F);
- else
- {
- CELL dirp = tag_boolean(S_ISDIR(sb.st_mode));
- CELL mode = tag_fixnum(sb.st_mode & ~S_IFMT);
- CELL size = tag_object(s48_long_long_to_bignum(sb.st_size));
- CELL mtime = tag_integer(sb.st_mtime);
- dpush(cons(
- dirp,
- cons(
- mode,
- cons(
- size,
- cons(
- mtime,F)))));
- }
-}
-
-void primitive_read_dir(void)
-{
- F_STRING* path;
- DIR* dir;
- CELL result = F;
-
- maybe_garbage_collection();
-
- path = untag_string(dpop());
- dir = opendir(to_c_string(path));
- if(dir != NULL)
- {
- struct dirent* file;
-
- while((file = readdir(dir)) != NULL)
- {
- CELL name = tag_object(from_c_string(
- file->d_name));
- result = cons(name,result);
- }
-
- closedir(dir);
- }
-
- dpush(result);
-}
-
-void primitive_cwd(void)
-{
- char wd[MAXPATHLEN];
- maybe_garbage_collection();
- if(getcwd(wd,MAXPATHLEN) < 0)
- io_error(__FUNCTION__);
- box_c_string(wd);
-}
-
-void primitive_cd(void)
-{
- maybe_garbage_collection();
- chdir(unbox_c_string());
-}
--- /dev/null
+#include "../factor.h"
+
+void primitive_open_file(void)
+{
+ bool write = unbox_boolean();
+ bool read = unbox_boolean();
+
+ char* path;
+ int mode, fd;
+
+ maybe_garbage_collection();
+
+ path = unbox_c_string();
+
+ if(read && write)
+ mode = O_RDWR | O_CREAT;
+ else if(read)
+ mode = O_RDONLY;
+ else if(write)
+ mode = O_WRONLY | O_CREAT | O_TRUNC;
+ else
+ mode = 0;
+
+ fd = open(path,mode,FILE_MODE);
+ if(fd < 0)
+ io_error(__FUNCTION__);
+
+ dpush(read ? tag_object(port(PORT_READ,fd)) : F);
+ dpush(write ? tag_object(port(PORT_WRITE,fd)) : F);
+}
+
+void primitive_stat(void)
+{
+ struct stat sb;
+ F_STRING* path;
+
+ maybe_garbage_collection();
+
+ path = untag_string(dpop());
+ if(stat(to_c_string(path),&sb) < 0)
+ dpush(F);
+ else
+ {
+ CELL dirp = tag_boolean(S_ISDIR(sb.st_mode));
+ CELL mode = tag_fixnum(sb.st_mode & ~S_IFMT);
+ CELL size = tag_object(s48_long_long_to_bignum(sb.st_size));
+ CELL mtime = tag_integer(sb.st_mtime);
+ dpush(cons(
+ dirp,
+ cons(
+ mode,
+ cons(
+ size,
+ cons(
+ mtime,F)))));
+ }
+}
+
+void primitive_read_dir(void)
+{
+ F_STRING* path;
+ DIR* dir;
+ CELL result = F;
+
+ maybe_garbage_collection();
+
+ path = untag_string(dpop());
+ dir = opendir(to_c_string(path));
+ if(dir != NULL)
+ {
+ struct dirent* file;
+
+ while((file = readdir(dir)) != NULL)
+ {
+ CELL name = tag_object(from_c_string(
+ file->d_name));
+ result = cons(name,result);
+ }
+
+ closedir(dir);
+ }
+
+ dpush(result);
+}
+
+void primitive_cwd(void)
+{
+ char wd[MAXPATHLEN];
+ maybe_garbage_collection();
+ if(getcwd(wd,MAXPATHLEN) < 0)
+ io_error(__FUNCTION__);
+ box_c_string(wd);
+}
+
+void primitive_cd(void)
+{
+ maybe_garbage_collection();
+ chdir(unbox_c_string());
+}
+
--- /dev/null
+#include "../factor.h"
+
+void primitive_open_file(void)
+{
+ bool write = unbox_boolean();
+ bool read = unbox_boolean();
+ char *path;
+ DWORD mode = 0, create = 0;
+ HANDLE fp;
+ SECURITY_ATTRIBUTES sa;
+
+ path = unbox_c_string();
+
+ mode |= write ? GENERIC_WRITE : 0;
+ mode |= read ? GENERIC_READ : 0;
+
+ if (read && write)
+ create = OPEN_ALWAYS;
+ else if (read)
+ create = OPEN_EXISTING;
+ else if (write)
+ create = CREATE_ALWAYS;
+
+ sa.nLength = sizeof(SECURITY_ATTRIBUTES);
+ sa.lpSecurityDescriptor = NULL;
+ sa.bInheritHandle = true;
+
+ fp = CreateFile(
+ path,
+ mode,
+ FILE_SHARE_DELETE|FILE_SHARE_READ|FILE_SHARE_WRITE,
+ &sa,
+ create,
+ /* FILE_FLAG_OVERLAPPED TODO */0,
+ NULL);
+
+ if (fp == INVALID_HANDLE_VALUE)
+ {
+ io_error(__FUNCTION__);
+ }
+ else
+ {
+ CreateIoCompletionPort(fp, completion_port, 0, 0);
+ dpush(read ? tag_object(port(PORT_READ, (CELL)fp)) : F);
+ dpush(write ? tag_object(port(PORT_WRITE, (CELL)fp)) : F);
+ }
+}
+
+void primitive_stat(void)
+{
+ F_STRING *path;
+ WIN32_FILE_ATTRIBUTE_DATA st;
+
+ maybe_garbage_collection();
+ path = untag_string(dpop());
+
+ if(!GetFileAttributesEx(to_c_string(path), GetFileExInfoStandard, &st))
+ {
+ dpush(F);
+ }
+ else
+ {
+ CELL dirp = tag_boolean(st.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
+ CELL size = tag_object(s48_long_long_to_bignum(
+ (int64_t)st.nFileSizeLow | (int64_t)st.nFileSizeHigh << 32));
+ CELL mtime = tag_integer((int)(*(int64_t*)&st.ftLastWriteTime / 100000000 - 172456224));
+ dpush(
+ cons(dirp,
+ cons(tag_fixnum(0),
+ cons(size,
+ cons(mtime, F)))));
+ }
+}
+
+void primitive_read_dir(void)
+{
+ F_STRING *path;
+ HANDLE dir;
+ WIN32_FIND_DATA find_data;
+ CELL result = F;
+
+ maybe_garbage_collection();
+
+ path = untag_string(dpop());
+ if (INVALID_HANDLE_VALUE != (dir = FindFirstFile(".\\*", &find_data)))
+ {
+ do
+ {
+ CELL name = tag_object(from_c_string(find_data.cFileName));
+ result = cons(name, result);
+ }
+ while (FindNextFile(dir, &find_data));
+ CloseHandle(dir);
+ }
+
+ dpush(result);
+}
+
+void primitive_cwd(void)
+{
+ char buf[MAX_PATH];
+
+ maybe_garbage_collection();
+ if(!GetCurrentDirectory(MAX_PATH, buf))
+ io_error(__FUNCTION__);
+
+ box_c_string(buf);
+}
+
+void primitive_cd(void)
+{
+ maybe_garbage_collection();
+ SetCurrentDirectory(unbox_c_string());
+}
\ No newline at end of file