]> gitweb.factorcode.org Git - factor.git/commitdiff
merge eiz's file.c
authorSlava Pestov <slava@factorcode.org>
Sat, 11 Dec 2004 03:12:05 +0000 (03:12 +0000)
committerSlava Pestov <slava@factorcode.org>
Sat, 11 Dec 2004 03:12:05 +0000 (03:12 +0000)
native/factor.c
native/file.c [deleted file]
native/unix/file.c [new file with mode: 0644]
native/win32/file.c [new file with mode: 0644]

index 027a2a87c7eee25bc52e2880388c2e88327d1d9b..66c5cd83c3fdff39a9d1411ca566331c9a97d180 100644 (file)
@@ -6,7 +6,11 @@ void init_factor(char* image)
        load_image(image);
        init_stacks();
        init_io();
+
+#ifdef WIN32
        init_signals();
+#endif
+
        init_compiler();
        init_errors();
        gc_time = 0;
diff --git a/native/file.c b/native/file.c
deleted file mode 100644 (file)
index 9f7f585..0000000
+++ /dev/null
@@ -1,99 +0,0 @@
-#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());
-}
diff --git a/native/unix/file.c b/native/unix/file.c
new file mode 100644 (file)
index 0000000..52183e2
--- /dev/null
@@ -0,0 +1,100 @@
+#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());
+}
+
diff --git a/native/win32/file.c b/native/win32/file.c
new file mode 100644 (file)
index 0000000..13ee48d
--- /dev/null
@@ -0,0 +1,114 @@
+#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