-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
bpemu.c
324 lines (280 loc) · 6.76 KB
/
bpemu.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/* bpemu.c */
/*****************************************************************************/
/* AS-Portierung */
/* */
/* Emulation einiger Borland-Pascal-Funktionen */
/* */
/* Historie: 20. 5.1996 Grundsteinlegung */
/* 2001-04-13 Win32 fixes */
/* */
/*****************************************************************************/
#include "stdinc.h"
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <ctype.h>
#include "strutil.h"
#include "bpemu.h"
#ifdef __MSDOS__
#include <dos.h>
#include <dir.h>
#endif
#if defined( __EMX__ ) || defined( __IBMC__ )
#include <os2.h>
#endif
#ifdef __MINGW32__
#include <direct.h>
#endif
char *FExpand(char *Src)
{
static String CurrentDir;
String Copy;
#ifdef DRSEP
String DrvPart;
#endif /* DRSEP */
char *p,*p2;
strmaxcpy(Copy, Src, 255);
#ifdef DRSEP
p = strchr(Copy,DRSEP);
if (p)
{
memcpy(DrvPart, Copy, p - Copy);
DrvPart[p - Copy] = '\0';
strmov(Copy, p + 1);
}
else
*DrvPart = '\0';
#endif
#if (defined __MSDOS__)
{
int DrvNum;
if (*DrvPart == '\0')
{
DrvNum = getdisk();
*DrvPart = DrvNum + 'A';
DrvPart[1] = '\0'; DrvNum++;
}
else
DrvNum = toupper(*DrvPart) - '@';
getcurdir(DrvNum, CurrentDir);
}
#elif (defined __EMX__) || (defined __IBMC__)
{
ULONG DrvNum, Dummy;
if (*DrvPart == '\0')
{
DosQueryCurrentDisk(&DrvNum, &Dummy);
*DrvPart = DrvNum + '@'; DrvPart[1] = '\0';
}
else
DrvNum = toupper(*DrvPart) - '@';
Dummy = 255;
DosQueryCurrentDir(DrvNum, (PBYTE) CurrentDir, &Dummy);
}
#elif (defined __MINGW32__)
{
int DrvNum;
if (!*DrvPart)
{
DrvNum = _getdrive();
*DrvPart = DrvNum + '@'; DrvPart[1] = '\0';
}
else
DrvNum = toupper(*DrvPart) - '@';
_getdcwd(DrvNum, CurrentDir, 255);
if (CurrentDir[1] == ':')
strmov(CurrentDir, CurrentDir + 2);
}
#elif (defined _WIN32) /* CygWIN */
getcwd(CurrentDir, 255);
for (p = CurrentDir; *p; p++)
if (*p == '/') *p = '\\';
#else /* UNIX */
getcwd(CurrentDir,255);
#endif
if ((*CurrentDir) && (CurrentDir[strlen(CurrentDir)-1]!=PATHSEP))
strmaxcat(CurrentDir,SPATHSEP,255);
if (*CurrentDir!=PATHSEP)
strmaxprep(CurrentDir,SPATHSEP,255);
if (*Copy == PATHSEP)
{
strmaxcpy(CurrentDir, SPATHSEP, 255); strmov(Copy, Copy + 1);
}
#ifdef DRSEP
#ifdef __CYGWIN32__
/* win32 getcwd() does not deliver current drive letter, therefore only prepend a drive letter
if there was one before. */
if (*DrvPart)
#endif
{
strmaxprep(CurrentDir, SDRSEP, 255);
strmaxprep(CurrentDir, DrvPart, 255);
}
#endif
while ((p = strchr(Copy, PATHSEP)))
{
*p = '\0';
if (!strcmp(Copy, "."));
else if ((!strcmp(Copy, "..")) && (strlen(CurrentDir) > 1))
{
CurrentDir[strlen(CurrentDir) - 1] = '\0';
p2 = strrchr(CurrentDir, PATHSEP); p2[1] = '\0';
}
else
{
strmaxcat(CurrentDir, Copy, 255);
strmaxcat(CurrentDir, SPATHSEP, 255);
}
strmov(Copy, p + 1);
}
strmaxcat(CurrentDir, Copy, 255);
return CurrentDir;
}
char *FSearch(char *File, char *Path)
BEGIN
static String Component;
char *p,*start,Save='\0';
FILE *Dummy;
Boolean OK;
Dummy=fopen(File,"r"); OK=(Dummy!=Nil);
if (OK)
BEGIN
fclose(Dummy);
strmaxcpy(Component,File,255); return Component;
END
start=Path;
do
BEGIN
if (*start=='\0') break;
p=strchr(start,DIRSEP);
if (p!=Nil)
BEGIN
Save=(*p); *p='\0';
END
strmaxcpy(Component,start,255);
#ifdef __CYGWIN32__
DeCygwinPath(Component);
#endif
strmaxcat(Component,SPATHSEP,255);
strmaxcat(Component,File,255);
if (p!=Nil) *p=Save;
Dummy=fopen(Component,"r"); OK=(Dummy!=Nil);
if (OK)
BEGIN
fclose(Dummy);
return Component;
END
start=p+1;
END
while (p!=Nil);
*Component='\0'; return Component;
END
long FileSize(FILE *file)
BEGIN
long Save=ftell(file),Size;
fseek(file,0,SEEK_END);
Size=ftell(file);
fseek(file,Save,SEEK_SET);
return Size;
END
Byte Lo(Word inp)
BEGIN
return (inp&0xff);
END
Byte Hi(Word inp)
BEGIN
return ((inp>>8)&0xff);
END
Boolean Odd(int inp)
BEGIN
return ((inp&1)==1);
END
Boolean DirScan(char *Mask, charcallback callback)
BEGIN
char Name[1024];
#ifdef __MSDOS__
struct ffblk blk;
int res;
char *pos;
res=findfirst(Mask,&blk,FA_RDONLY|FA_HIDDEN|FA_SYSTEM|FA_LABEL|FA_DIREC|FA_ARCH);
if (res<0) return False;
pos=strrchr(Mask,PATHSEP); if (pos==Nil) pos=strrchr(Mask,DRSEP);
if (pos==Nil) pos=Mask; else pos++;
memcpy(Name,Mask,pos-Mask);
while (res==0)
BEGIN
if ((blk.ff_attrib&(FA_LABEL|FA_DIREC))==0)
BEGIN
strcpy(Name+(pos-Mask),blk.ff_name);
callback(Name);
END
res=findnext(&blk);
END
return True;
#else
#if defined ( __EMX__ ) || defined ( __IBMC__ )
HDIR hdir=1;
FILEFINDBUF3 buf;
ULONG rescnt;
USHORT res;
char *pos;
rescnt=1; res=DosFindFirst(Mask,&hdir,0x16,&buf,sizeof(buf),&rescnt,1);
if (res!=0) return False;
pos=strrchr(Mask,PATHSEP); if (pos==Nil) pos=strrchr(Mask,DRSEP);
if (pos==Nil) pos=Mask; else pos++;
memcpy(Name,Mask,pos-Mask);
while (res==0)
BEGIN
strcpy(Name+(pos-Mask),buf.achName); callback(Name);
res=DosFindNext(hdir,&buf,sizeof(buf),&rescnt);
END
return True;
#else
strmaxcpy(Name,Mask,255); callback(Name); return True;
#endif
#endif
END
LongInt MyGetFileTime(char *Name)
{
struct stat st;
if (stat(Name,&st)==-1) return 0;
else return st.st_mtime;
}
#ifdef __CYGWIN32__
/* convert CygWin-style paths back to something usable by other Win32 apps */
char *DeCygWinDirList(char *pStr)
{
char *pRun;
for (pRun = pStr; *pRun; pRun++)
if (*pRun == ':')
*pRun = ';';
return pStr;
}
char *DeCygwinPath(char *pStr)
{
char *pRun;
if ((strlen(pStr) >= 4)
&& (pStr[0] =='/') && (pStr[1] == '/') && (pStr[3] == '/')
&& (isalpha(pStr[2])))
{
strmov(pStr, pStr + 1);
pStr[0] = pStr[1];
pStr[1] = ':';
}
if ((strlen(pStr) >= 4)
&& (pStr[0] =='\\') && (pStr[1] == '\\') && (pStr[3] == '\\')
&& (isalpha(pStr[2])))
{
strmov(pStr, pStr + 1);
pStr[0] = pStr[1];
pStr[1] = ':';
}
for (pRun = pStr; *pRun; pRun++)
if (*pRun == '/')
*pRun = '\\';
return pStr;
}
#endif /* __CYGWIN32__ */
void bpemu_init(void)
BEGIN
END