|
|
1.1 root 1: /*
2: * UAE - The Un*x Amiga Emulator
3: *
4: * Win32 interface
5: *
6: * Copyright 1997 Mathias Ortmann
7: */
8:
1.1.1.2 ! root 9: #include "config.h"
! 10: #include "sysconfig.h"
! 11: #include "sysdeps.h"
! 12:
1.1 root 13: #include <windows.h>
14: #include <ddraw.h>
15: #include <stdlib.h>
16: #include <stdarg.h>
17: #include <commctrl.h>
18: #include <commdlg.h>
19: #include <stdio.h>
20: #include <fcntl.h>
21: #include <sys/stat.h>
22: #include <io.h>
23:
24: #include "options.h"
25:
26: /* stdioemu, posixemu, mallocemu, and various file system helper routines */
27: static DWORD lasterror;
28:
29: static int isillegal (unsigned char *str)
30: {
31: unsigned char a = *str, b = str[1], c = str[2];
32:
33: if (a >= 'a' && a <= 'z')
34: a &= ~' ';
35: if (b >= 'a' && b <= 'z')
36: b &= ~' ';
37: if (c >= 'a' && c <= 'z')
38: c &= ~' ';
39:
40: return (a == 'A' && b == 'U' && c == 'X' ||
41: a == 'C' && b == 'O' && c == 'N' ||
42: a == 'P' && b == 'R' && c == 'N' ||
43: a == 'N' && b == 'U' && c == 'L');
44: }
45:
46: static int checkspace (char *str, char s, char d)
47: {
48: char *ptr = str;
49:
50: while (*ptr && *ptr == s)
51: ptr++;
52:
53: if (!*ptr || *ptr == '/' || *ptr == '\\') {
54: while (str < ptr)
55: *(str++) = d;
56: return 0;
57: }
58: return 1;
59: }
60:
61: /* This is sick and incomplete... in the meantime, I have discovered six new illegal file name formats
62: * M$ sucks! */
1.1.1.2 ! root 63: void fname_atow (const char *src, char *dst, int size)
1.1 root 64: {
1.1.1.2 ! root 65: char *lastslash = dst, *strt = dst, *posn = NULL, *temp = NULL;
1.1 root 66: int i, j;
67:
1.1.1.2 ! root 68: temp = xmalloc( size );
! 69:
1.1 root 70: while (size-- > 0) {
71: if (!(*dst = *src++))
72: break;
73:
74: if (*dst == '~' || *dst == '|' || *dst == '*' || *dst == '?') {
75: if (size > 2) {
76: sprintf (dst, "~%02x", *dst);
77: size -= 2;
78: dst += 2;
79: }
80: } else if (*dst == '/') {
81: if (checkspace (lastslash, ' ', 0xa0) && (dst - lastslash == 3 || (dst - lastslash > 3 && lastslash[3] == '.')) && isillegal (lastslash)) {
82: i = dst - lastslash - 3;
83: dst++;
84: for (j = i + 1; j--; dst--)
85: *dst = dst[-1];
86: *(dst++) = 0xa0;
87: dst += i;
88: size--;
89: } else if (*lastslash == '.' && (dst - lastslash == 1 || lastslash[1] == '.' && dst - lastslash == 2) && size) {
90: *(dst++) = 0xa0;
91: size--;
92: }
93: *dst = '\\';
94: lastslash = dst + 1;
95: }
96: dst++;
97: }
98:
99: if (checkspace (lastslash, ' ', 0xa0) && (dst - lastslash == 3 || (dst - lastslash > 3 && lastslash[3] == '.')) && isillegal (lastslash) && size > 1) {
100: i = dst - lastslash - 3;
101: dst++;
102: for (j = i + 1; j--; dst--)
103: *dst = dst[-1];
104: *(dst++) = 0xa0;
105: } else if (!strcmp (lastslash, ".") || !strcmp (lastslash, ".."))
106: strcat (lastslash, "\xa0");
107:
1.1.1.2 ! root 108: /* Major kludge, because I can't find the problem... */
! 109: if( ( posn = strstr( strt, "..\xA0\\" ) ) == strt && temp)
1.1 root 110: {
1.1.1.2 ! root 111: strcpy( temp, "..\\" );
! 112: strcat( temp, strt + 4 );
! 113: strcpy( strt, temp );
1.1 root 114: }
115: }
116:
117: static int hextol (char a)
118: {
119: if (a >= '0' && a <= '9')
120: return a - '0';
121: if (a >= 'a' && a <= 'f')
122: return a - 'a' + 10;
123: if (a >= 'A' && a <= 'F')
124: return a - 'A' + 10;
125: return 2;
126: }
127:
128: /* Win32 file name restrictions suck... */
129: void fname_wtoa (unsigned char *ptr)
130: {
131: unsigned char *lastslash = ptr;
132:
133: while (*ptr) {
134: if (*ptr == '~') {
135: *ptr = hextol (ptr[1]) * 16 + hextol (ptr[2]);
136: strcpy (ptr + 1, ptr + 3);
137: } else if (*ptr == '\\') {
138: if (checkspace (lastslash, ' ', 0xa0) && ptr - lastslash > 3 && lastslash[3] == 0xa0 && isillegal (lastslash)) {
139: ptr--;
140: strcpy (lastslash + 3, lastslash + 4);
141: }
142: *ptr = '/';
143: lastslash = ptr + 1;
144: }
145: ptr++;
146: }
147:
148: if (checkspace (lastslash, ' ', 0xa0) && ptr - lastslash > 3 && lastslash[3] == 0xa0 && isillegal (lastslash))
149: strcpy (lastslash + 3, lastslash + 4);
150: }
151:
152: /* pthread Win32 emulation */
153: void sem_init (HANDLE * event, int manual_reset, int initial_state)
154: {
155: *event = CreateEvent (NULL, manual_reset, initial_state, NULL);
156: }
157:
158: void sem_wait (HANDLE * event)
159: {
160: WaitForSingleObject (*event, INFINITE);
161: }
162:
163: void sem_post (HANDLE * event)
164: {
165: SetEvent (*event);
166: }
167:
168: int sem_trywait (HANDLE * event)
169: {
170: return WaitForSingleObject (*event, 0) == WAIT_OBJECT_0;
171: }
172:
173: /* Mega-klduge to prevent problems with Watcom's habit of passing
174: * arguments in registers... */
175: static HANDLE thread_sem;
176: static void *(*thread_startfunc) (void *);
177: #ifndef __GNUC__
1.1.1.2 ! root 178: static void * __stdcall thread_starter (void *arg)
! 179: #else
! 180: static void * thread_starter( void *arg )
1.1 root 181: #endif
182: {
183: void *(*func) (void *) = thread_startfunc;
184: SetEvent (thread_sem);
185: return (*func) (arg);
186: }
187:
188: /* this creates high priority threads by default to speed up the file system (kludge, will be
189: * replaced by a set_penguin_priority() routine soon) */
190: int start_penguin (void *(*f) (void *), void *arg, DWORD * foo)
191: {
192: static int have_event = 0;
193: HANDLE hThread;
194:
195: if (! have_event) {
196: thread_sem = CreateEvent (NULL, 0, 0, NULL);
197: have_event = 1;
198: }
199: thread_startfunc = f;
200: hThread = CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE) thread_starter, arg, 0, foo);
201: SetThreadPriority (hThread, THREAD_PRIORITY_HIGHEST);
202: WaitForSingleObject (thread_sem, INFINITE);
203: }
204:
1.1.1.2 ! root 205: #ifndef HAVE_TRUNCATE
! 206: int truncate (const char *name, long int len)
! 207: {
! 208: /* @@@ - there doesn't seem to be a way to truncate a file under Windows??? */
! 209: return 0;
! 210: }
! 211: #endif
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.