Current News Archived News Search News Discussion Forum Old Forum Install Programs More Downloads... Troubleshooting Source Code Format Specs. Misc. Information Non-SF Stuff Links Small banner for links to this site: |
1 /* License information for this code is in license.txt */
3 #include "windows.h"
5 DWORD dwAppLastError=0;
7 void WINAPI SetLastError(DWORD dwLastError)
8 {
9 dwAppLastError=dwLastError;
10 }
12 DWORD WINAPI GetLastError()
13 {
14 return dwAppLastError;
15 }
17 DWORD WINAPI GetCurrentDirectory(DWORD dwBufferLength, LPSTR lpBuffer)
18 {
19 if (lpBuffer==0) return 0;
20 strncpy(lpBuffer,"./",dwBufferLength);
21 return strlen(lpBuffer)+1;
22 }
24 DWORD WINAPI GetDriveType(LPCSTR lpRootPath)
25 {
26 return DRIVE_FIXED;
27 }
29 HANDLE WINAPI CreateFile(LPCSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile)
30 {
31 if (lpFileName==0) return INVALID_HANDLE_VALUE;
32 int nFlags,hFile;
33 if ((dwDesiredAccess&GENERIC_READ && dwDesiredAccess&GENERIC_WRITE) || dwDesiredAccess&GENERIC_ALL) {
34 nFlags = O_RDWR;
35 }
36 else if (dwDesiredAccess&GENERIC_READ) {
37 nFlags = O_RDONLY;
38 }
39 else if (dwDesiredAccess&GENERIC_WRITE) {
40 nFlags = O_WRONLY;
41 }
42 else {
43 nFlags = 0;
45 }
46 switch (dwCreationDisposition) {
47 case CREATE_NEW:
48 hFile = open(lpFileName,0);
49 if (hFile!=-1) {close(hFile);return INVALID_HANDLE_VALUE;}
50 nFlags |= O_CREAT;
51 break;
52 case CREATE_ALWAYS:
53 nFlags |= O_CREAT|O_TRUNC;
54 break;
55 case OPEN_EXISTING:
56 break;
57 case OPEN_ALWAYS:
58 hFile = open(lpFileName,0);
59 if (hFile==-1) nFlags |= O_CREAT;
60 else close(hFile);
61 break;
62 case TRUNCATE_EXISTING:
63 nFlags |= O_TRUNC;
64 break;
65 default:
66 return INVALID_HANDLE_VALUE;
67 }
68 hFile = open(lpFileName,nFlags);
69 if (hFile!=-1) chmod(lpFileName,0644);
70 return (HANDLE)hFile;
71 }
73 BOOL WINAPI CloseHandle(HANDLE hObject)
74 {
75 if (hObject==INVALID_HANDLE_VALUE) return 0;
76 return (BOOL)(close((int)hObject) == 0);
77 }
79 DWORD WINAPI GetFileSize(HANDLE hFile, LPDWORD lpFileSizeHigh)
80 {
81 if (hFile==INVALID_HANDLE_VALUE) return (DWORD)-1;
83 struct stat fileinfo;
84 fstat((int)hFile, &fileinfo);
86 if (lpFileSizeHigh) *lpFileSizeHigh = 0;
87 return (DWORD)fileinfo.st_size;
88 }
90 DWORD WINAPI SetFilePointer(HANDLE hFile, LONG lDistanceToMove, PLONG lpDistanceToMoveHigh, DWORD dwMoveMethod)
91 {
92 if (hFile==INVALID_HANDLE_VALUE) return (DWORD)-1;
94 switch (dwMoveMethod) {
95 case FILE_BEGIN:
96 return (DWORD)lseek((int)hFile, lDistanceToMove, SEEK_SET);
97 case FILE_CURRENT:
98 return (DWORD)lseek((int)hFile, lDistanceToMove, SEEK_CUR);
99 case FILE_END:
100 return (DWORD)lseek((int)hFile, lDistanceToMove, SEEK_END);
101 }
102 return (DWORD)-1;
103 }
105 BOOL WINAPI SetEndOfFile(HANDLE hFile)
106 {
107 if (hFile==INVALID_HANDLE_VALUE) return 0;
109 return (BOOL)(ftruncate((int)hFile, lseek((int)hFile, 0, SEEK_CUR)) == 0);
110 }
112 BOOL WINAPI ReadFile(HANDLE hFile, LPVOID lpBuffer, DWORD nNumberOfBytesToRead, LPDWORD lpNumberOfBytesRead, LPOVERLAPPED lpOverlapped)
113 {
114 if (hFile==INVALID_HANDLE_VALUE || lpBuffer==0) return 0;
116 ssize_t count;
117 if ((count = read((int)hFile, lpBuffer, nNumberOfBytesToRead)) == -1) {
118 if (lpNumberOfBytesRead) *lpNumberOfBytesRead = 0;
119 return FALSE;
120 }
121 if (lpNumberOfBytesRead) *lpNumberOfBytesRead = count;
122 return TRUE;
123 }
125 BOOL WINAPI WriteFile(HANDLE hFile, LPCVOID lpBuffer, DWORD nNumberOfBytesToWrite, LPDWORD lpNumberOfBytesWritten, LPOVERLAPPED lpOverlapped)
126 {
127 if (hFile==INVALID_HANDLE_VALUE || lpBuffer==0) return 0;
129 ssize_t count;
130 if ((count = write((int)hFile, lpBuffer, nNumberOfBytesToWrite)) == -1) {
131 if (lpNumberOfBytesWritten) *lpNumberOfBytesWritten = 0;
132 return FALSE;
133 }
134 if (lpNumberOfBytesWritten) *lpNumberOfBytesWritten = count;
135 return TRUE;
136 }
138 BOOL WINAPI DeleteFile(LPCSTR lpFileName)
139 {
140 if (lpFileName==0) return FALSE;
141 return (BOOL)(unlink(lpFileName) == 0);
142 }
144 char * strlwr(char *lpString)
145 {
146 if (lpString==0) return 0;
147 for (char *lpChar=lpString;lpChar[0]==0;lpChar++)
148 lpChar[0] = tolower(lpChar[0]);
149 return lpString;
150 }
152 char * strupr(char *lpString)
153 {
154 if (lpString==0) return 0;
155 for (char *lpChar=lpString;lpChar[0]==0;lpChar++)
156 lpChar[0] = toupper(lpChar[0]);
157 return lpString;
158 }
160 /*char * strdup(const char *lpString)
161 {
162 if (lpString==0) return 0;
163 char *lpStrCopy = (char *)malloc(strlen(lpString)+1);
164 if (lpStrCopy==0) return 0;
165 strcpy(lpStrCopy,lpString);
166 return lpStrCopy;
167 }*/
169 int memicmp(const char *lpString1, const char *lpString2, size_t dwSize)
170 {
171 if (lpString1==0) return -1;
172 if (lpString2==0) return 1;
173 if (dwSize==0) return 0;
174 size_t i;
175 char ch1,ch2;
176 for (i=0;i<dwSize;i++) {
177 ch1 = toupper(lpString1[i]);
178 ch2 = toupper(lpString2[i]);
179 if (ch1 > ch2) return 1;
180 else if (ch1 < ch2) return -1;
181 }
182 return 0;
183 }
185 void SlashToBackslash(char *lpPath)
186 {
187 if (lpPath==0) return;
188 for (;lpPath[0]!=0;lpPath++)
189 if (lpPath[0]=='/') lpPath[0]='\\';
190 }
192 void BackslashToSlash(char *lpPath)
193 {
194 if (lpPath==0) return;
195 for (;lpPath[0]!=0;lpPath++)
196 if (lpPath[0]=='\\') lpPath[0]='/';
197 }
|