physfs_platform_os2.c 25 KB

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