physfs_platform_os2.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  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 and it's ASCII */
  178. cmp = __PHYSFS_stricmpASCII(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. int __PHYSFS_platformDeinit(void)
  237. {
  238. if (uconvdll)
  239. {
  240. pUniFreeUconvObject(uconv);
  241. uconv = 0;
  242. DosFreeModule(uconvdll);
  243. uconvdll = 0;
  244. } /* if */
  245. return 1; /* success. */
  246. } /* __PHYSFS_platformDeinit */
  247. static int discIsInserted(ULONG drive)
  248. {
  249. int rc;
  250. char buf[20];
  251. DosError(FERR_DISABLEHARDERR | FERR_DISABLEEXCEPTION);
  252. rc = DosQueryFSInfo(drive + 1, FSIL_VOLSER, buf, sizeof (buf));
  253. DosError(FERR_ENABLEHARDERR | FERR_ENABLEEXCEPTION);
  254. return (rc == NO_ERROR);
  255. } /* is_cdrom_inserted */
  256. /* looks like "CD01" in ASCII (littleendian)...used for an ioctl. */
  257. #define CD01 0x31304443
  258. static int isCdRomDrive(ULONG drive)
  259. {
  260. PHYSFS_uint32 param, data;
  261. ULONG ul1, ul2;
  262. APIRET rc;
  263. HFILE hfile = NULLHANDLE;
  264. unsigned char drivename[3] = { 0, ':', '\0' };
  265. drivename[0] = 'A' + drive;
  266. rc = DosOpen(drivename, &hfile, &ul1, 0, 0,
  267. OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_FAIL_IF_NEW,
  268. OPEN_FLAGS_DASD | OPEN_FLAGS_FAIL_ON_ERROR |
  269. OPEN_FLAGS_NOINHERIT | OPEN_SHARE_DENYNONE, NULL);
  270. if (rc != NO_ERROR)
  271. return 0;
  272. data = 0;
  273. param = PHYSFS_swapULE32(CD01);
  274. ul1 = ul2 = sizeof (PHYSFS_uint32);
  275. rc = DosDevIOCtl(hfile, IOCTL_CDROMDISK, CDROMDISK_GETDRIVER,
  276. &param, sizeof (param), &ul1, &data, sizeof (data), &ul2);
  277. DosClose(hfile);
  278. return ((rc == NO_ERROR) && (PHYSFS_swapULE32(data) == CD01));
  279. } /* isCdRomDrive */
  280. void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
  281. {
  282. ULONG dummy = 0;
  283. ULONG drivemap = 0;
  284. ULONG i, bit;
  285. const APIRET rc = DosQueryCurrentDisk(&dummy, &drivemap);
  286. BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc),);
  287. for (i = 0, bit = 1; i < 26; i++, bit <<= 1)
  288. {
  289. if (drivemap & bit) /* this logical drive exists. */
  290. {
  291. if ((isCdRomDrive(i)) && (discIsInserted(i)))
  292. {
  293. char drive[4] = "x:\\";
  294. drive[0] = ('A' + i);
  295. cb(data, drive);
  296. } /* if */
  297. } /* if */
  298. } /* for */
  299. } /* __PHYSFS_platformDetectAvailableCDs */
  300. char *__PHYSFS_platformCalcBaseDir(const char *argv0)
  301. {
  302. char *retval = NULL;
  303. char buf[CCHMAXPATH];
  304. APIRET rc;
  305. PTIB ptib;
  306. PPIB ppib;
  307. PHYSFS_sint32 len;
  308. rc = DosGetInfoBlocks(&ptib, &ppib);
  309. BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), 0);
  310. rc = DosQueryModuleName(ppib->pib_hmte, sizeof (buf), (PCHAR) buf);
  311. BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), 0);
  312. retval = cvtCodepageToUtf8(buf);
  313. BAIL_IF_ERRPASS(!retval, NULL);
  314. /* chop off filename, leave path. */
  315. for (len = strlen(retval) - 1; len >= 0; len--)
  316. {
  317. if (retval[len] == '\\')
  318. {
  319. retval[len + 1] = '\0';
  320. break;
  321. } /* if */
  322. } /* for */
  323. assert(len > 0); /* should have been a "x:\\" on the front on string. */
  324. /* The string is capitalized! Figure out the REAL case... */
  325. return cvtPathToCorrectCase(retval);
  326. } /* __PHYSFS_platformCalcBaseDir */
  327. char *__PHYSFS_platformCalcUserDir(void)
  328. {
  329. return __PHYSFS_platformCalcBaseDir(NULL); /* !!! FIXME: ? */
  330. } /* __PHYSFS_platformCalcUserDir */
  331. char *__PHYSFS_platformCalcPrefDir(const char *org, const char *app)
  332. {
  333. return __PHYSFS_platformCalcBaseDir(NULL); /* !!! FIXME: ? */
  334. }
  335. void __PHYSFS_platformEnumerateFiles(const char *dirname,
  336. PHYSFS_EnumFilesCallback callback,
  337. const char *origdir,
  338. void *callbackdata)
  339. {
  340. size_t utf8len = strlen(dirname);
  341. char *utf8 = (char *) __PHYSFS_smallAlloc(utf8len + 5);
  342. char *cpspec = NULL;
  343. FILEFINDBUF3 fb;
  344. HDIR hdir = HDIR_CREATE;
  345. ULONG count = 1;
  346. APIRET rc;
  347. BAIL_IF(!utf8, PHYSFS_ERR_OUT_OF_MEMORY,);
  348. strcpy(utf8, dirname);
  349. if (utf8[utf8len - 1] != '\\')
  350. strcpy(utf8 + utf8len, "\\*.*");
  351. else
  352. strcpy(utf8 + utf8len, "*.*");
  353. cpspec = cvtUtf8ToCodepage(utf8);
  354. __PHYSFS_smallFree(utf8);
  355. if (!cpspec)
  356. return;
  357. rc = DosFindFirst((unsigned char *) cpspec, &hdir,
  358. FILE_DIRECTORY | FILE_ARCHIVED |
  359. FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM,
  360. &fb, sizeof (fb), &count, FIL_STANDARD);
  361. allocator.Free(cpspec);
  362. BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc),);
  363. while (count == 1)
  364. {
  365. if ((strcmp(fb.achName, ".") != 0) && (strcmp(fb.achName, "..") != 0))
  366. {
  367. utf8 = cvtCodepageToUtf8(fb.achName);
  368. if (utf8)
  369. {
  370. callback(callbackdata, origdir, utf8);
  371. allocator.Free(utf8);
  372. } /* if */
  373. } /* if */
  374. DosFindNext(hdir, &fb, sizeof (fb), &count);
  375. } /* while */
  376. DosFindClose(hdir);
  377. } /* __PHYSFS_platformEnumerateFiles */
  378. char *__PHYSFS_platformCurrentDir(void)
  379. {
  380. char *retval;
  381. char *cpstr;
  382. char *utf8;
  383. ULONG currentDisk;
  384. ULONG dummy;
  385. ULONG pathSize = 0;
  386. APIRET rc;
  387. BYTE byte;
  388. rc = DosQueryCurrentDisk(&currentDisk, &dummy);
  389. BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), NULL);
  390. /* The first call just tells us how much space we need for the string. */
  391. rc = DosQueryCurrentDir(currentDisk, &byte, &pathSize);
  392. pathSize++; /* Add space for null terminator. */
  393. cpstr = (char *) __PHYSFS_smallAlloc(pathSize);
  394. BAIL_IF(cpstr == NULL, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  395. /* Actually get the string this time. */
  396. rc = DosQueryCurrentDir(currentDisk, (PBYTE) cpstr, &pathSize);
  397. if (rc != NO_ERROR)
  398. {
  399. __PHYSFS_smallFree(cpstr);
  400. BAIL(errcodeFromAPIRET(rc), NULL);
  401. } /* if */
  402. utf8 = cvtCodepageToUtf8(cpstr);
  403. __PHYSFS_smallFree(cpstr);
  404. BAIL_IF_ERRPASS(utf8 == NULL, NULL);
  405. /* +4 for "x:\\" drive selector and null terminator. */
  406. retval = (char *) allocator.Malloc(strlen(utf8) + 4);
  407. if (retval == NULL)
  408. {
  409. allocator.Free(utf8);
  410. BAIL(PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  411. } /* if */
  412. retval[0] = ('A' + (currentDisk - 1));
  413. retval[1] = ':';
  414. retval[2] = '\\';
  415. strcpy(retval + 3, utf8);
  416. allocator.Free(utf8);
  417. return retval;
  418. } /* __PHYSFS_platformCurrentDir */
  419. int __PHYSFS_platformMkDir(const char *filename)
  420. {
  421. APIRET rc;
  422. char *cpstr = cvtUtf8ToCodepage(filename);
  423. BAIL_IF_ERRPASS(!cpstr, 0);
  424. rc = DosCreateDir((unsigned char *) cpstr, NULL);
  425. allocator.Free(cpstr);
  426. BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), 0);
  427. return 1;
  428. } /* __PHYSFS_platformMkDir */
  429. static HFILE openFile(const char *filename, const ULONG flags, const ULONG mode)
  430. {
  431. char *cpfname = cvtUtf8ToCodepage(filename);
  432. ULONG action = 0;
  433. HFILE hfile = NULLHANDLE;
  434. APIRET rc;
  435. BAIL_IF_ERRPASS(!cpfname, 0);
  436. rc = DosOpen(cpfname, &hfile, &action, 0, FILE_NORMAL, flags, mode, NULL);
  437. allocator.Free(cpfname);
  438. BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), 0);
  439. return hfile;
  440. } /* openFile */
  441. void *__PHYSFS_platformOpenRead(const char *filename)
  442. {
  443. /*
  444. * File must be opened SHARE_DENYWRITE and ACCESS_READONLY, otherwise
  445. * DosQueryFileInfo() will fail if we try to get a file length, etc.
  446. */
  447. return (void *) openFile(filename,
  448. OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_FAIL_IF_NEW,
  449. OPEN_FLAGS_FAIL_ON_ERROR | OPEN_FLAGS_NO_LOCALITY |
  450. OPEN_FLAGS_NOINHERIT | OPEN_SHARE_DENYWRITE |
  451. OPEN_ACCESS_READONLY);
  452. } /* __PHYSFS_platformOpenRead */
  453. void *__PHYSFS_platformOpenWrite(const char *filename)
  454. {
  455. return (void *) openFile(filename,
  456. OPEN_ACTION_REPLACE_IF_EXISTS |
  457. OPEN_ACTION_CREATE_IF_NEW,
  458. OPEN_FLAGS_FAIL_ON_ERROR | OPEN_FLAGS_NO_LOCALITY |
  459. OPEN_FLAGS_NOINHERIT | OPEN_SHARE_DENYWRITE);
  460. } /* __PHYSFS_platformOpenWrite */
  461. void *__PHYSFS_platformOpenAppend(const char *filename)
  462. {
  463. APIRET rc;
  464. ULONG dummy = 0;
  465. HFILE hfile;
  466. /*
  467. * File must be opened SHARE_DENYWRITE and ACCESS_READWRITE, otherwise
  468. * DosQueryFileInfo() will fail if we try to get a file length, etc.
  469. */
  470. hfile = openFile(filename,
  471. OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW,
  472. OPEN_FLAGS_FAIL_ON_ERROR | OPEN_FLAGS_NO_LOCALITY |
  473. OPEN_FLAGS_NOINHERIT | OPEN_SHARE_DENYWRITE |
  474. OPEN_ACCESS_READWRITE);
  475. BAIL_IF_ERRPASS(!hfile, NULL);
  476. rc = DosSetFilePtr(hfile, 0, FILE_END, &dummy);
  477. if (rc != NO_ERROR)
  478. {
  479. DosClose(hfile);
  480. BAIL(errcodeFromAPIRET(rc), NULL);
  481. } /* if */
  482. return ((void *) hfile);
  483. } /* __PHYSFS_platformOpenAppend */
  484. PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buf, PHYSFS_uint64 len)
  485. {
  486. ULONG br = 0;
  487. APIRET rc;
  488. BAIL_IF(!__PHYSFS_ui64FitsAddressSpace(len),PHYSFS_ERR_INVALID_ARGUMENT,-1);
  489. rc = DosRead((HFILE) opaque, buf, (ULONG) len, &br);
  490. BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), (br > 0) ? ((PHYSFS_sint64) br) : -1);
  491. return (PHYSFS_sint64) br;
  492. } /* __PHYSFS_platformRead */
  493. PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buf,
  494. PHYSFS_uint64 len)
  495. {
  496. ULONG bw = 0;
  497. APIRET rc;
  498. BAIL_IF(!__PHYSFS_ui64FitsAddressSpace(len),PHYSFS_ERR_INVALID_ARGUMENT,-1);
  499. rc = DosWrite((HFILE) opaque, (void *) buf, (ULONG) len, &bw);
  500. BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), (bw > 0) ? ((PHYSFS_sint64) bw) : -1);
  501. return (PHYSFS_sint64) bw;
  502. } /* __PHYSFS_platformWrite */
  503. int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos)
  504. {
  505. ULONG dummy;
  506. HFILE hfile = (HFILE) opaque;
  507. LONG dist = (LONG) pos;
  508. APIRET rc;
  509. /* hooray for 32-bit filesystem limits! :) */
  510. BAIL_IF((PHYSFS_uint64) dist != pos, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  511. rc = DosSetFilePtr(hfile, dist, FILE_BEGIN, &dummy);
  512. BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), 0);
  513. return 1;
  514. } /* __PHYSFS_platformSeek */
  515. PHYSFS_sint64 __PHYSFS_platformTell(void *opaque)
  516. {
  517. ULONG pos;
  518. HFILE hfile = (HFILE) opaque;
  519. const APIRET rc = DosSetFilePtr(hfile, 0, FILE_CURRENT, &pos);
  520. BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), -1);
  521. return ((PHYSFS_sint64) pos);
  522. } /* __PHYSFS_platformTell */
  523. PHYSFS_sint64 __PHYSFS_platformFileLength(void *opaque)
  524. {
  525. FILESTATUS3 fs;
  526. HFILE hfile = (HFILE) opaque;
  527. const APIRET rc = DosQueryFileInfo(hfile, FIL_STANDARD, &fs, sizeof (fs));
  528. BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), -1);
  529. return ((PHYSFS_sint64) fs.cbFile);
  530. } /* __PHYSFS_platformFileLength */
  531. int __PHYSFS_platformFlush(void *opaque)
  532. {
  533. const APIRET rc = DosResetBuffer((HFILE) opaque);
  534. BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), 0);
  535. return 1;
  536. } /* __PHYSFS_platformFlush */
  537. void __PHYSFS_platformClose(void *opaque)
  538. {
  539. DosClose((HFILE) opaque); /* ignore errors. You should have flushed! */
  540. } /* __PHYSFS_platformClose */
  541. int __PHYSFS_platformDelete(const char *path)
  542. {
  543. char *cppath = cvtUtf8ToCodepage(path);
  544. FILESTATUS3 fs;
  545. APIRET rc;
  546. int retval = 0;
  547. BAIL_IF_ERRPASS(!cppath, 0);
  548. rc = DosQueryPathInfo(cppath, FIL_STANDARD, &fs, sizeof (fs));
  549. GOTO_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), done);
  550. rc = (fs.attrFile & FILE_DIRECTORY) ? DosDeleteDir(path) : DosDelete(path);
  551. GOTO_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), done);
  552. retval = 1; /* success */
  553. done:
  554. allocator.Free(cppath);
  555. return retval;
  556. } /* __PHYSFS_platformDelete */
  557. /* Convert to a format PhysicsFS can grok... */
  558. PHYSFS_sint64 os2TimeToUnixTime(const FDATE *date, const FTIME *time)
  559. {
  560. struct tm tm;
  561. tm.tm_sec = ((PHYSFS_uint32) time->twosecs) * 2;
  562. tm.tm_min = time->minutes;
  563. tm.tm_hour = time->hours;
  564. tm.tm_mday = date->day;
  565. tm.tm_mon = date->month;
  566. tm.tm_year = ((PHYSFS_uint32) date->year) + 80;
  567. tm.tm_wday = -1 /*st_localtz.wDayOfWeek*/;
  568. tm.tm_yday = -1;
  569. tm.tm_isdst = -1;
  570. return (PHYSFS_sint64) mktime(&tm);
  571. } /* os2TimeToUnixTime */
  572. int __PHYSFS_platformStat(const char *filename, PHYSFS_Stat *stat)
  573. {
  574. char *cpfname = cvtUtf8ToCodepage(filename);
  575. FILESTATUS3 fs;
  576. int retval = 0;
  577. APIRET rc;
  578. BAIL_IF_ERRPASS(!cpfname, 0);
  579. rc = DosQueryPathInfo(cpfname, FIL_STANDARD, &fs, sizeof (fs));
  580. GOTO_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), done);
  581. if (fs.attrFile & FILE_DIRECTORY)
  582. {
  583. stat->filetype = PHYSFS_FILETYPE_DIRECTORY;
  584. stat->filesize = 0;
  585. } /* if */
  586. else
  587. {
  588. stat->filetype = PHYSFS_FILETYPE_REGULAR;
  589. stat->filesize = fs.cbFile;
  590. } /* else */
  591. stat->modtime = os2TimeToUnixTime(&fs.fdateLastWrite, &fs.ftimeLastWrite);
  592. if (stat->modtime < 0)
  593. stat->modtime = 0;
  594. stat->accesstime = os2TimeToUnixTime(&fs.fdateLastAccess, &fs.ftimeLastAccess);
  595. if (stat->accesstime < 0)
  596. stat->accesstime = 0;
  597. stat->createtime = os2TimeToUnixTime(&fs.fdateCreation, &fs.ftimeCreation);
  598. if (stat->createtime < 0)
  599. stat->createtime = 0;
  600. stat->readonly = ((fs.attrFile & FILE_READONLY) == FILE_READONLY);
  601. return 1; /* success */
  602. done:
  603. allocator.Free(cpfname);
  604. return retval;
  605. } /* __PHYSFS_platformStat */
  606. void *__PHYSFS_platformGetThreadID(void)
  607. {
  608. PTIB ptib;
  609. PPIB ppib;
  610. /*
  611. * Allegedly, this API never fails, but we'll punt and return a
  612. * default value (zero might as well do) if it does.
  613. */
  614. const APIRET rc = DosGetInfoBlocks(&ptib, &ppib);
  615. BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), 0);
  616. return ((void *) ptib->tib_ordinal);
  617. } /* __PHYSFS_platformGetThreadID */
  618. void *__PHYSFS_platformCreateMutex(void)
  619. {
  620. HMTX hmtx = NULLHANDLE;
  621. const APIRET rc = DosCreateMutexSem(NULL, &hmtx, 0, 0);
  622. BAIL_IF(rc != NO_ERROR, errcodeFromAPIRET(rc), NULL);
  623. return ((void *) hmtx);
  624. } /* __PHYSFS_platformCreateMutex */
  625. void __PHYSFS_platformDestroyMutex(void *mutex)
  626. {
  627. DosCloseMutexSem((HMTX) mutex);
  628. } /* __PHYSFS_platformDestroyMutex */
  629. int __PHYSFS_platformGrabMutex(void *mutex)
  630. {
  631. /* Do _NOT_ set the physfs error message in here! */
  632. return (DosRequestMutexSem((HMTX) mutex, SEM_INDEFINITE_WAIT) == NO_ERROR);
  633. } /* __PHYSFS_platformGrabMutex */
  634. void __PHYSFS_platformReleaseMutex(void *mutex)
  635. {
  636. DosReleaseMutexSem((HMTX) mutex);
  637. } /* __PHYSFS_platformReleaseMutex */
  638. #endif /* PHYSFS_PLATFORM_OS2 */
  639. /* end of physfs_platform_os2.c ... */