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

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

init_winsock() here.

File size: 3.6 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
42#define WSYSCALL_RETURN(form) \
43  do { \
44    int __result = form; \
45\
46    if (__result < 0){ \
47      return -errno; \
48    } \
49    return __result; \
50  } while (0)
51
52
53#if 1
54__int64
55windows_open(wchar_t *path, int flag, int mode)
56{
57  WSYSCALL_RETURN(_wopen(path, flag, mode));
58}
59#else
60__int64
61windows_open(char *path, int flag, int mode)
62{
63  WSYSCALL_RETURN(_open(path, flag, mode));
64}
65#endif
66__int64
67windows_close(int fd)
68{
69  WSYSCALL_RETURN(_close(fd));
70}
71
72__int64
73windows_read(int fd, void *buf, unsigned int count)
74{
75  WSYSCALL_RETURN(_read(fd, buf, count));
76}
77
78__int64
79windows_write(int fd, void *buf, unsigned int count)
80{
81  WSYSCALL_RETURN( _write(fd, buf, count));
82}
83
84__int64
85windows_fchmod(int fd, int mode)
86{
87  return -ENOSYS;
88}
89
90__int64
91windows_lseek(int fd, _off64_t offset, int whence)
92{
93  WSYSCALL_RETURN(lseek64(fd, offset, whence));
94}
95
96__int64
97windows_stat(wchar_t *path, struct _stat64 *buf)
98{
99  WSYSCALL_RETURN(_wstat64(path,buf));
100}
101
102__int64
103windows_fstat(int fd, struct _stat64 *buf)
104{
105  WSYSCALL_RETURN(_fstat64(fd,buf));
106}
107
108
109__int64
110windows_ftruncate(int fd, _off64_t new_size)
111{
112  /* Note that _ftruncate only allows 32-bit length */
113  WSYSCALL_RETURN(ftruncate(fd,(off_t)new_size));
114}
115
116
117void *
118windows_syscall_table[] = {
119  windows_open,
120  windows_close,
121  windows_read,
122  windows_write,
123  windows_fchmod,
124  windows_lseek,
125  windows_stat,
126  windows_fstat,
127  windows_ftruncate
128};
129
130HMODULE *modules = NULL;
131DWORD cbmodules = 0;
132
133void *
134windows_find_symbol(void *handle, char *name)
135{
136  if (handle == ((void *)-2L)) {
137    handle = NULL;
138  }
139  if (handle != NULL) {
140    return GetProcAddress(handle, name);
141  } else {
142    DWORD cbneeded,  have, i;
143
144    if (cbmodules == 0) {
145      cbmodules = 16 * sizeof(HANDLE);
146      modules = LocalAlloc(LPTR, cbmodules);
147    }
148   
149    while (1) {
150      EnumProcessModules(GetCurrentProcess(),modules,cbmodules,&cbneeded);
151      if (cbmodules >= cbneeded) {
152        break;
153      }
154      cbmodules = cbneeded;
155      modules = LocalReAlloc(modules,cbmodules,0);
156    }
157    have = cbneeded/sizeof(HANDLE);
158
159    for (i = 0; i < have; i++) {
160      void *addr = GetProcAddress(modules[i],name);
161
162      if (addr) {
163        return addr;
164      }
165    }
166    return NULL;
167  }
168}
169
170void
171init_winsock()
172{
173  WSADATA data;
174
175  WSAStartup((2<<8)|2,&data);
176}
Note: See TracBrowser for help on using the repository browser.