physfs_platform_os2.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. /*
  2. * OS/2 support routines for PhysicsFS.
  3. *
  4. * Please see the file LICENSE.txt in the source's root directory.
  5. *
  6. * This file written by Ryan C. Gordon.
  7. */
  8. #define __PHYSICSFS_INTERNAL__
  9. #include "physfs_platforms.h"
  10. #ifdef PHYSFS_PLATFORM_OS2
  11. #define INCL_DOSMODULEMGR
  12. #define INCL_DOSSEMAPHORES
  13. #define INCL_DOSDATETIME
  14. #define INCL_DOSFILEMGR
  15. #define INCL_DOSMODULEMGR
  16. #define INCL_DOSERRORS
  17. #define INCL_DOSPROCESS
  18. #define INCL_DOSDEVICES
  19. #define INCL_DOSDEVIOCTL
  20. #define INCL_DOSMISC
  21. #include <os2.h>
  22. #include <uconv.h>
  23. #include <errno.h>
  24. #include <time.h>
  25. #include <ctype.h>
  26. #include "physfs_internal.h"
  27. static HMODULE uconvdll = 0;
  28. static UconvObject uconv = 0;
  29. static int (_System *pUniCreateUconvObject)(UniChar *, UconvObject *) = NULL;
  30. static int (_System *pUniFreeUconvObject)(UconvObject *) = NULL;
  31. static int (_System *pUniUconvToUcs)(UconvObject,void **,size_t *, UniChar**, size_t *, size_t *) = NULL;
  32. static int (_System *pUniUconvFromUcs)(UconvObject,UniChar **,size_t *,void **,size_t *,size_t *) = NULL;
  33. static PHYSFS_ErrorCode errcodeFromAPIRET(const APIRET rc)
  34. {
  35. switch (rc)
  36. {
  37. case NO_ERROR: return PHYSFS_ERR_OK; /* not an error. */
  38. case ERROR_INTERRUPT: return PHYSFS_ERR_OK; /* not an error. */
  39. case ERROR_TIMEOUT: return PHYSFS_ERR_OK; /* not an error. */
  40. case ERROR_NOT_ENOUGH_MEMORY: return PHYSFS_ERR_OUT_OF_MEMORY;
  41. case ERROR_FILE_NOT_FOUND: return PHYSFS_ERR_NOT_FOUND;
  42. case ERROR_PATH_NOT_FOUND: return PHYSFS_ERR_NOT_FOUND;
  43. case ERROR_ACCESS_DENIED: return PHYSFS_ERR_PERMISSION;
  44. case ERROR_NOT_DOS_DISK: return PHYSFS_ERR_NOT_FOUND;
  45. case ERROR_SHARING_VIOLATION: return PHYSFS_ERR_PERMISSION;
  46. case ERROR_CANNOT_MAKE: return PHYSFS_ERR_IO; /* maybe this is wrong? */
  47. case ERROR_DEVICE_IN_USE: return PHYSFS_ERR_BUSY;
  48. case ERROR_OPEN_FAILED: return PHYSFS_ERR_IO; /* maybe this is wrong? */
  49. case ERROR_DISK_FULL: return PHYSFS_ERR_NO_SPACE;
  50. case ERROR_PIPE_BUSY: return PHYSFS_ERR_BUSY;
  51. case ERROR_SHARING_BUFFER_EXCEEDED: return PHYSFS_ERR_IO;
  52. case ERROR_FILENAME_EXCED_RANGE: return PHYSFS_ERR_BAD_FILENAME;
  53. case ERROR_META_EXPANSION_TOO_LONG: return PHYSFS_ERR_BAD_FILENAME;
  54. case ERROR_TOO_MANY_HANDLES: return PHYSFS_ERR_IO;
  55. case ERROR_TOO_MANY_OPEN_FILES: return PHYSFS_ERR_IO;
  56. case ERROR_NO_MORE_SEARCH_HANDLES: return PHYSFS_ERR_IO;
  57. case ERROR_SEEK_ON_DEVICE: return PHYSFS_ERR_IO;
  58. case ERROR_NEGATIVE_SEEK: return PHYSFS_ERR_INVALID_ARGUMENT;
  59. case ERROR_WRITE_PROTECT: return PHYSFS_ERR_PERMISSION;
  60. case ERROR_WRITE_FAULT: return PHYSFS_ERR_IO;
  61. case ERROR_UNCERTAIN_MEDIA: return PHYSFS_ERR_IO;
  62. case ERROR_PROTECTION_VIOLATION: return PHYSFS_ERR_IO;
  63. case ERROR_BROKEN_PIPE: return PHYSFS_ERR_IO;
  64. /* !!! FIXME: some of these might be PHYSFS_ERR_BAD_FILENAME, etc */
  65. case ERROR_LOCK_VIOLATION:
  66. case ERROR_GEN_FAILURE:
  67. case ERROR_INVALID_PARAMETER:
  68. case ERROR_INVALID_NAME:
  69. case ERROR_INVALID_DRIVE:
  70. case ERROR_INVALID_HANDLE:
  71. case ERROR_INVALID_FUNCTION:
  72. case ERROR_INVALID_LEVEL:
  73. case ERROR_INVALID_CATEGORY:
  74. case ERROR_DUPLICATE_NAME:
  75. case ERROR_BUFFER_OVERFLOW:
  76. case ERROR_BAD_LENGTH:
  77. case ERROR_BAD_DRIVER_LEVEL:
  78. case ERROR_DIRECT_ACCESS_HANDLE:
  79. case ERROR_NOT_OWNER:
  80. return PHYSFS_ERR_OS_ERROR;
  81. default: break;
  82. } /* switch */
  83. return PHYSFS_ERR_OTHER_ERROR;
  84. } /* errcodeFromAPIRET */
  85. static char *cvtUtf8ToCodepage(const char *utf8str)
  86. {
  87. if (uconvdll)
  88. {
  89. int rc;
  90. size_t len = strlen(utf8str) + 1;
  91. const size_t uc2buflen = len * sizeof (UniChar);
  92. UniChar *uc2ptr = (UniChar *) __PHYSFS_smallAlloc(uc2buflen);
  93. UniChar *uc2str = uc2ptr;
  94. char *cpptr = NULL;
  95. char *cpstr = NULL;
  96. size_t subs = 0;
  97. size_t unilen;
  98. size_t cplen;
  99. GOTO_IF(!uc2str, PHYSFS_ERR_OUT_OF_MEMORY, failed);
  100. PHYSFS_utf8ToUcs2(utf8str, (PHYSFS_uint16 *) uc2str, uc2buflen);
  101. for (unilen = 0; uc2str[unilen]; unilen++) { /* spin */ }
  102. unilen++; /* null terminator. */
  103. cplen = unilen * 4; /* overallocate, just in case. */
  104. cpptr = (char *) allocator.Malloc(cplen);
  105. GOTO_IF(!cpptr, PHYSFS_ERR_OUT_OF_MEMORY, failed);
  106. cpstr = cpptr;
  107. rc = pUniUconvFromUcs(uconv, &uc2str, &unilen, (void **) &cpstr, &cplen, &subs);
  108. GOTO_IF(rc != ULS_SUCCESS, PHYSFS_ERR_BAD_FILENAME, failed);
  109. GOTO_IF(subs > 0, PHYSFS_ERR_BAD_FILENAME, failed);
  110. assert(unilen == 0);
  111. return cpptr;
  112. failed:
  113. __PHYSFS_smallFree(uc2ptr);
  114. allocator.Free(cpptr);
  115. } /* if */
  116. return NULL;
  117. } /* cvtUtf8ToCodepage */
  118. static char *cvtCodepageToUtf8(const char *cpstr)
  119. {
  120. char *retval = NULL;
  121. if (uconvdll)
  122. {
  123. int rc;
  124. size_t len = strlen(cpstr) + 1;
  125. size_t cplen = len;
  126. size_t unilen = len;
  127. size_t subs = 0;
  128. UniChar *uc2ptr = __PHYSFS_smallAlloc(len * sizeof (UniChar));
  129. UniChar *uc2str = uc2ptr;
  130. BAIL_IF(!uc2ptr, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  131. rc = pUniUconvToUcs(uconv, (void **) &cpstr, &cplen, &uc2str, &unilen, &subs);
  132. GOTO_IF(rc != ULS_SUCCESS, PHYSFS_ERR_BAD_FILENAME, done);
  133. GOTO_IF(subs > 0, PHYSFS_ERR_BAD_FILENAME, done);
  134. assert(cplen == 0);
  135. retval = (char *) allocator.Malloc(len * 4);
  136. GOTO_IF(!retval, PHYSFS_ERR_OUT_OF_MEMORY, done);
  137. PHYSFS_utf8FromUcs2((const PHYSFS_uint16 *) uc2ptr, retval, len * 4);
  138. done:
  139. __PHYSFS_smallFree(uc2ptr);
  140. } /* if */
  141. return retval;
  142. } /* cvtCodepageToUtf8 */
  143. /* (be gentle, this function isn't very robust.) */
  144. static char *cvtPathToCorrectCase(char *buf)
  145. {
  146. char *retval = buf;
  147. char *fname = buf + 3; /* point to first element. */
  148. char *ptr = strchr(fname, '\\'); /* find end of first element. */
  149. buf[0] = toupper(buf[0]); /* capitalize drive letter. */
  150. /*
  151. * Go through each path element, and enumerate its parent dir until
  152. * a case-insensitive match is found. If one is (and it SHOULD be)
  153. * then overwrite the original element with the correct case.
  154. * If there's an error, or the path has vanished for some reason, it
  155. * won't hurt to have the original case, so we just keep going.
  156. */
  157. while ((fname != NULL) && (*fname != '\0'))
  158. {
  159. char spec[CCHMAXPATH];
  160. FILEFINDBUF3 fb;
  161. HDIR hdir = HDIR_CREATE;
  162. ULONG count = 1;
  163. APIRET rc;
  164. *(fname - 1) = '\0'; /* isolate parent dir string. */
  165. strcpy(spec, buf); /* copy isolated parent dir... */
  166. strcat(spec, "\\*.*"); /* ...and add wildcard search spec. */
  167. if (ptr != NULL) /* isolate element to find (fname is the start). */
  168. *ptr = '\0';
  169. rc = DosFindFirst((unsigned char *) spec, &hdir, FILE_DIRECTORY,
  170. &fb, sizeof (fb), &count, FIL_STANDARD);
  171. if (rc == NO_ERROR)
  172. {
  173. while (count == 1) /* while still entries to enumerate... */
  174. {
  175. int cmp;
  176. char *utf8 = cvtCodepageToUtf8(fb.achName);
  177. if (!utf8) /* ugh, maybe we'll get lucky with the C runtime. */
  178. cmp = stricmp(utf8, fname);
  179. else
  180. {
  181. cmp = PHYSFS_utf8stricmp(utf8, fname);
  182. allocator.Free(utf8);
  183. } /* else */
  184. if (cmp == 0)
  185. {
  186. strcpy(fname, fb.achName);
  187. break; /* there it is. Overwrite and stop searching. */
  188. } /* if */
  189. DosFindNext(hdir, &fb, sizeof (fb), &count);
  190. } /* while */
  191. DosFindClose(hdir);
  192. } /* if */
  193. *(fname - 1) = '\\'; /* unisolate parent dir. */
  194. fname = ptr; /* point to next element. */
  195. if (ptr != NULL)
  196. {
  197. *ptr = '\\'; /* unisolate element. */
  198. ptr = strchr(++fname, '\\'); /* find next element. */
  199. } /* if */
  200. } /* while */
  201. return retval;
  202. } /* cvtPathToCorrectCase */
  203. static void prepUnicodeSupport(void)
  204. {
  205. /* really old OS/2 might not have Unicode support _at all_, so load
  206. the system library and do without if it doesn't exist. */
  207. int ok = 0;
  208. char buf[CCHMAXPATH];
  209. UniChar defstr[] = { 0 };
  210. if (DosLoadModule(buf, sizeof (buf) - 1, "uconv", &uconvdll) == NO_ERROR)
  211. {
  212. #define LOAD(x) (DosQueryProcAddr(uconvdll,0,#x,(PFN*)&p##x)==NO_ERROR)
  213. ok = LOAD(UniCreateUconvObject) &&
  214. LOAD(UniFreeUconvObject) &&
  215. LOAD(UniUconvToUcs) &&
  216. LOAD(UniUconvFromUcs);
  217. #undef LOAD
  218. } /* else */
  219. if (!ok || (pUniCreateUconvObject(defstr, &uconv) != ULS_SUCCESS))
  220. {
  221. /* oh well, live without it. */
  222. if (uconvdll)
  223. {
  224. if (uconv)
  225. pUniFreeUconvObject(uconv);
  226. DosFreeModule(uconvdll);
  227. uconvdll = 0;
  228. } /* if */
  229. } /* if */
  230. } /* prepUnicodeSupport */
  231. int __PHYSFS_platformInit(void)
  232. {
  233. prepUnicodeSupport();
  234. return 1; /* ready to go! */
  235. } /* __PHYSFS_platformInit */
  236. void __PHYSFS_platformDeinit(void)
  237. {
  238. if (uconvdll)
  239. {
  240. pUniFreeUconvObject(uconv);
  241. uconv = 0;
  242. DosFreeModule(uconvdll);
  243. uconvdll = 0;
  244. } /* if */
  245. } /* __PHYSFS_platformDeinit */
  246. static int discIsInserted(ULONG drive)
  247. {
  248. int rc;
  249. char buf[20];
  250. DosError(FERR_DISABLEHARDERR | FERR_DISABLEEXCEPTION);
  251. rc = DosQueryFSInfo(drive + 1, FSIL_VOLSER, buf, sizeof (buf));
  252. DosError(FERR_ENABLEHARDERR | FERR_ENABLEEXCEPTION);
  253. return (rc == NO_ERROR);
  254. } /* is_cdrom_inserted */
  255. /* looks like "CD01" in ASCII (littleendian)...used for an ioctl. */
  256. #define CD01 0x31304443
  257. static int isCdRomDrive(ULONG drive)
  258. {
  259. PHYSFS_uint32 param, data;
  260. ULONG ul1, ul2;
  261. APIRET rc;
  262. HFILE hfile = NULLHANDLE;
  263. unsigned char drivename[3] = { 0, ':', '\0' };
  264. drivename[0] = 'A' + drive;
  265. rc = DosOpen(drivename, &hfile, &ul1, 0, 0,
  266. OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_FAIL_IF_NEW,
  267. OPEN_FLAGS_DASD | OPEN_FLAGS_FAIL_ON_ERROR |
  268. OPEN_FLAGS_NOINHERIT | OPEN_SHARE_DENYNONE, NULL);
  269. if (rc != NO_ERROR)
  270. return 0;
  271. data = 0;
  272. param = PHYSFS_swapULE32(CD01);
  273. ul1 = ul2 = sizeof (PHYSFS_uint32);
  274. rc = DosDevIOCtl(hfile, IOCTL_CDROMDISK, CDROMDISK_GETDRIVER,
  275. &param, sizeof (param), &ul1, &data, sizeof (data), &ul2);
  276. DosClose(hfile);
  277. return ((rc == NO_ERROR) && (PHYSFS_swapULE32(data) == CD01));
  278. } /* isCdRomDrive */
  279. void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
  280. {
  281. ULONG dummy = 0;
  282. ULONG drivemap = 0;
  283. ULONG i, bit;
  284. const APIRET rc = DosQueryCurrentDisk(&dummy, &drivemap);
  285. BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc),);
  286. for (i = 0, bit = 1; i < 26; i++, bit <<= 1)
  287. {
  288. if (drivemap & bit) /* this logical drive exists. */
  289. {
  290. if ((isCdRomDrive(i)) && (discIsInserted(i)))
  291. {
  292. char drive[4] = "x:\\";
  293. drive[0] = ('A' + i);
  294. cb(data, drive);
  295. } /* if */
  296. } /* if */
  297. } /* for */
  298. } /* __PHYSFS_platformDetectAvailableCDs */
  299. char *__PHYSFS_platformCalcBaseDir(const char *argv0)
  300. {
  301. char *retval = NULL;
  302. char buf[CCHMAXPATH];
  303. APIRET rc;
  304. PTIB ptib;
  305. PPIB ppib;
  306. PHYSFS_sint32 len;
  307. rc = DosGetInfoBlocks(&ptib, &ppib);
  308. BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), 0);
  309. rc = DosQueryModuleName(ppib->pib_hmte, sizeof (buf), (PCHAR) buf);
  310. BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), 0);
  311. retval = cvtCodepageToUtf8(buf);
  312. BAIL_IF_ERRPASS(!retval, NULL);
  313. /* chop off filename, leave path. */
  314. for (len = strlen(retval) - 1; len >= 0; len--)
  315. {
  316. if (retval[len] == '\\')
  317. {
  318. retval[len + 1] = '\0';
  319. break;
  320. } /* if */
  321. } /* for */
  322. assert(len > 0); /* should have been a "x:\\" on the front on string. */
  323. /* The string is capitalized! Figure out the REAL case... */
  324. return cvtPathToCorrectCase(retval);
  325. } /* __PHYSFS_platformCalcBaseDir */
  326. char *__PHYSFS_platformCalcUserDir(void)
  327. {
  328. return __PHYSFS_platformCalcBaseDir(NULL); /* !!! FIXME-3.0: ? */
  329. } /* __PHYSFS_platformCalcUserDir */
  330. char *__PHYSFS_platformCalcPrefDir(const char *org, const char *app)
  331. {
  332. return __PHYSFS_platformCalcBaseDir(NULL); /* !!! FIXME-3.0: ? */
  333. } /* __PHYSFS_platformCalcPrefDir */
  334. int __PHYSFS_platformEnumerate(const char *dirname,
  335. PHYSFS_EnumerateCallback callback,
  336. const char *origdir, void *callbackdata)
  337. {
  338. size_t utf8len = strlen(dirname);
  339. char *utf8 = (char *) __PHYSFS_smallAlloc(utf8len + 5);
  340. char *cpspec = NULL;
  341. FILEFINDBUF3 fb;
  342. HDIR hdir = HDIR_CREATE;
  343. ULONG count = 1;
  344. APIRET rc;
  345. int retval = 1;
  346. BAIL_IF(!utf8, PHYSFS_ERR_OUT_OF_MEMORY, -1);
  347. strcpy(utf8, dirname);
  348. if (utf8[utf8len - 1] != '\\')
  349. strcpy(utf8 + utf8len, "\\*.*");
  350. else
  351. strcpy(utf8 + utf8len, "*.*");
  352. cpspec = cvtUtf8ToCodepage(utf8);
  353. __PHYSFS_smallFree(utf8);
  354. BAIL_IF_ERRPASS(!cpspec, -1);
  355. rc = DosFindFirst((unsigned char *) cpspec, &hdir,
  356. FILE_DIRECTORY | FILE_ARCHIVED |
  357. FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM,
  358. &fb, sizeof (fb), &count, FIL_STANDARD);
  359. allocator.Free(cpspec);
  360. BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), -1);
  361. while (count == 1)
  362. {
  363. if ((strcmp(fb.achName, ".") != 0) && (strcmp(fb.achName, "..") != 0))
  364. {
  365. utf8 = cvtCodepageToUtf8(fb.achName);
  366. if (!utf8)
  367. retval = -1;
  368. else
  369. {
  370. retval = callback(callbackdata, origdir, utf8);
  371. allocator.Free(utf8);
  372. if (retval == -1)
  373. PHYSFS_setErrorCode(PHYSFS_ERR_APP_CALLBACK);
  374. } /* else */
  375. } /* if */
  376. if (retval != 1)
  377. break;
  378. DosFindNext(hdir, &fb, sizeof (fb), &count);
  379. } /* while */
  380. DosFindClose(hdir);
  381. return retval;
  382. } /* __PHYSFS_platformEnumerate */
  383. char *__PHYSFS_platformCurrentDir(void)
  384. {
  385. char *retval;
  386. char *cpstr;
  387. char *utf8;
  388. ULONG currentDisk;
  389. ULONG dummy;
  390. ULONG pathSize = 0;
  391. APIRET rc;
  392. BYTE byte;
  393. rc = DosQueryCurrentDisk(&currentDisk, &dummy);
  394. BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), NULL);
  395. /* The first call just tells us how much space we need for the string. */
  396. rc = DosQueryCurrentDir(currentDisk, &byte, &pathSize);
  397. pathSize++; /* Add space for null terminator. */
  398. cpstr = (char *) __PHYSFS_smallAlloc(pathSize);
  399. BAIL_IF(cpstr == NULL, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  400. /* Actually get the string this time. */
  401. rc = DosQueryCurrentDir(currentDisk, (PBYTE) cpstr, &pathSize);
  402. if (rc != NO_ERROR)
  403. {
  404. __PHYSFS_smallFree(cpstr);
  405. BAIL(errcodeFromAPIRET(rc), NULL);
  406. } /* if */
  407. utf8 = cvtCodepageToUtf8(cpstr);
  408. __PHYSFS_smallFree(cpstr);
  409. BAIL_IF_ERRPASS(utf8 == NULL, NULL);
  410. /* +4 for "x:\\" drive selector and null terminator. */
  411. retval = (char *) allocator.Malloc(strlen(utf8) + 4);
  412. if (retval == NULL)
  413. {
  414. allocator.Free(utf8);
  415. BAIL(PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  416. } /* if */
  417. retval[0] = ('A' + (currentDisk - 1));
  418. retval[1] = ':';
  419. retval[2] = '\\';
  420. strcpy(retval + 3, utf8);
  421. allocator.Free(utf8);
  422. return retval;
  423. } /* __PHYSFS_platformCurrentDir */
  424. int __PHYSFS_platformMkDir(const char *filename)
  425. {
  426. APIRET rc;
  427. char *cpstr = cvtUtf8ToCodepage(filename);
  428. BAIL_IF_ERRPASS(!cpstr, 0);
  429. rc = DosCreateDir((unsigned char *) cpstr, NULL);
  430. allocator.Free(cpstr);
  431. BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), 0);
  432. return 1;
  433. } /* __PHYSFS_platformMkDir */
  434. static HFILE openFile(const char *filename, const ULONG flags, const ULONG mode)
  435. {
  436. char *cpfname = cvtUtf8ToCodepage(filename);
  437. ULONG action = 0;
  438. HFILE hfile = NULLHANDLE;
  439. APIRET rc;
  440. BAIL_IF_ERRPASS(!cpfname, 0);
  441. rc = DosOpen(cpfname, &hfile, &action, 0, FILE_NORMAL, flags, mode, NULL);
  442. allocator.Free(cpfname);
  443. BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), 0);
  444. return hfile;
  445. } /* openFile */
  446. void *__PHYSFS_platformOpenRead(const char *filename)
  447. {
  448. /*
  449. * File must be opened SHARE_DENYWRITE and ACCESS_READONLY, otherwise
  450. * DosQueryFileInfo() will fail if we try to get a file length, etc.
  451. */
  452. return (void *) openFile(filename,
  453. OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_FAIL_IF_NEW,
  454. OPEN_FLAGS_FAIL_ON_ERROR | OPEN_FLAGS_NO_LOCALITY |
  455. OPEN_FLAGS_NOINHERIT | OPEN_SHARE_DENYWRITE |
  456. OPEN_ACCESS_READONLY);
  457. } /* __PHYSFS_platformOpenRead */
  458. void *__PHYSFS_platformOpenWrite(const char *filename)
  459. {
  460. return (void *) openFile(filename,
  461. OPEN_ACTION_REPLACE_IF_EXISTS |
  462. OPEN_ACTION_CREATE_IF_NEW,
  463. OPEN_FLAGS_FAIL_ON_ERROR | OPEN_FLAGS_NO_LOCALITY |
  464. OPEN_FLAGS_NOINHERIT | OPEN_SHARE_DENYWRITE);
  465. } /* __PHYSFS_platformOpenWrite */
  466. void *__PHYSFS_platformOpenAppend(const char *filename)
  467. {
  468. APIRET rc;
  469. ULONG dummy = 0;
  470. HFILE hfile;
  471. /*
  472. * File must be opened SHARE_DENYWRITE and ACCESS_READWRITE, otherwise
  473. * DosQueryFileInfo() will fail if we try to get a file length, etc.
  474. */
  475. hfile = openFile(filename,
  476. OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW,
  477. OPEN_FLAGS_FAIL_ON_ERROR | OPEN_FLAGS_NO_LOCALITY |
  478. OPEN_FLAGS_NOINHERIT | OPEN_SHARE_DENYWRITE |
  479. OPEN_ACCESS_READWRITE);
  480. BAIL_IF_ERRPASS(!hfile, NULL);
  481. rc = DosSetFilePtr(hfile, 0, FILE_END, &dummy);
  482. if (rc != NO_ERROR)
  483. {
  484. DosClose(hfile);
  485. BAIL(errcodeFromAPIRET(rc), NULL);
  486. } /* if */
  487. return ((void *) hfile);
  488. } /* __PHYSFS_platformOpenAppend */
  489. PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buf, PHYSFS_uint64 len)
  490. {
  491. ULONG br = 0;
  492. APIRET rc;
  493. BAIL_IF(!__PHYSFS_ui64FitsAddressSpace(len),PHYSFS_ERR_INVALID_ARGUMENT,-1);
  494. rc = DosRead((HFILE) opaque, buf, (ULONG) len, &br);
  495. BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), (br > 0) ? ((PHYSFS_sint64) br) : -1);
  496. return (PHYSFS_sint64) br;
  497. } /* __PHYSFS_platformRead */
  498. PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buf,
  499. PHYSFS_uint64 len)
  500. {
  501. ULONG bw = 0;
  502. APIRET rc;
  503. BAIL_IF(!__PHYSFS_ui64FitsAddressSpace(len),PHYSFS_ERR_INVALID_ARGUMENT,-1);
  504. rc = DosWrite((HFILE) opaque, (void *) buf, (ULONG) len, &bw);
  505. BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), (bw > 0) ? ((PHYSFS_sint64) bw) : -1);
  506. return (PHYSFS_sint64) bw;
  507. } /* __PHYSFS_platformWrite */
  508. int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos)
  509. {
  510. ULONG dummy;
  511. HFILE hfile = (HFILE) opaque;
  512. LONG dist = (LONG) pos;
  513. APIRET rc;
  514. /* hooray for 32-bit filesystem limits! :) */
  515. BAIL_IF((PHYSFS_uint64) dist != pos, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  516. rc = DosSetFilePtr(hfile, dist, FILE_BEGIN, &dummy);
  517. BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), 0);
  518. return 1;
  519. } /* __PHYSFS_platformSeek */
  520. PHYSFS_sint64 __PHYSFS_platformTell(void *opaque)
  521. {
  522. ULONG pos;
  523. HFILE hfile = (HFILE) opaque;
  524. const APIRET rc = DosSetFilePtr(hfile, 0, FILE_CURRENT, &pos);
  525. BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), -1);
  526. return ((PHYSFS_sint64) pos);
  527. } /* __PHYSFS_platformTell */
  528. PHYSFS_sint64 __PHYSFS_platformFileLength(void *opaque)
  529. {
  530. FILESTATUS3 fs;
  531. HFILE hfile = (HFILE) opaque;
  532. const APIRET rc = DosQueryFileInfo(hfile, FIL_STANDARD, &fs, sizeof (fs));
  533. BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), -1);
  534. return ((PHYSFS_sint64) fs.cbFile);
  535. } /* __PHYSFS_platformFileLength */
  536. int __PHYSFS_platformFlush(void *opaque)
  537. {
  538. const APIRET rc = DosResetBuffer((HFILE) opaque);
  539. BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), 0);
  540. return 1;
  541. } /* __PHYSFS_platformFlush */
  542. void __PHYSFS_platformClose(void *opaque)
  543. {
  544. DosClose((HFILE) opaque); /* ignore errors. You should have flushed! */
  545. } /* __PHYSFS_platformClose */
  546. int __PHYSFS_platformDelete(const char *path)
  547. {
  548. char *cppath = cvtUtf8ToCodepage(path);
  549. FILESTATUS3 fs;
  550. APIRET rc;
  551. int retval = 0;
  552. BAIL_IF_ERRPASS(!cppath, 0);
  553. rc = DosQueryPathInfo(cppath, FIL_STANDARD, &fs, sizeof (fs));
  554. GOTO_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), done);
  555. rc = (fs.attrFile & FILE_DIRECTORY) ? DosDeleteDir(path) : DosDelete(path);
  556. GOTO_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), done);
  557. retval = 1; /* success */
  558. done:
  559. allocator.Free(cppath);
  560. return retval;
  561. } /* __PHYSFS_platformDelete */
  562. /* Convert to a format PhysicsFS can grok... */
  563. PHYSFS_sint64 os2TimeToUnixTime(const FDATE *date, const FTIME *time)
  564. {
  565. struct tm tm;
  566. tm.tm_sec = ((PHYSFS_uint32) time->twosecs) * 2;
  567. tm.tm_min = time->minutes;
  568. tm.tm_hour = time->hours;
  569. tm.tm_mday = date->day;
  570. tm.tm_mon = date->month;
  571. tm.tm_year = ((PHYSFS_uint32) date->year) + 80;
  572. tm.tm_wday = -1 /*st_localtz.wDayOfWeek*/;
  573. tm.tm_yday = -1;
  574. tm.tm_isdst = -1;
  575. return (PHYSFS_sint64) mktime(&tm);
  576. } /* os2TimeToUnixTime */
  577. int __PHYSFS_platformStat(const char *filename, PHYSFS_Stat *stat)
  578. {
  579. char *cpfname = cvtUtf8ToCodepage(filename);
  580. FILESTATUS3 fs;
  581. int retval = 0;
  582. APIRET rc;
  583. BAIL_IF_ERRPASS(!cpfname, 0);
  584. rc = DosQueryPathInfo(cpfname, FIL_STANDARD, &fs, sizeof (fs));
  585. GOTO_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), done);
  586. if (fs.attrFile & FILE_DIRECTORY)
  587. {
  588. stat->filetype = PHYSFS_FILETYPE_DIRECTORY;
  589. stat->filesize = 0;
  590. } /* if */
  591. else
  592. {
  593. stat->filetype = PHYSFS_FILETYPE_REGULAR;
  594. stat->filesize = fs.cbFile;
  595. } /* else */
  596. stat->modtime = os2TimeToUnixTime(&fs.fdateLastWrite, &fs.ftimeLastWrite);
  597. if (stat->modtime < 0)
  598. stat->modtime = 0;
  599. stat->accesstime = os2TimeToUnixTime(&fs.fdateLastAccess, &fs.ftimeLastAccess);
  600. if (stat->accesstime < 0)
  601. stat->accesstime = 0;
  602. stat->createtime = os2TimeToUnixTime(&fs.fdateCreation, &fs.ftimeCreation);
  603. if (stat->createtime < 0)
  604. stat->createtime = 0;
  605. stat->readonly = ((fs.attrFile & FILE_READONLY) == FILE_READONLY);
  606. return 1; /* success */
  607. done:
  608. allocator.Free(cpfname);
  609. return retval;
  610. } /* __PHYSFS_platformStat */
  611. void *__PHYSFS_platformGetThreadID(void)
  612. {
  613. PTIB ptib;
  614. PPIB ppib;
  615. /*
  616. * Allegedly, this API never fails, but we'll punt and return a
  617. * default value (zero might as well do) if it does.
  618. */
  619. const APIRET rc = DosGetInfoBlocks(&ptib, &ppib);
  620. BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), 0);
  621. return ((void *) ptib->tib_ordinal);
  622. } /* __PHYSFS_platformGetThreadID */
  623. void *__PHYSFS_platformCreateMutex(void)
  624. {
  625. HMTX hmtx = NULLHANDLE;
  626. const APIRET rc = DosCreateMutexSem(NULL, &hmtx, 0, 0);
  627. BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), NULL);
  628. return ((void *) hmtx);
  629. } /* __PHYSFS_platformCreateMutex */
  630. void __PHYSFS_platformDestroyMutex(void *mutex)
  631. {
  632. DosCloseMutexSem((HMTX) mutex);
  633. } /* __PHYSFS_platformDestroyMutex */
  634. int __PHYSFS_platformGrabMutex(void *mutex)
  635. {
  636. /* Do _NOT_ set the physfs error message in here! */
  637. return (DosRequestMutexSem((HMTX) mutex, SEM_INDEFINITE_WAIT) == NO_ERROR);
  638. } /* __PHYSFS_platformGrabMutex */
  639. void __PHYSFS_platformReleaseMutex(void *mutex)
  640. {
  641. DosReleaseMutexSem((HMTX) mutex);
  642. } /* __PHYSFS_platformReleaseMutex */
  643. #endif /* PHYSFS_PLATFORM_OS2 */
  644. /* end of physfs_platform_os2.c ... */