| 1 |
/* Emacs style mode select -*- C -*- |
| 2 |
*----------------------------------------------------------------------------- |
| 3 |
* |
| 4 |
* |
| 5 |
* PrBoom: a Doom port merged with LxDoom and LSDLDoom |
| 6 |
* based on BOOM, a modified and improved DOOM engine |
| 7 |
* Copyright (C) 1999 by |
| 8 |
* id Software, Chi Hoang, Lee Killough, Jim Flynn, Rand Phares, Ty Halderman |
| 9 |
* Copyright (C) 1999-2000 by |
| 10 |
* Jess Haas, Nicolas Kalkhof, Colin Phipps, Florian Schulze |
| 11 |
* Copyright 2005, 2006 by |
| 12 |
* Florian Schulze, Colin Phipps, Neil Stevens, Andrey Budko |
| 13 |
* |
| 14 |
* This program is free software; you can redistribute it and/or |
| 15 |
* modify it under the terms of the GNU General Public License |
| 16 |
* as published by the Free Software Foundation; either version 2 |
| 17 |
* of the License, or (at your option) any later version. |
| 18 |
* |
| 19 |
* This program is distributed in the hope that it will be useful, |
| 20 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 |
* GNU General Public License for more details. |
| 23 |
* |
| 24 |
* You should have received a copy of the GNU General Public License |
| 25 |
* along with this program; if not, write to the Free Software |
| 26 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 27 |
* 02111-1307, USA. |
| 28 |
* |
| 29 |
* DESCRIPTION: |
| 30 |
* External simple file handling. |
| 31 |
* |
| 32 |
*-----------------------------------------------------------------------------*/ |
| 33 |
|
| 34 |
#ifdef _WIN32 |
| 35 |
#define WIN32_LEAN_AND_MEAN |
| 36 |
#include <windows.h> |
| 37 |
#include <io.h> |
| 38 |
#include <direct.h> |
| 39 |
#endif |
| 40 |
|
| 41 |
#ifdef HAVE_CONFIG_H |
| 42 |
#include "config.h" |
| 43 |
#endif |
| 44 |
|
| 45 |
#ifdef HAVE_UNISTD_H |
| 46 |
#include <unistd.h> |
| 47 |
#endif |
| 48 |
|
| 49 |
#include <stdlib.h> |
| 50 |
#include <string.h> |
| 51 |
#include <fcntl.h> |
| 52 |
#include <sys/stat.h> |
| 53 |
#include <errno.h> |
| 54 |
|
| 55 |
#include "i_glob.h" |
| 56 |
#include "lprintf.h" |
| 57 |
#include "z_zone.h" |
| 58 |
|
| 59 |
#include "m_file.h" |
| 60 |
|
| 61 |
#ifdef _MSC_VER |
| 62 |
#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) |
| 63 |
#define F_OK 0 |
| 64 |
#define W_OK 2 |
| 65 |
#define R_OK 4 |
| 66 |
#endif |
| 67 |
|
| 68 |
#ifndef O_BINARY |
| 69 |
#define O_BINARY 0 |
| 70 |
#endif |
| 71 |
|
| 72 |
#define MKDIR_NO_ERROR 0 |
| 73 |
|
| 74 |
#ifdef _WIN32 |
| 75 |
static wchar_t *ConvertMultiByteToWide(const char *str, UINT code_page) { |
| 76 |
wchar_t *wstr = NULL; |
| 77 |
int wlen = 0; |
| 78 |
|
| 79 |
wlen = MultiByteToWideChar(code_page, 0, str, -1, NULL, 0); |
| 80 |
|
| 81 |
if (!wlen) { |
| 82 |
errno = EINVAL; |
| 83 |
lprintf(LO_INFO, "Warning: Failed to convert path to wide encoding\n"); |
| 84 |
return NULL; |
| 85 |
} |
| 86 |
|
| 87 |
wstr = Z_Malloc(sizeof(wchar_t) * wlen); |
| 88 |
|
| 89 |
if (!wstr) { |
| 90 |
lprintf(LO_INFO, "ConvertMultiByteToWide: Failed to allocate new string\n"); |
| 91 |
return NULL; |
| 92 |
} |
| 93 |
|
| 94 |
if (MultiByteToWideChar(code_page, 0, str, -1, wstr, wlen) == 0) { |
| 95 |
errno = EINVAL; |
| 96 |
lprintf(LO_INFO, "Warning: Failed to convert path to wide encoding\n"); |
| 97 |
Z_Free(wstr); |
| 98 |
return NULL; |
| 99 |
} |
| 100 |
|
| 101 |
return wstr; |
| 102 |
} |
| 103 |
|
| 104 |
static char *ConvertWideToMultiByte(const wchar_t *wstr, UINT code_page) { |
| 105 |
char *str = NULL; |
| 106 |
int len = 0; |
| 107 |
|
| 108 |
len = WideCharToMultiByte(code_page, 0, wstr, -1, NULL, 0, NULL, NULL); |
| 109 |
|
| 110 |
if (!len) { |
| 111 |
errno = EINVAL; |
| 112 |
lprintf(LO_INFO, "Warning: Failed to convert path to multi byte encoding\n"); |
| 113 |
return NULL; |
| 114 |
} |
| 115 |
|
| 116 |
str = Z_Malloc(sizeof(char) * len); |
| 117 |
|
| 118 |
if (!str) { |
| 119 |
lprintf(LO_INFO, "ConvertWideToMultiByte: Failed to allocate new string\n"); |
| 120 |
return NULL; |
| 121 |
} |
| 122 |
|
| 123 |
if (WideCharToMultiByte(code_page, 0, wstr, -1, str, len, NULL, NULL) == 0) { |
| 124 |
errno = EINVAL; |
| 125 |
lprintf(LO_INFO, "Warning: Failed to convert path to multi byte encoding\n"); |
| 126 |
Z_Free(str); |
| 127 |
return NULL; |
| 128 |
} |
| 129 |
|
| 130 |
return str; |
| 131 |
} |
| 132 |
|
| 133 |
wchar_t *ConvertUtf8ToWide(const char *str) { |
| 134 |
return ConvertMultiByteToWide(str, CP_UTF8); |
| 135 |
} |
| 136 |
|
| 137 |
char *ConvertWideToUtf8(const wchar_t *wstr) { |
| 138 |
return ConvertWideToMultiByte(wstr, CP_UTF8); |
| 139 |
} |
| 140 |
|
| 141 |
static wchar_t *ConvertSysNativeMBToWide(const char *str) { |
| 142 |
return ConvertMultiByteToWide(str, CP_ACP); |
| 143 |
} |
| 144 |
|
| 145 |
static char *ConvertWideToSysNativeMB(const wchar_t *wstr) { |
| 146 |
return ConvertWideToMultiByte(wstr, CP_ACP); |
| 147 |
} |
| 148 |
#endif |
| 149 |
|
| 150 |
char *ConvertSysNativeMBToUtf8(const char *str) { |
| 151 |
#ifdef _WIN32 |
| 152 |
char *ret = NULL; |
| 153 |
wchar_t *wstr = NULL; |
| 154 |
|
| 155 |
wstr = ConvertSysNativeMBToWide(str); |
| 156 |
|
| 157 |
if (!wstr) |
| 158 |
return NULL; |
| 159 |
|
| 160 |
ret = ConvertWideToUtf8(wstr); |
| 161 |
|
| 162 |
Z_Free(wstr); |
| 163 |
|
| 164 |
return ret; |
| 165 |
#else |
| 166 |
return Z_Strdup(str); |
| 167 |
#endif |
| 168 |
} |
| 169 |
|
| 170 |
char *ConvertUtf8ToSysNativeMB(const char *str) { |
| 171 |
#ifdef _WIN32 |
| 172 |
char *ret = NULL; |
| 173 |
wchar_t *wstr = NULL; |
| 174 |
|
| 175 |
wstr = ConvertUtf8ToWide(str); |
| 176 |
|
| 177 |
if (!wstr) |
| 178 |
return NULL; |
| 179 |
|
| 180 |
ret = ConvertWideToSysNativeMB(wstr); |
| 181 |
|
| 182 |
Z_Free(wstr); |
| 183 |
|
| 184 |
return ret; |
| 185 |
#else |
| 186 |
return Z_Strdup(str); |
| 187 |
#endif |
| 188 |
} |
| 189 |
|
| 190 |
static int M_open(const char *filename, int oflag) { |
| 191 |
#ifdef _WIN32 |
| 192 |
wchar_t *wname; |
| 193 |
int ret; |
| 194 |
|
| 195 |
wname = ConvertUtf8ToWide(filename); |
| 196 |
|
| 197 |
if (!wname) |
| 198 |
return 0; |
| 199 |
|
| 200 |
ret = _wopen(wname, oflag); |
| 201 |
|
| 202 |
Z_Free(wname); |
| 203 |
|
| 204 |
return ret; |
| 205 |
#else |
| 206 |
return open(filename, oflag); |
| 207 |
#endif |
| 208 |
} |
| 209 |
|
| 210 |
static int M_access(const char *path, int mode) { |
| 211 |
#ifdef _WIN32 |
| 212 |
wchar_t *wpath; |
| 213 |
int ret; |
| 214 |
|
| 215 |
wpath = ConvertUtf8ToWide(path); |
| 216 |
|
| 217 |
if (!wpath) |
| 218 |
return 0; |
| 219 |
|
| 220 |
ret = _waccess(wpath, mode); |
| 221 |
|
| 222 |
Z_Free(wpath); |
| 223 |
|
| 224 |
return ret; |
| 225 |
#else |
| 226 |
return access(path, mode); |
| 227 |
#endif |
| 228 |
} |
| 229 |
|
| 230 |
static int M_mkdir(const char *path) { |
| 231 |
#ifdef _WIN32 |
| 232 |
wchar_t *wdir = NULL; |
| 233 |
int ret; |
| 234 |
|
| 235 |
wdir = ConvertUtf8ToWide(path); |
| 236 |
|
| 237 |
if (!wdir) |
| 238 |
return -1; |
| 239 |
|
| 240 |
ret = _wmkdir(wdir); |
| 241 |
|
| 242 |
Z_Free(wdir); |
| 243 |
|
| 244 |
return ret; |
| 245 |
#else |
| 246 |
return mkdir(path, 0755); |
| 247 |
#endif |
| 248 |
} |
| 249 |
|
| 250 |
static int M_stat(const char *path, struct stat *buf) |
| 251 |
{ |
| 252 |
#ifdef _WIN32 |
| 253 |
wchar_t *wpath; |
| 254 |
struct _stat wbuf; |
| 255 |
int ret; |
| 256 |
|
| 257 |
wpath = ConvertUtf8ToWide(path); |
| 258 |
|
| 259 |
if (!wpath) |
| 260 |
return -1; |
| 261 |
|
| 262 |
ret = _wstat(wpath, &wbuf); |
| 263 |
|
| 264 |
// The _wstat() function expects a struct _stat* parameter that is |
| 265 |
// incompatible with struct stat*. We copy only the required compatible |
| 266 |
// field. |
| 267 |
buf->st_mode = wbuf.st_mode; |
| 268 |
buf->st_mtime = wbuf.st_mtime; |
| 269 |
|
| 270 |
Z_Free(wpath); |
| 271 |
|
| 272 |
return ret; |
| 273 |
#else |
| 274 |
return stat(path, buf); |
| 275 |
#endif |
| 276 |
} |
| 277 |
|
| 278 |
int M_remove(const char *path) |
| 279 |
{ |
| 280 |
#ifdef _WIN32 |
| 281 |
wchar_t *wpath; |
| 282 |
int ret; |
| 283 |
|
| 284 |
wpath = ConvertUtf8ToWide(path); |
| 285 |
|
| 286 |
if (!wpath) |
| 287 |
return -1; |
| 288 |
|
| 289 |
ret = _wremove(wpath); |
| 290 |
|
| 291 |
Z_Free(wpath); |
| 292 |
|
| 293 |
return ret; |
| 294 |
#else |
| 295 |
return remove(path); |
| 296 |
#endif |
| 297 |
} |
| 298 |
|
| 299 |
int M_MakeDir(const char *path, int require) { |
| 300 |
int error; |
| 301 |
|
| 302 |
if (M_IsDir(path)) |
| 303 |
return MKDIR_NO_ERROR; |
| 304 |
|
| 305 |
error = M_mkdir(path); |
| 306 |
|
| 307 |
if (require && error) |
| 308 |
I_Error("Unable to create directory %s (%d)", path, errno); |
| 309 |
|
| 310 |
return error; |
| 311 |
} |
| 312 |
|
| 313 |
dboolean M_IsDir(const char *name) |
| 314 |
{ |
| 315 |
struct stat sbuf; |
| 316 |
|
| 317 |
return !M_stat(name, &sbuf) && S_ISDIR(sbuf.st_mode); |
| 318 |
} |
| 319 |
|
| 320 |
dboolean M_ReadWriteAccess(const char *name) |
| 321 |
{ |
| 322 |
return !M_access(name, R_OK | W_OK); |
| 323 |
} |
| 324 |
|
| 325 |
dboolean M_ReadAccess(const char *name) |
| 326 |
{ |
| 327 |
return !M_access(name, R_OK); |
| 328 |
} |
| 329 |
|
| 330 |
dboolean M_WriteAccess(const char *name) |
| 331 |
{ |
| 332 |
return !M_access(name, W_OK); |
| 333 |
} |
| 334 |
|
| 335 |
FILE* M_OpenFile(const char *name, const char *mode) |
| 336 |
{ |
| 337 |
#ifdef _WIN32 |
| 338 |
FILE *file; |
| 339 |
wchar_t *wname, *wmode; |
| 340 |
|
| 341 |
wname = ConvertUtf8ToWide(name); |
| 342 |
|
| 343 |
if (!wname) |
| 344 |
return NULL; |
| 345 |
|
| 346 |
wmode = ConvertUtf8ToWide(mode); |
| 347 |
|
| 348 |
if (!wmode) |
| 349 |
{ |
| 350 |
Z_Free(wname); |
| 351 |
return NULL; |
| 352 |
} |
| 353 |
|
| 354 |
file = _wfopen(wname, wmode); |
| 355 |
|
| 356 |
Z_Free(wname); |
| 357 |
Z_Free(wmode); |
| 358 |
|
| 359 |
return file; |
| 360 |
#else |
| 361 |
return fopen(name, mode); |
| 362 |
#endif |
| 363 |
} |
| 364 |
|
| 365 |
int M_OpenRB(const char *name) |
| 366 |
{ |
| 367 |
return M_open(name, O_RDONLY | O_BINARY); |
| 368 |
} |
| 369 |
|
| 370 |
dboolean M_FileExists(const char *name) |
| 371 |
{ |
| 372 |
FILE* fp; |
| 373 |
|
| 374 |
fp = M_OpenFile(name, "rb"); |
| 375 |
|
| 376 |
if (fp) |
| 377 |
{ |
| 378 |
fclose(fp); |
| 379 |
|
| 380 |
return true; |
| 381 |
} |
| 382 |
|
| 383 |
return false; |
| 384 |
} |
| 385 |
|
| 386 |
/* |
| 387 |
* M_WriteFile |
| 388 |
* |
| 389 |
* killough 9/98: rewritten to use stdio and to flash disk icon |
| 390 |
*/ |
| 391 |
|
| 392 |
dboolean M_WriteFile(char const *name, const void *source, size_t length) |
| 393 |
{ |
| 394 |
FILE *fp; |
| 395 |
|
| 396 |
errno = 0; |
| 397 |
|
| 398 |
if (!(fp = M_OpenFile(name, "wb"))) // Try opening file |
| 399 |
return 0; // Could not open file for writing |
| 400 |
|
| 401 |
length = fwrite(source, 1, length, fp) == (size_t)length; // Write data |
| 402 |
fclose(fp); |
| 403 |
|
| 404 |
if (!length) // Remove partially written file |
| 405 |
M_remove(name); |
| 406 |
|
| 407 |
return length; |
| 408 |
} |
| 409 |
|
| 410 |
/* |
| 411 |
* M_ReadFile |
| 412 |
* |
| 413 |
* killough 9/98: rewritten to use stdio and to flash disk icon |
| 414 |
*/ |
| 415 |
|
| 416 |
int M_ReadFile(char const *name, byte **buffer) |
| 417 |
{ |
| 418 |
FILE *fp; |
| 419 |
|
| 420 |
if ((fp = M_OpenFile(name, "rb"))) |
| 421 |
{ |
| 422 |
size_t length; |
| 423 |
|
| 424 |
fseek(fp, 0, SEEK_END); |
| 425 |
length = ftell(fp); |
| 426 |
fseek(fp, 0, SEEK_SET); |
| 427 |
*buffer = Z_Malloc(length); |
| 428 |
if (fread(*buffer, 1, length, fp) == length) |
| 429 |
{ |
| 430 |
fclose(fp); |
| 431 |
return length; |
| 432 |
} |
| 433 |
fclose(fp); |
| 434 |
} |
| 435 |
|
| 436 |
/* cph 2002/08/10 - this used to return 0 on error, but that's ambiguous, |
| 437 |
* because we could have a legit 0-length file. So make it -1. */ |
| 438 |
return -1; |
| 439 |
} |
| 440 |
|
| 441 |
// Same as above, but add null terminator |
| 442 |
int M_ReadFileToString(char const *name, char **buffer) { |
| 443 |
FILE *fp; |
| 444 |
|
| 445 |
if ((fp = M_OpenFile(name, "rb"))) |
| 446 |
{ |
| 447 |
size_t length; |
| 448 |
|
| 449 |
fseek(fp, 0, SEEK_END); |
| 450 |
length = ftell(fp); |
| 451 |
fseek(fp, 0, SEEK_SET); |
| 452 |
*buffer = Z_Malloc(length + 1); |
| 453 |
if (fread(*buffer, 1, length, fp) == length) |
| 454 |
{ |
| 455 |
fclose(fp); |
| 456 |
(*buffer)[length] = '\0'; |
| 457 |
return length; |
| 458 |
} |
| 459 |
Z_Free(*buffer); |
| 460 |
*buffer = NULL; |
| 461 |
fclose(fp); |
| 462 |
} |
| 463 |
|
| 464 |
/* cph 2002/08/10 - this used to return 0 on error, but that's ambiguous, |
| 465 |
* because we could have a legit 0-length file. So make it -1. */ |
| 466 |
return -1; |
| 467 |
} |
| 468 |
|
| 469 |
char *M_getcwd(char *buffer, int len) |
| 470 |
{ |
| 471 |
#ifdef _WIN32 |
| 472 |
wchar_t *wret; |
| 473 |
char *ret; |
| 474 |
|
| 475 |
wret = _wgetcwd(NULL, 0); |
| 476 |
|
| 477 |
if (!wret) |
| 478 |
return NULL; |
| 479 |
|
| 480 |
ret = ConvertWideToUtf8(wret); |
| 481 |
|
| 482 |
free(wret); |
| 483 |
|
| 484 |
if (!ret) |
| 485 |
return NULL; |
| 486 |
|
| 487 |
if (!buffer) |
| 488 |
return ret; |
| 489 |
|
| 490 |
if (strlen(ret) >= len) { |
| 491 |
Z_Free(ret); |
| 492 |
return NULL; |
| 493 |
} |
| 494 |
|
| 495 |
strcpy(buffer, ret); |
| 496 |
Z_Free(ret); |
| 497 |
|
| 498 |
return buffer; |
| 499 |
#else |
| 500 |
return getcwd(buffer, len); |
| 501 |
#endif |
| 502 |
} |
| 503 |
|
| 504 |
#ifdef _WIN32 |
| 505 |
typedef struct { |
| 506 |
char *var; |
| 507 |
const char *name; |
| 508 |
} env_var_t; |
| 509 |
|
| 510 |
static env_var_t *env_vars; |
| 511 |
static int num_vars; |
| 512 |
#endif |
| 513 |
|
| 514 |
char *M_getenv(const char *name) { |
| 515 |
#ifdef _WIN32 |
| 516 |
int i; |
| 517 |
wchar_t *wenv, *wname; |
| 518 |
char *env; |
| 519 |
|
| 520 |
for (i = 0; i < num_vars; ++i) { |
| 521 |
if (!strcasecmp(name, env_vars[i].name)) |
| 522 |
return env_vars[i].var; |
| 523 |
} |
| 524 |
|
| 525 |
wname = ConvertUtf8ToWide(name); |
| 526 |
|
| 527 |
if (!wname) |
| 528 |
return NULL; |
| 529 |
|
| 530 |
wenv = _wgetenv(wname); |
| 531 |
|
| 532 |
Z_Free(wname); |
| 533 |
|
| 534 |
if (wenv) |
| 535 |
env = ConvertWideToUtf8(wenv); |
| 536 |
else |
| 537 |
env = NULL; |
| 538 |
|
| 539 |
env_vars = Z_Realloc(env_vars, (num_vars + 1) * sizeof(*env_vars)); |
| 540 |
env_vars[num_vars].var = env; |
| 541 |
env_vars[num_vars].name = Z_Strdup(name); |
| 542 |
num_vars++; |
| 543 |
|
| 544 |
return env; |
| 545 |
#else |
| 546 |
return getenv(name); |
| 547 |
#endif |
| 548 |
} |
| 549 |
|
| 550 |
dboolean M_RemoveFilesAtPath(const char *path) |
| 551 |
{ |
| 552 |
glob_t *glob; |
| 553 |
const char *filename; |
| 554 |
dboolean success = true; |
| 555 |
|
| 556 |
glob = I_StartGlob(path, "*.*", GLOB_FLAG_NOCASE | GLOB_FLAG_SORTED); |
| 557 |
|
| 558 |
for(;;) { |
| 559 |
filename = I_NextGlob(glob); |
| 560 |
if (filename == NULL) |
| 561 |
break; |
| 562 |
|
| 563 |
if (M_remove(filename) != 0) { |
| 564 |
lprintf(LO_ERROR, "M_RemoveFilesAtPath: unable to delete file %s\n", filename); |
| 565 |
success = false; |
| 566 |
break; |
| 567 |
} |
| 568 |
} |
| 569 |
I_EndGlob(glob); |
| 570 |
return success; |
| 571 |
} |