source: branches/win64/lisp-kernel/windows-calls.c @ 9320

Last change on this file since 9320 was 9320, checked in by gb, 13 years ago

Add (wide-char) directory-access functions (statically linkged from
mingw lib.)

File size: 3.9 KB
Line 
1/*
2   Copyright (C) 2008, Clozure Associates and contributors,
3   This file is part of Clozure CL. 
4
5   Clozure CL is licensed under the terms of the Lisp Lesser GNU Public
6   License , known as the LLGPL and distributed with Clozure CL as the
7   file "LICENSE".  The LLGPL consists of a preamble and the LGPL,
8   which is distributed with Clozure CL as the file "LGPL".  Where these
9   conflict, the preamble takes precedence. 
10
11   Clozure CL is referenced in the preamble as the "LIBRARY."
12
13   The LLGPL is also available online at
14   http://opensource.franz.com/preamble.html
15*/
16
17/*
18   These aren't really system calls; they're just C runtime functions
19   that (a) are built in to the lisp, so they can be called early
20   in the cold load, before the FFI is initialized and (b) return negated
21   error code on failure, so that it's not necessary to separately
22   fetch errno.
23
24   It's reasonable to consider replacing these things with wrappers
25   around native functionality (ReadFile, etc.) someday.
26
27   The order of the entries in windows_syscall_table[] should match
28   the order of syscall indices defined in
29   "ccl:library;x86-win64-syscalls.lisp".
30
31   One last time: these aren't really system calls.
32*/
33
34#include <io.h>
35#include <unistd.h>
36#include <sys/fcntl.h>
37#include <errno.h>
38#include <sys/stat.h>
39#include <windows.h>
40#include <psapi.h>
41#include <dirent.h>
42
43#define WSYSCALL_RETURN(form) \
44  do { \
45    int __result = form; \
46\
47    if (__result < 0){ \
48      return -errno; \
49    } \
50    return __result; \
51  } while (0)
52
53
54#if 1
55__int64
56windows_open(wchar_t *path, int flag, int mode)
57{
58  WSYSCALL_RETURN(_wopen(path, flag, mode));
59}
60#else
61__int64
62windows_open(char *path, int flag, int mode)
63{
64  WSYSCALL_RETURN(_open(path, flag, mode));
65}
66#endif
67__int64
68windows_close(int fd)
69{
70  WSYSCALL_RETURN(_close(fd));
71}
72
73__int64
74windows_read(int fd, void *buf, unsigned int count)
75{
76  WSYSCALL_RETURN(_read(fd, buf, count));
77}
78
79__int64
80windows_write(int fd, void *buf, unsigned int count)
81{
82  WSYSCALL_RETURN( _write(fd, buf, count));
83}
84
85__int64
86windows_fchmod(int fd, int mode)
87{
88  return -ENOSYS;
89}
90
91__int64
92windows_lseek(int fd, _off64_t offset, int whence)
93{
94  WSYSCALL_RETURN(lseek64(fd, offset, whence));
95}
96
97__int64
98windows_stat(wchar_t *path, struct _stat64 *buf)
99{
100  WSYSCALL_RETURN(_wstat64(path,buf));
101}
102
103__int64
104windows_fstat(int fd, struct _stat64 *buf)
105{
106  WSYSCALL_RETURN(_fstat64(fd,buf));
107}
108
109
110__int64
111windows_ftruncate(int fd, _off64_t new_size)
112{
113  /* Note that _ftruncate only allows 32-bit length */
114  WSYSCALL_RETURN(ftruncate(fd,(off_t)new_size));
115}
116
117_WDIR *
118windows_opendir(wchar_t *path)
119{
120  return _wopendir(path);
121}
122
123struct _wdirent *
124windows_readdir(_WDIR *dir)
125{
126  return _wreaddir(dir);
127}
128
129__int64
130windows_closedir(_WDIR *dir)
131{
132  WSYSCALL_RETURN(_wclosedir(dir));
133}
134
135void *
136windows_syscall_table[] = {
137  windows_open,
138  windows_close,
139  windows_read,
140  windows_write,
141  windows_fchmod,
142  windows_lseek,
143  windows_stat,
144  windows_fstat,
145  windows_ftruncate,
146  windows_opendir,
147  windows_readdir,
148  windows_closedir
149};
150
151HMODULE *modules = NULL;
152DWORD cbmodules = 0;
153
154void *
155windows_find_symbol(void *handle, char *name)
156{
157  if (handle == ((void *)-2L)) {
158    handle = NULL;
159  }
160  if (handle != NULL) {
161    return GetProcAddress(handle, name);
162  } else {
163    DWORD cbneeded,  have, i;
164
165    if (cbmodules == 0) {
166      cbmodules = 16 * sizeof(HANDLE);
167      modules = LocalAlloc(LPTR, cbmodules);
168    }
169   
170    while (1) {
171      EnumProcessModules(GetCurrentProcess(),modules,cbmodules,&cbneeded);
172      if (cbmodules >= cbneeded) {
173        break;
174      }
175      cbmodules = cbneeded;
176      modules = LocalReAlloc(modules,cbmodules,0);
177    }
178    have = cbneeded/sizeof(HANDLE);
179
180    for (i = 0; i < have; i++) {
181      void *addr = GetProcAddress(modules[i],name);
182
183      if (addr) {
184        return addr;
185      }
186    }
187    return NULL;
188  }
189}
190
191void
192init_winsock()
193{
194  WSADATA data;
195
196  WSAStartup((2<<8)|2,&data);
197}
Note: See TracBrowser for help on using the repository browser.