physfs_platform_os2.c 24 KB

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