forked from StarWolf3000/vasm-mirror
-
Notifications
You must be signed in to change notification settings - Fork 0
/
osdep.c
206 lines (169 loc) · 3.49 KB
/
osdep.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
/* osdep.c - OS-dependant routines */
/* (c) in 2018 by Frank Wille */
#include <stdlib.h>
#include <string.h>
/* from supp.c */
extern void *mymalloc(size_t);
extern char *mystrdup(char *);
#define MAX_WORKDIR_LEN 1024
#if defined(UNIX)
#include <unistd.h>
#elif defined(AMIGA)
#include <dos/dos.h>
#include <dos/dosextens.h>
#include <proto/dos.h>
#ifdef __amigaos4__
#include <dos/obsolete.h>
#endif
#elif defined(ATARI)
#include <tos.h>
#elif defined(_WIN32)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif
char *convert_path(char *path)
{
char *newpath;
#if defined(AMIGA)
char *p = newpath = mymalloc(strlen(path)+1);
while (*path) {
if (*path=='.') {
if (*(path+1)=='\0') {
path++;
continue;
}
else if (*(path+1)=='/' || *(path+1)=='\\') {
path += 2;
continue;
}
else if (*(path+1)=='.' &&
(*(path+2)=='/' || *(path+2)=='\\'))
path += 2;
}
if (*path == '\\') {
*p++ = '/';
path++;
}
else
*p++ = *path++;
}
*p = '\0';
#elif defined(MSDOS) || defined(ATARI) || defined(_WIN32)
char *p;
newpath = mystrdup(path);
for (p=newpath; *p; p++) {
if (*p == '/')
*p = '\\';
}
#else /* Unixish */
char *p;
newpath = mystrdup(path);
for (p=newpath; *p; p++) {
if (*p == '\\')
*p = '/';
}
#endif
return newpath;
}
char *append_path_delimiter(char *old)
{
int len = strlen(old);
char *new;
#if defined(AMIGA)
if (len>0 && old[len-1]!='/' && old[len-1]!=':') {
new = mymalloc(len+2);
strcpy(new,old);
new[len] = '/';
new[len+1] = '\0';
}
#elif defined(MSDOS) || defined(ATARI) || defined(_WIN32)
if (len>0 && old[len-1]!='\\' && old[len-1]!=':') {
new = mymalloc(len+2);
strcpy(new,old);
new[len] = '\\';
new[len+1] = '\0';
}
#else
if (len>0 && old[len-1]!='/') {
new = mymalloc(len+2);
strcpy(new,old);
new[len] = '/';
new[len+1] = '\0';
}
#endif
else
new = mystrdup(old);
return new;
}
char *remove_path_delimiter(char *old)
{
int len = strlen(old);
char *new;
#if defined(AMIGA)
if (len>1 && (old[len-1]=='/' || old[len-1]==':')) {
#elif defined(MSDOS) || defined(ATARI) || defined(_WIN32)
if (len>1 && (old[len-1]=='\\' || old[len-1]==':')) {
#else
if (len>1 && old[len-1]=='/') {
#endif
new = mymalloc(len);
memcpy(new,old,len-1);
new[len-1] = '\0';
}
else
new = mystrdup(old);
return new;
}
char *get_filepart(char *path)
{
char *filepart;
if ((filepart = strrchr(path,'/')) != NULL ||
(filepart = strrchr(path,'\\')) != NULL ||
(filepart = strrchr(path,':')) != NULL)
return filepart+1;
return path;
}
#if defined(UNIX)
char *get_workdir(void)
{
static char buf[MAX_WORKDIR_LEN];
if (getcwd(buf,MAX_WORKDIR_LEN) == NULL)
buf[0] = '\0';
return buf;
}
#elif defined(AMIGA)
char *get_workdir(void)
{
static char buf[MAX_WORKDIR_LEN];
#ifndef __amigaos4__
if (DOSBase->dl_lib.lib_Version < 36)
buf[0] = '\0';
else
#endif
if (!GetCurrentDirName(buf,MAX_WORKDIR_LEN))
buf[0] = '\0';
return buf;
}
#elif defined(ATARI)
char *get_workdir(void)
{
static char buf[MAX_WORKDIR_LEN];
WORD drv = Dgetdrv();
buf[0] = 'A' + drv;
buf[1] = ':';
Dgetpath(buf+2,drv+1);
return buf;
}
#elif defined(_WIN32)
char *get_workdir(void)
{
static char buf[MAX_WORKDIR_LEN];
GetCurrentDirectoryA(MAX_WORKDIR_LEN,buf);
return buf;
}
#else /* portable default */
char *get_workdir(void)
{
return "";
}
#endif