os2.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  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_DOSSEMAPHORES
  12. #define INCL_DOSDATETIME
  13. #define INCL_DOSFILEMGR
  14. #define INCL_DOSMODULEMGR
  15. #define INCL_DOSERRORS
  16. #define INCL_DOSPROCESS
  17. #define INCL_DOSDEVICES
  18. #define INCL_DOSDEVIOCTL
  19. #define INCL_DOSMISC
  20. #include <os2.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <errno.h>
  24. #include <string.h>
  25. #include <time.h>
  26. #include <ctype.h>
  27. #include "physfs_internal.h"
  28. const char *__PHYSFS_platformDirSeparator = "\\";
  29. static const char *get_os2_error_string(APIRET rc)
  30. {
  31. switch (rc)
  32. {
  33. case NO_ERROR: return(NULL); /* not an error. */
  34. case ERROR_INTERRUPT: return(NULL); /* not an error. */
  35. case ERROR_TIMEOUT: return(NULL); /* not an error. */
  36. case ERROR_NOT_ENOUGH_MEMORY: return(ERR_OUT_OF_MEMORY);
  37. case ERROR_FILE_NOT_FOUND: return(ERR_NO_SUCH_FILE);
  38. case ERROR_PATH_NOT_FOUND: return(ERR_NO_SUCH_PATH);
  39. case ERROR_ACCESS_DENIED: return(ERR_ACCESS_DENIED);
  40. case ERROR_NOT_DOS_DISK: return(ERR_NOT_A_DOS_DISK);
  41. case ERROR_SHARING_VIOLATION: return(ERR_SHARING_VIOLATION);
  42. case ERROR_CANNOT_MAKE: return(ERR_CANNOT_MAKE);
  43. case ERROR_DEVICE_IN_USE: return(ERR_DEV_IN_USE);
  44. case ERROR_OPEN_FAILED: return(ERR_OPEN_FAILED);
  45. case ERROR_DISK_FULL: return(ERR_DISK_FULL);
  46. case ERROR_PIPE_BUSY: return(ERR_PIPE_BUSY);
  47. case ERROR_SHARING_BUFFER_EXCEEDED: return(ERR_SHARING_BUF_EXCEEDED);
  48. case ERROR_FILENAME_EXCED_RANGE: return(ERR_BAD_FILENAME);
  49. case ERROR_META_EXPANSION_TOO_LONG: return(ERR_BAD_FILENAME);
  50. case ERROR_TOO_MANY_HANDLES: return(ERR_TOO_MANY_HANDLES);
  51. case ERROR_TOO_MANY_OPEN_FILES: return(ERR_TOO_MANY_HANDLES);
  52. case ERROR_NO_MORE_SEARCH_HANDLES: return(ERR_TOO_MANY_HANDLES);
  53. case ERROR_SEEK_ON_DEVICE: return(ERR_SEEK_ERROR);
  54. case ERROR_NEGATIVE_SEEK: return(ERR_SEEK_OUT_OF_RANGE);
  55. /*!!! FIXME: Where did this go? case ERROR_DEL_CURRENT_DIRECTORY: return(ERR_DEL_CWD);*/
  56. case ERROR_WRITE_PROTECT: return(ERR_WRITE_PROTECT_ERROR);
  57. case ERROR_WRITE_FAULT: return(ERR_WRITE_FAULT);
  58. case ERROR_LOCK_VIOLATION: return(ERR_LOCK_VIOLATION);
  59. case ERROR_GEN_FAILURE: return(ERR_GEN_FAILURE);
  60. case ERROR_UNCERTAIN_MEDIA: return(ERR_UNCERTAIN_MEDIA);
  61. case ERROR_PROTECTION_VIOLATION: return(ERR_PROT_VIOLATION);
  62. case ERROR_BROKEN_PIPE: return(ERR_BROKEN_PIPE);
  63. case ERROR_INVALID_PARAMETER:
  64. case ERROR_INVALID_NAME:
  65. case ERROR_INVALID_DRIVE:
  66. case ERROR_INVALID_HANDLE:
  67. case ERROR_INVALID_FUNCTION:
  68. case ERROR_INVALID_LEVEL:
  69. case ERROR_INVALID_CATEGORY:
  70. case ERROR_DUPLICATE_NAME:
  71. case ERROR_BUFFER_OVERFLOW:
  72. case ERROR_BAD_LENGTH:
  73. case ERROR_BAD_DRIVER_LEVEL:
  74. case ERROR_DIRECT_ACCESS_HANDLE:
  75. case ERROR_NOT_OWNER:
  76. return(ERR_PHYSFS_BAD_OS_CALL);
  77. default: return(ERR_OS2_GENERIC);
  78. } /* switch */
  79. return(NULL);
  80. } /* get_os2_error_string */
  81. static APIRET os2err(APIRET retval)
  82. {
  83. char buf[128];
  84. const char *err = get_os2_error_string(retval);
  85. if (err == ERR_OS2_GENERIC)
  86. {
  87. snprintf(buf, sizeof (buf), ERR_OS2_GENERIC, (int) retval);
  88. err = buf;
  89. } /* if */
  90. if (err != NULL)
  91. __PHYSFS_setError(err);
  92. return(retval);
  93. } /* os2err */
  94. /* (be gentle, this function isn't very robust.) */
  95. static void cvt_path_to_correct_case(char *buf)
  96. {
  97. char *fname = buf + 3; /* point to first element. */
  98. char *ptr = strchr(fname, '\\'); /* find end of first element. */
  99. buf[0] = toupper(buf[0]); /* capitalize drive letter. */
  100. /*
  101. * Go through each path element, and enumerate its parent dir until
  102. * a case-insensitive match is found. If one is (and it SHOULD be)
  103. * then overwrite the original element with the correct case.
  104. * If there's an error, or the path has vanished for some reason, it
  105. * won't hurt to have the original case, so we just keep going.
  106. */
  107. while (fname != NULL)
  108. {
  109. char spec[CCHMAXPATH];
  110. FILEFINDBUF3 fb;
  111. HDIR hdir = HDIR_CREATE;
  112. ULONG count = 1;
  113. APIRET rc;
  114. *(fname - 1) = '\0'; /* isolate parent dir string. */
  115. strcpy(spec, buf); /* copy isolated parent dir... */
  116. strcat(spec, "\\*.*"); /* ...and add wildcard search spec. */
  117. if (ptr != NULL) /* isolate element to find (fname is the start). */
  118. *ptr = '\0';
  119. rc = DosFindFirst(spec, &hdir, FILE_DIRECTORY,
  120. &fb, sizeof (fb), &count, FIL_STANDARD);
  121. if (rc == NO_ERROR)
  122. {
  123. while (count == 1) /* while still entries to enumerate... */
  124. {
  125. if (__PHYSFS_platformStricmp(fb.achName, fname) == 0)
  126. {
  127. strcpy(fname, fb.achName);
  128. break; /* there it is. Overwrite and stop searching. */
  129. } /* if */
  130. DosFindNext(hdir, &fb, sizeof (fb), &count);
  131. } /* while */
  132. DosFindClose(hdir);
  133. } /* if */
  134. *(fname - 1) = '\\'; /* unisolate parent dir. */
  135. fname = ptr; /* point to next element. */
  136. if (ptr != NULL)
  137. {
  138. *ptr = '\\'; /* unisolate element. */
  139. ptr = strchr(++fname, '\\'); /* find next element. */
  140. } /* if */
  141. } /* while */
  142. } /* cvt_file_to_correct_case */
  143. static char *baseDir = NULL;
  144. int __PHYSFS_platformInit(void)
  145. {
  146. char buf[CCHMAXPATH];
  147. APIRET rc;
  148. PTIB ptib;
  149. PPIB ppib;
  150. PHYSFS_sint32 len;
  151. assert(baseDir == NULL);
  152. BAIL_IF_MACRO(os2err(DosGetInfoBlocks(&ptib, &ppib)) != NO_ERROR, NULL, 0);
  153. rc = DosQueryModuleName(ppib->pib_hmte, sizeof (buf), (PCHAR) buf);
  154. BAIL_IF_MACRO(os2err(rc) != NO_ERROR, NULL, 0);
  155. /* chop off filename, leave path. */
  156. for (len = strlen(buf) - 1; len >= 0; len--)
  157. {
  158. if (buf[len] == '\\')
  159. {
  160. buf[len] = '\0';
  161. break;
  162. } /* if */
  163. } /* for */
  164. assert(len > 0); /* should have been a "x:\\" on the front on string. */
  165. /* The string is capitalized! Figure out the REAL case... */
  166. cvt_path_to_correct_case(buf);
  167. baseDir = (char *) allocator.Malloc(len + 1);
  168. BAIL_IF_MACRO(baseDir == NULL, ERR_OUT_OF_MEMORY, 0);
  169. strcpy(baseDir, buf);
  170. return(1); /* success. */
  171. } /* __PHYSFS_platformInit */
  172. int __PHYSFS_platformDeinit(void)
  173. {
  174. assert(baseDir != NULL);
  175. allocator.Free(baseDir);
  176. baseDir = NULL;
  177. return(1); /* success. */
  178. } /* __PHYSFS_platformDeinit */
  179. static int disc_is_inserted(ULONG drive)
  180. {
  181. int rc;
  182. char buf[20];
  183. DosError(FERR_DISABLEHARDERR | FERR_DISABLEEXCEPTION);
  184. rc = DosQueryFSInfo(drive + 1, FSIL_VOLSER, buf, sizeof (buf));
  185. DosError(FERR_ENABLEHARDERR | FERR_ENABLEEXCEPTION);
  186. return(rc == NO_ERROR);
  187. } /* is_cdrom_inserted */
  188. /* looks like "CD01" in ASCII (littleendian)...used for an ioctl. */
  189. #define CD01 0x31304443
  190. static int is_cdrom_drive(ULONG drive)
  191. {
  192. PHYSFS_uint32 param, data;
  193. ULONG ul1, ul2;
  194. APIRET rc;
  195. HFILE hfile = NULLHANDLE;
  196. char drivename[3] = { 'A' + drive, ':', '\0' };
  197. rc = DosOpen(drivename, &hfile, &ul1, 0, 0,
  198. OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_FAIL_IF_NEW,
  199. OPEN_FLAGS_DASD | OPEN_FLAGS_FAIL_ON_ERROR |
  200. OPEN_FLAGS_NOINHERIT | OPEN_SHARE_DENYNONE, NULL);
  201. BAIL_IF_MACRO(rc != NO_ERROR, NULL, 0);
  202. data = 0;
  203. param = PHYSFS_swapULE32(CD01);
  204. ul1 = ul2 = sizeof (PHYSFS_uint32);
  205. rc = DosDevIOCtl(hfile, IOCTL_CDROMDISK, CDROMDISK_GETDRIVER,
  206. &param, sizeof (param), &ul1, &data, sizeof (data), &ul2);
  207. DosClose(hfile);
  208. return((rc == NO_ERROR) && (PHYSFS_swapULE32(data) == CD01));
  209. } /* is_cdrom_drive */
  210. void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
  211. {
  212. ULONG dummy = 0;
  213. ULONG drivemap = 0;
  214. ULONG i, bit;
  215. APIRET rc = DosQueryCurrentDisk(&dummy, &drivemap);
  216. if (os2err(rc) != NO_ERROR)
  217. return;
  218. for (i = 0, bit = 1; i < 26; i++, bit <<= 1)
  219. {
  220. if (drivemap & bit) /* this logical drive exists. */
  221. {
  222. if ((is_cdrom_drive(i)) && (disc_is_inserted(i)))
  223. {
  224. char drive[4] = "x:\\";
  225. drive[0] = ('A' + i);
  226. cb(data, drive);
  227. } /* if */
  228. } /* if */
  229. } /* for */
  230. } /* __PHYSFS_platformDetectAvailableCDs */
  231. char *__PHYSFS_platformCalcBaseDir(const char *argv0)
  232. {
  233. char *retval = (char *) allocator.Malloc(strlen(baseDir) + 1);
  234. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  235. strcpy(retval, baseDir); /* calculated at init time. */
  236. return(retval);
  237. } /* __PHYSFS_platformCalcBaseDir */
  238. char *__PHYSFS_platformGetUserName(void)
  239. {
  240. return(NULL); /* (*shrug*) */
  241. } /* __PHYSFS_platformGetUserName */
  242. char *__PHYSFS_platformGetUserDir(void)
  243. {
  244. return(__PHYSFS_platformCalcBaseDir(NULL));
  245. } /* __PHYSFS_platformGetUserDir */
  246. int __PHYSFS_platformStricmp(const char *x, const char *y)
  247. {
  248. int ux, uy;
  249. do
  250. {
  251. ux = toupper((int) *x);
  252. uy = toupper((int) *y);
  253. if (ux > uy)
  254. return(1);
  255. else if (ux < uy)
  256. return(-1);
  257. x++;
  258. y++;
  259. } while ((ux) && (uy));
  260. return(0);
  261. } /* __PHYSFS_platformStricmp */
  262. int __PHYSFS_platformStrnicmp(const char *x, const char *y, PHYSFS_uint32 len)
  263. {
  264. int ux, uy;
  265. if (!len)
  266. return(0);
  267. do
  268. {
  269. ux = toupper((int) *x);
  270. uy = toupper((int) *y);
  271. if (ux > uy)
  272. return(1);
  273. else if (ux < uy)
  274. return(-1);
  275. x++;
  276. y++;
  277. len--;
  278. } while ((ux) && (uy) && (len));
  279. return(0);
  280. } /* __PHYSFS_platformStrnicmp */
  281. int __PHYSFS_platformExists(const char *fname)
  282. {
  283. FILESTATUS3 fs;
  284. APIRET rc = DosQueryPathInfo(fname, FIL_STANDARD, &fs, sizeof (fs));
  285. return(os2err(rc) == NO_ERROR);
  286. } /* __PHYSFS_platformExists */
  287. int __PHYSFS_platformIsSymLink(const char *fname)
  288. {
  289. return(0); /* no symlinks in OS/2. */
  290. } /* __PHYSFS_platformIsSymlink */
  291. int __PHYSFS_platformIsDirectory(const char *fname)
  292. {
  293. FILESTATUS3 fs;
  294. APIRET rc = DosQueryPathInfo(fname, FIL_STANDARD, &fs, sizeof (fs));
  295. BAIL_IF_MACRO(os2err(rc) != NO_ERROR, NULL, 0)
  296. return((fs.attrFile & FILE_DIRECTORY) != 0);
  297. } /* __PHYSFS_platformIsDirectory */
  298. /* !!! FIXME: can we lose the malloc here? */
  299. char *__PHYSFS_platformCvtToDependent(const char *prepend,
  300. const char *dirName,
  301. const char *append)
  302. {
  303. int len = ((prepend) ? strlen(prepend) : 0) +
  304. ((append) ? strlen(append) : 0) +
  305. strlen(dirName) + 1;
  306. char *retval = allocator.Malloc(len);
  307. char *p;
  308. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  309. if (prepend)
  310. strcpy(retval, prepend);
  311. else
  312. retval[0] = '\0';
  313. strcat(retval, dirName);
  314. if (append)
  315. strcat(retval, append);
  316. for (p = strchr(retval, '/'); p != NULL; p = strchr(p + 1, '/'))
  317. *p = '\\';
  318. return(retval);
  319. } /* __PHYSFS_platformCvtToDependent */
  320. void __PHYSFS_platformEnumerateFiles(const char *dirname,
  321. int omitSymLinks,
  322. PHYSFS_EnumFilesCallback callback,
  323. const char *origdir,
  324. void *callbackdata)
  325. {
  326. char spec[CCHMAXPATH];
  327. FILEFINDBUF3 fb;
  328. HDIR hdir = HDIR_CREATE;
  329. ULONG count = 1;
  330. APIRET rc;
  331. if (strlen(dirname) > sizeof (spec) - 5)
  332. {
  333. __PHYSFS_setError(ERR_BAD_FILENAME);
  334. return;
  335. } /* if */
  336. strcpy(spec, dirname);
  337. strcat(spec, (spec[strlen(spec) - 1] != '\\') ? "\\*.*" : "*.*");
  338. rc = DosFindFirst(spec, &hdir,
  339. FILE_DIRECTORY | FILE_ARCHIVED |
  340. FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM,
  341. &fb, sizeof (fb), &count, FIL_STANDARD);
  342. if (os2err(rc) != NO_ERROR)
  343. return;
  344. while (count == 1)
  345. {
  346. if ((strcmp(fb.achName, ".") != 0) && (strcmp(fb.achName, "..") != 0))
  347. callback(callbackdata, origdir, fb.achName);
  348. DosFindNext(hdir, &fb, sizeof (fb), &count);
  349. } /* while */
  350. DosFindClose(hdir);
  351. } /* __PHYSFS_platformEnumerateFiles */
  352. char *__PHYSFS_platformCurrentDir(void)
  353. {
  354. char *retval;
  355. ULONG currentDisk;
  356. ULONG dummy;
  357. ULONG pathSize = 0;
  358. APIRET rc;
  359. BYTE byte;
  360. rc = DosQueryCurrentDisk(&currentDisk, &dummy);
  361. BAIL_IF_MACRO(os2err(rc) != NO_ERROR, NULL, NULL);
  362. /* The first call just tells us how much space we need for the string. */
  363. rc = DosQueryCurrentDir(currentDisk, &byte, &pathSize);
  364. pathSize++; /* Add space for null terminator. */
  365. retval = (char *) allocator.Malloc(pathSize + 3); /* plus "x:\\" */
  366. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  367. /* Actually get the string this time. */
  368. rc = DosQueryCurrentDir(currentDisk, (PBYTE) (retval + 3), &pathSize);
  369. if (os2err(rc) != NO_ERROR)
  370. {
  371. allocator.Free(retval);
  372. return(NULL);
  373. } /* if */
  374. retval[0] = ('A' + (currentDisk - 1));
  375. retval[1] = ':';
  376. retval[2] = '\\';
  377. return(retval);
  378. } /* __PHYSFS_platformCurrentDir */
  379. char *__PHYSFS_platformRealPath(const char *path)
  380. {
  381. char buf[CCHMAXPATH];
  382. char *retval;
  383. APIRET rc = DosQueryPathInfo(path, FIL_QUERYFULLNAME, buf, sizeof (buf));
  384. BAIL_IF_MACRO(os2err(rc) != NO_ERROR, NULL, NULL);
  385. retval = (char *) allocator.Malloc(strlen(buf) + 1);
  386. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  387. strcpy(retval, buf);
  388. return(retval);
  389. } /* __PHYSFS_platformRealPath */
  390. int __PHYSFS_platformMkDir(const char *path)
  391. {
  392. return(os2err(DosCreateDir(path, NULL)) == NO_ERROR);
  393. } /* __PHYSFS_platformMkDir */
  394. void *__PHYSFS_platformOpenRead(const char *filename)
  395. {
  396. ULONG actionTaken = 0;
  397. HFILE hfile = NULLHANDLE;
  398. /*
  399. * File must be opened SHARE_DENYWRITE and ACCESS_READONLY, otherwise
  400. * DosQueryFileInfo() will fail if we try to get a file length, etc.
  401. */
  402. os2err(DosOpen(filename, &hfile, &actionTaken, 0, FILE_NORMAL,
  403. OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_FAIL_IF_NEW,
  404. OPEN_FLAGS_FAIL_ON_ERROR | OPEN_FLAGS_NO_LOCALITY |
  405. OPEN_FLAGS_NOINHERIT | OPEN_SHARE_DENYWRITE |
  406. OPEN_ACCESS_READONLY, NULL));
  407. return((void *) hfile);
  408. } /* __PHYSFS_platformOpenRead */
  409. void *__PHYSFS_platformOpenWrite(const char *filename)
  410. {
  411. ULONG actionTaken = 0;
  412. HFILE hfile = NULLHANDLE;
  413. /*
  414. * File must be opened SHARE_DENYWRITE and ACCESS_READWRITE, otherwise
  415. * DosQueryFileInfo() will fail if we try to get a file length, etc.
  416. */
  417. os2err(DosOpen(filename, &hfile, &actionTaken, 0, FILE_NORMAL,
  418. OPEN_ACTION_REPLACE_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW,
  419. OPEN_FLAGS_FAIL_ON_ERROR | OPEN_FLAGS_NO_LOCALITY |
  420. OPEN_FLAGS_NOINHERIT | OPEN_SHARE_DENYWRITE |
  421. OPEN_ACCESS_READWRITE, NULL));
  422. return((void *) hfile);
  423. } /* __PHYSFS_platformOpenWrite */
  424. void *__PHYSFS_platformOpenAppend(const char *filename)
  425. {
  426. ULONG dummy = 0;
  427. HFILE hfile = NULLHANDLE;
  428. APIRET rc;
  429. /*
  430. * File must be opened SHARE_DENYWRITE and ACCESS_READWRITE, otherwise
  431. * DosQueryFileInfo() will fail if we try to get a file length, etc.
  432. */
  433. rc = os2err(DosOpen(filename, &hfile, &dummy, 0, FILE_NORMAL,
  434. OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW,
  435. OPEN_FLAGS_FAIL_ON_ERROR | OPEN_FLAGS_NO_LOCALITY |
  436. OPEN_FLAGS_NOINHERIT | OPEN_SHARE_DENYWRITE |
  437. OPEN_ACCESS_READWRITE, NULL));
  438. if (rc == NO_ERROR)
  439. {
  440. if (os2err(DosSetFilePtr(hfile, 0, FILE_END, &dummy)) != NO_ERROR)
  441. {
  442. DosClose(hfile);
  443. hfile = NULLHANDLE;
  444. } /* if */
  445. } /* if */
  446. return((void *) hfile);
  447. } /* __PHYSFS_platformOpenAppend */
  448. PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buffer,
  449. PHYSFS_uint32 size, PHYSFS_uint32 count)
  450. {
  451. HFILE hfile = (HFILE) opaque;
  452. PHYSFS_sint64 retval;
  453. ULONG br;
  454. for (retval = 0; retval < count; retval++)
  455. {
  456. os2err(DosRead(hfile, buffer, size, &br));
  457. if (br < size)
  458. {
  459. DosSetFilePtr(hfile, -br, FILE_CURRENT, &br); /* try to cleanup. */
  460. return(retval);
  461. } /* if */
  462. buffer = (void *) ( ((char *) buffer) + size );
  463. } /* for */
  464. return(retval);
  465. } /* __PHYSFS_platformRead */
  466. PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buffer,
  467. PHYSFS_uint32 size, PHYSFS_uint32 count)
  468. {
  469. HFILE hfile = (HFILE) opaque;
  470. PHYSFS_sint64 retval;
  471. ULONG bw;
  472. for (retval = 0; retval < count; retval++)
  473. {
  474. os2err(DosWrite(hfile, buffer, size, &bw));
  475. if (bw < size)
  476. {
  477. DosSetFilePtr(hfile, -bw, FILE_CURRENT, &bw); /* try to cleanup. */
  478. return(retval);
  479. } /* if */
  480. buffer = (void *) ( ((char *) buffer) + size );
  481. } /* for */
  482. return(retval);
  483. } /* __PHYSFS_platformWrite */
  484. int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos)
  485. {
  486. ULONG dummy;
  487. HFILE hfile = (HFILE) opaque;
  488. LONG dist = (LONG) pos;
  489. /* hooray for 32-bit filesystem limits! :) */
  490. BAIL_IF_MACRO((PHYSFS_uint64) dist != pos, ERR_SEEK_OUT_OF_RANGE, 0);
  491. return(os2err(DosSetFilePtr(hfile, dist, FILE_BEGIN, &dummy)) == NO_ERROR);
  492. } /* __PHYSFS_platformSeek */
  493. PHYSFS_sint64 __PHYSFS_platformTell(void *opaque)
  494. {
  495. ULONG pos;
  496. HFILE hfile = (HFILE) opaque;
  497. APIRET rc = os2err(DosSetFilePtr(hfile, 0, FILE_CURRENT, &pos));
  498. BAIL_IF_MACRO(rc != NO_ERROR, NULL, -1);
  499. return((PHYSFS_sint64) pos);
  500. } /* __PHYSFS_platformTell */
  501. PHYSFS_sint64 __PHYSFS_platformFileLength(void *opaque)
  502. {
  503. FILESTATUS3 fs;
  504. HFILE hfile = (HFILE) opaque;
  505. APIRET rc = DosQueryFileInfo(hfile, FIL_STANDARD, &fs, sizeof (fs));
  506. BAIL_IF_MACRO(os2err(rc) != NO_ERROR, NULL, -1);
  507. return((PHYSFS_sint64) fs.cbFile);
  508. } /* __PHYSFS_platformFileLength */
  509. int __PHYSFS_platformEOF(void *opaque)
  510. {
  511. PHYSFS_sint64 len, pos;
  512. len = __PHYSFS_platformFileLength(opaque);
  513. BAIL_IF_MACRO(len == -1, NULL, 1); /* (*shrug*) */
  514. pos = __PHYSFS_platformTell(opaque);
  515. BAIL_IF_MACRO(pos == -1, NULL, 1); /* (*shrug*) */
  516. return(pos >= len);
  517. } /* __PHYSFS_platformEOF */
  518. int __PHYSFS_platformFlush(void *opaque)
  519. {
  520. return(os2err(DosResetBuffer((HFILE) opaque) == NO_ERROR));
  521. } /* __PHYSFS_platformFlush */
  522. int __PHYSFS_platformClose(void *opaque)
  523. {
  524. return(os2err(DosClose((HFILE) opaque) == NO_ERROR));
  525. } /* __PHYSFS_platformClose */
  526. int __PHYSFS_platformDelete(const char *path)
  527. {
  528. if (__PHYSFS_platformIsDirectory(path))
  529. return(os2err(DosDeleteDir(path)) == NO_ERROR);
  530. return(os2err(DosDelete(path) == NO_ERROR));
  531. } /* __PHYSFS_platformDelete */
  532. PHYSFS_sint64 __PHYSFS_platformGetLastModTime(const char *fname)
  533. {
  534. PHYSFS_sint64 retval;
  535. struct tm tm;
  536. FILESTATUS3 fs;
  537. APIRET rc = DosQueryPathInfo(fname, FIL_STANDARD, &fs, sizeof (fs));
  538. BAIL_IF_MACRO(os2err(rc) != NO_ERROR, NULL, -1);
  539. /* Convert to a format that mktime() can grok... */
  540. tm.tm_sec = ((PHYSFS_uint32) fs.ftimeLastWrite.twosecs) * 2;
  541. tm.tm_min = fs.ftimeLastWrite.minutes;
  542. tm.tm_hour = fs.ftimeLastWrite.hours;
  543. tm.tm_mday = fs.fdateLastWrite.day;
  544. tm.tm_mon = fs.fdateLastWrite.month;
  545. tm.tm_year = ((PHYSFS_uint32) fs.fdateLastWrite.year) + 80;
  546. tm.tm_wday = -1 /*st_localtz.wDayOfWeek*/;
  547. tm.tm_yday = -1;
  548. tm.tm_isdst = -1;
  549. /* Convert to a format PhysicsFS can grok... */
  550. retval = (PHYSFS_sint64) mktime(&tm);
  551. BAIL_IF_MACRO(retval == -1, strerror(errno), -1);
  552. return(retval);
  553. } /* __PHYSFS_platformGetLastModTime */
  554. /* Much like my college days, try to sleep for 10 milliseconds at a time... */
  555. void __PHYSFS_platformTimeslice(void)
  556. {
  557. DosSleep(10);
  558. } /* __PHYSFS_platformTimeslice(void) */
  559. PHYSFS_uint64 __PHYSFS_platformGetThreadID(void)
  560. {
  561. PTIB ptib;
  562. PPIB ppib;
  563. /*
  564. * Allegedly, this API never fails, but we'll punt and return a
  565. * default value (zero might as well do) if it does.
  566. */
  567. BAIL_IF_MACRO(os2err(DosGetInfoBlocks(&ptib, &ppib)) != NO_ERROR, 0, 0);
  568. return((PHYSFS_uint64) ptib->tib_ordinal);
  569. } /* __PHYSFS_platformGetThreadID */
  570. void *__PHYSFS_platformCreateMutex(void)
  571. {
  572. HMTX hmtx = NULLHANDLE;
  573. os2err(DosCreateMutexSem(NULL, &hmtx, 0, 0));
  574. return((void *) hmtx);
  575. } /* __PHYSFS_platformCreateMutex */
  576. void __PHYSFS_platformDestroyMutex(void *mutex)
  577. {
  578. DosCloseMutexSem((HMTX) mutex);
  579. } /* __PHYSFS_platformDestroyMutex */
  580. int __PHYSFS_platformGrabMutex(void *mutex)
  581. {
  582. /* Do _NOT_ call os2err() (which sets the physfs error msg) in here! */
  583. return(DosRequestMutexSem((HMTX) mutex, SEM_INDEFINITE_WAIT) == NO_ERROR);
  584. } /* __PHYSFS_platformGrabMutex */
  585. void __PHYSFS_platformReleaseMutex(void *mutex)
  586. {
  587. DosReleaseMutexSem((HMTX) mutex);
  588. } /* __PHYSFS_platformReleaseMutex */
  589. int __PHYSFS_platformAllocatorInit(void)
  590. {
  591. return(1); /* always succeeds. */
  592. } /* __PHYSFS_platformAllocatorInit */
  593. void __PHYSFS_platformAllocatorDeinit(void)
  594. {
  595. /* no-op */
  596. } /* __PHYSFS_platformAllocatorInit */
  597. void *__PHYSFS_platformAllocatorMalloc(PHYSFS_uint64 s)
  598. {
  599. BAIL_IF_MACRO(__PHYSFS_ui64FitsAddressSpace(s), ERR_OUT_OF_MEMORY, NULL);
  600. #undef malloc
  601. return(malloc((size_t) s));
  602. } /* __PHYSFS_platformMalloc */
  603. void *__PHYSFS_platformAllocatorRealloc(void *ptr, PHYSFS_uint64 s)
  604. {
  605. BAIL_IF_MACRO(__PHYSFS_ui64FitsAddressSpace(s), ERR_OUT_OF_MEMORY, NULL);
  606. #undef realloc
  607. return(realloc(ptr, (size_t) s));
  608. } /* __PHYSFS_platformRealloc */
  609. void __PHYSFS_platformAllocatorFree(void *ptr)
  610. {
  611. #undef free
  612. free(ptr);
  613. } /* __PHYSFS_platformAllocatorFree */
  614. #endif /* PHYSFS_PLATFORM_OS2 */
  615. /* end of os2.c ... */