Skip to content

Latest commit

 

History

History
269 lines (216 loc) · 6.63 KB

platform_win32.c

File metadata and controls

269 lines (216 loc) · 6.63 KB
 
Jan 5, 2004
Jan 5, 2004
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
#include <windows.h>
#include <io.h>
#include <direct.h>
#include "platform.h"
int file_exists(const char *fname)
{
return(GetFileAttributes(fname) != 0xffffffff);
} /* file_exists */
int file_is_directory(const char *fname)
{
return((GetFileAttributes(fname) & FILE_ATTRIBUTE_DIRECTORY) != 0);
} /* file_is_directory */
int file_is_symlink(const char *fname)
{
return(0);
} /* file_is_symlink */
static const char *win32strerror(void)
{
static TCHAR msgbuf[255];
FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default language */
msgbuf,
sizeof (msgbuf) / sizeof (TCHAR),
NULL
);
return((const char *) msgbuf);
} /* win32strerror */
/* enumerate contents of (base) directory. */
file_list *make_filelist(const char *base)
{
file_list *retval = NULL;
file_list *l = NULL;
file_list *prev = NULL;
HANDLE dir;
WIN32_FIND_DATA ent;
char wildcard[MAX_PATH];
snprintf(wildcard, sizeof (wildcard), "%s\\*", base);
dir = FindFirstFile(wildcard, &ent);
if (dir == INVALID_HANDLE_VALUE)
{
fprintf(stderr, "Error: Can't list files in %s: %s.\n", base, win32strerror());
return(NULL);
} /* if */
while (FindNextFile(dir, &ent) != 0)
{
if (strcmp(ent.cFileName, ".") == 0)
continue;
if (strcmp(ent.cFileName, "..") == 0)
continue;
l = (file_list *) malloc(sizeof (file_list));
if (l == NULL)
{
fprintf(stderr, "Error: out of memory.\n");
break;
} /* if */
l->fname = (char *) malloc(strlen(ent.cFileName) + 1);
if (l->fname == NULL)
{
fprintf(stderr, "Error: out of memory.\n");
free(l);
break;
} /* if */
strcpy(l->fname, ent.cFileName);
if (retval == NULL)
retval = l;
else
prev->next = l;
prev = l;
l->next = NULL;
} /* while */
FindClose(dir);
return(retval);
} /* make_filelist */
int get_file_size(const char *fname, long *fsize)
{
DWORD FileSz;
DWORD FileSzHigh;
HANDLE hFil;
assert(fsize != NULL);
hFil = CreateFile(fname, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
if (hFil == INVALID_HANDLE_VALUE)
{
fprintf(stderr, "Error: Couldn't get size of [%s]: %s.\n", fname, win32strerror());
return(0);
} /* if */
FileSz = GetFileSize(hFil, &FileSzHigh);
assert(FileSzHigh == 0);
CloseHandle(hFil);
*fsize = FileSz;
return(1);
} /* get_file_size */
May 18, 2005
May 18, 2005
125
char *get_current_dir(char *buf, size_t bufsize);
Jan 5, 2004
Jan 5, 2004
126
{
May 18, 2005
May 18, 2005
127
128
DWORD buflen = GetCurrentDirectory(bufsize, buf);
if (buflen <= bufsize)
Jan 5, 2004
Jan 5, 2004
129
{
May 18, 2005
May 18, 2005
130
*buf = '\0';
May 18, 2005
May 18, 2005
131
return(NULL);
Jan 5, 2004
Jan 5, 2004
132
133
} /* if */
May 18, 2005
May 18, 2005
134
135
if (buf[buflen - 2] != '\\')
strcat(buf, "\\");
Jan 5, 2004
Jan 5, 2004
136
May 18, 2005
May 18, 2005
137
return(buf);
Jan 5, 2004
Jan 5, 2004
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
} /* get_current_dir */
char *get_realpath(const char *path)
{
char *retval = NULL;
char *p = NULL;
retval = (char *) malloc(MAX_PATH);
if (retval == NULL)
{
fprintf(stderr, "Error: out of memory.\n");
return(NULL);
} /* if */
/*
* If in \\server\path format, it's already an absolute path.
* We'll need to check for "." and ".." dirs, though, just in case.
*/
if ((path[0] == '\\') && (path[1] == '\\'))
strcpy(retval, path);
else
{
char *currentDir = get_current_dir();
if (currentDir == NULL)
{
free(retval);
fprintf(stderr, "Error: out of memory.\n");
return(NULL);
} /* if */
if (path[1] == ':') /* drive letter specified? */
{
/*
* Apparently, "D:mypath" is the same as "D:\\mypath" if
* D: is not the current drive. However, if D: is the
* current drive, then "D:mypath" is a relative path. Ugh.
*/
if (path[2] == '\\') /* maybe an absolute path? */
strcpy(retval, path);
else /* definitely an absolute path. */
{
if (path[0] == currentDir[0]) /* current drive; relative. */
{
strcpy(retval, currentDir);
strcat(retval, path + 2);
} /* if */
else /* not current drive; absolute. */
{
retval[0] = path[0];
retval[1] = ':';
retval[2] = '\\';
strcpy(retval + 3, path + 2);
} /* else */
} /* else */
} /* if */
else /* no drive letter specified. */
{
if (path[0] == '\\') /* absolute path. */
{
retval[0] = currentDir[0];
retval[1] = ':';
strcpy(retval + 2, path);
} /* if */
else
{
strcpy(retval, currentDir);
strcat(retval, path);
} /* else */
} /* else */
free(currentDir);
} /* else */
/* (whew.) Ok, now take out "." and ".." path entries... */
p = retval;
while ( (p = strstr(p, "\\.")) != NULL)
{
/* it's a "." entry that doesn't end the string. */
if (p[2] == '\\')
memmove(p + 1, p + 3, strlen(p + 3) + 1);
/* it's a "." entry that ends the string. */
else if (p[2] == '\0')
p[0] = '\0';
/* it's a ".." entry. */
else if (p[2] == '.')
{
char *prevEntry = p - 1;
while ((prevEntry != retval) && (*prevEntry != '\\'))
prevEntry--;
if (prevEntry == retval) /* make it look like a "." entry. */
memmove(p + 1, p + 2, strlen(p + 2) + 1);
else
{
if (p[3] != '\0') /* doesn't end string. */
*prevEntry = '\0';
else /* ends string. */
memmove(prevEntry + 1, p + 4, strlen(p + 4) + 1);
p = prevEntry;
} /* else */
} /* else if */
else
{
p++; /* look past current char. */
} /* else */
} /* while */
/* shrink the retval's memory block if possible... */
p = (char *) realloc(retval, strlen(retval) + 1);
if (p != NULL)
retval = p;
return(retval);
} /* get_realpath */
int main(int argc, char **argv) /* !!! FIXME: WinMain, in the future? */
{
return(mojopatch_main(argc, argv));
} /* main */
/* end of platform_win32.c ... */