os2.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  1. /*
  2. * OS/2 support routines for PhysicsFS.
  3. *
  4. * Please see the file LICENSE in the source's root directory.
  5. *
  6. * This file written by Ryan C. Gordon.
  7. */
  8. #if HAVE_CONFIG_H
  9. # include <config.h>
  10. #endif
  11. #if (defined OS2)
  12. #define INCL_DOSSEMAPHORES
  13. #define INCL_DOSDATETIME
  14. #define INCL_DOSFILEMGR
  15. #define INCL_DOSMODULEMGR
  16. #define INCL_DOSERRORS
  17. #define INCL_DOSDEVICES
  18. #include <os2.h>
  19. #include <stdlib.h>
  20. #include <errno.h>
  21. #include <string.h>
  22. #include <assert.h>
  23. static APIRET os2err(APIRET retval)
  24. {
  25. const char *err = NULL;
  26. /*
  27. * The ones that say "OS/2 reported" are more for PhysicsFS developer
  28. * debugging. We give more generic messages for ones that are likely to
  29. * fall through to an application.
  30. */
  31. switch (retval)
  32. {
  33. case NO_ERROR: /* Don't set the PhysicsFS error message for these... */
  34. case ERROR_INTERRUPT:
  35. case ERROR_TIMEOUT:
  36. break;
  37. case ERROR_NOT_ENOUGH_MEMORY:
  38. err = ERR_OUT_OF_MEMORY;
  39. break;
  40. case ERROR_FILE_NOT_FOUND:
  41. err = "File not found";
  42. break;
  43. case ERROR_PATH_NOT_FOUND:
  44. err = "Path not found";
  45. break;
  46. case ERROR_ACCESS_DENIED:
  47. err = "Access denied";
  48. break;
  49. case ERROR_NOT_DOS_DISK:
  50. err = "Not a DOS disk";
  51. break;
  52. case ERROR_DRIVE_LOCKED:
  53. err = "Drive is locked";
  54. break;
  55. case ERROR_SHARING_VIOLATION:
  56. err = "Sharing violation";
  57. break;
  58. case ERROR_CANNOT_MAKE:
  59. err = "Cannot make";
  60. break;
  61. case ERROR_DEVICE_IN_USE:
  62. err = "Device already in use";
  63. break;
  64. case ERROR_DRIVE_LOCKED:
  65. err = "Drive is locked";
  66. break;
  67. case ERROR_OPEN_FAILED:
  68. err = "Open failed";
  69. break;
  70. case ERROR_DISK_FULL:
  71. err = "Disk is full";
  72. break;
  73. case ERROR_DISK_FULL:
  74. err = "Pipe busy";
  75. break;
  76. case ERROR_SHARING_BUFFER_EXCEEDED:
  77. err = "Sharing buffer exceeded";
  78. break;
  79. case ERROR_FILENAME_EXCED_RANGE:
  80. case ERROR_META_EXPANSION_TOO_LONG:
  81. err = "Filename too big";
  82. break;
  83. case ERROR_TOO_MANY_HANDLES:
  84. case ERROR_TOO_OPEN_FILES:
  85. case ERROR_NO_MORE_SEARCH_HANDLES:
  86. err = "Too many open handles";
  87. break;
  88. case ERROR_SEEK_ON_DEVICE:
  89. err = "Seek error"; /* Is that what this error means? */
  90. break;
  91. case ERROR_NEGATIVE_SEEK:
  92. err = "Seek past start of file";
  93. break;
  94. case ERROR_CURRENT_DIRECTORY:
  95. err = "Trying to delete current working directory";
  96. break;
  97. case ERROR_WRITE_PROTECT:
  98. err = "Write protect error";
  99. break;
  100. case ERROR_WRITE_FAULT:
  101. err = "Write fault";
  102. break;
  103. case ERROR_LOCK_VIOLATION:
  104. err = "Lock violation";
  105. break;
  106. case ERROR_GEN_FAILURE:
  107. err = "General failure";
  108. break;
  109. case ERROR_UNCERTAIN_MEDIA:
  110. err = "Uncertain media";
  111. break;
  112. case ERROR_PROTECTION_VIOLATION:
  113. err = "Protection violation";
  114. break;
  115. case ERROR_INVALID_PARAMETER:
  116. case ERROR_INVALID_NAME:
  117. case ERROR_INVALID_DRIVE:
  118. case ERROR_INVALID_HANDLE:
  119. case ERROR_INVALID_FUNCTION:
  120. case ERROR_INVALID_LEVEL:
  121. case ERROR_INVALID_CATEGORY:
  122. case ERROR_DUPLICATE_NAME:
  123. case ERROR_BUFFER_OVERFLOW:
  124. case ERROR_BAD_LENGTH:
  125. case ERROR_BAD_DRIVER_LEVEL:
  126. case ERROR_DIRECT_ACCESS_HANDLE:
  127. err = "OS/2 reported an invalid parameter to an API function";
  128. break;
  129. case ERROR_NO_VOLUME_LABEL:
  130. err = "OS/2 reported no volume label";
  131. break;
  132. case ERROR_NO_MORE_FILES:
  133. err = "OS/2 reported no more files";
  134. break;
  135. case ERROR_MONITORS_NOT_SUPPORTED:
  136. err = "OS/2 reported monitors not supported";
  137. break;
  138. case ERROR_BROKEN_PIPE:
  139. err = "OS/2 reported a broken pipe";
  140. break;
  141. case ERROR_MORE_DATA:
  142. err = "OS/2 reported \"more data\" (?)";
  143. break;
  144. case ERROR_EAS_DIDNT_FIT:
  145. err = "OS/2 reported Extended Attributes didn't fit";
  146. break;
  147. case ERROR_INVALID_EA_NAME:
  148. err = "OS/2 reported an invalid Extended Attribute name";
  149. break;
  150. case ERROR_EA_LIST_INCONSISTENT:
  151. err = "OS/2 reported an inconsistent Extended Attribute list";
  152. break;
  153. case ERROR_EA_VALUE_UNSUPPORTABLE:
  154. err = "OS/2 reported an unsupportable Extended Attribute value";
  155. break;
  156. case ERROR_SEM_OWNER_DIED:
  157. err = "OS/2 reported that semaphore owner died";
  158. break;
  159. case ERROR_TOO_MANY_SEM_REQUESTS:
  160. err = "OS/2 reported too many semaphore requests";
  161. break;
  162. case ERROR_SEM_BUSY:
  163. err = "OS/2 reported a blocked semaphore";
  164. break;
  165. case ERROR_NOT_OWNER:
  166. err = "OS/2 reported that we used a resource we don't own.";
  167. break;
  168. default:
  169. err = "OS/2 reported back with unrecognized error code";
  170. break;
  171. } /* switch */
  172. if (err != NULL)
  173. __PHYSFS_setError(err);
  174. return(retval);
  175. } /* os2err */
  176. static char *baseDir = NULL;
  177. int __PHYSFS_platformInit(void)
  178. {
  179. char buf[CCHMAXPATH];
  180. APIRET rc;
  181. TIB tib;
  182. PIB pib;
  183. assert(baseDir == NULL);
  184. BAIL_IF_MACRO(os2err(DosGetInfoBlocks(&tib, &pib)) != NO_ERROR, NULL, 0);
  185. rc = DosQueryModuleName(pib.pib_hmte, sizeof (buf), (PCHAR) buf);
  186. BAIL_IF_MACRO(os2err(rc) != NO_ERROR, NULL, 0);
  187. baseDir = (char *) malloc(strlen(buf) + 1);
  188. BAIL_IF_MACRO(baseDir == NULL, ERR_OUT_OF_MEMORY, 0);
  189. strcpy(baseDir);
  190. return(1); /* success. */
  191. } /* __PHYSFS_platformInit */
  192. int __PHYSFS_platformDeinit(void)
  193. {
  194. assert(baseDir != NULL);
  195. free(baseDir);
  196. baseDir = NULL;
  197. return(1); /* success. */
  198. } /* __PHYSFS_platformDeinit */
  199. static int disc_is_inserted(ULONG drive)
  200. {
  201. int rc;
  202. char buf[20];
  203. DosError(FERR_DISABLEHARDERR | FERR_DISABLEEXCEPTION);
  204. rc = DosQueryFSInfo(drive + 1, FSIL_VOLSER, buf, sizeof (buf));
  205. DosError(FERR_ENABLEHARDERR | FERR_ENABLEEXCEPTION);
  206. return(rc == NO_ERROR);
  207. } /* is_cdrom_inserted */
  208. static int is_cdrom_drive(ULONG drive)
  209. {
  210. PHYSFS_uint16 cmd = (((drive & 0xFF) << 8) | 0);
  211. BIOSPARAMETERBLOCK bpb;
  212. ULONG ul1, ul2;
  213. APIRET rc;
  214. rc = DosDevIOCtl((HFILE) -1, IOCTL_DISK,
  215. DSK_GETDEVICEPARAMS,
  216. &cmd, sizeof (cmd), &ul1,
  217. &bpb, sizeof (bpb), &u2);
  218. /*
  219. * !!! FIXME: Note that this tells us that the media is REMOVABLE...
  220. * !!! FIXME: but it might not be a CD-ROM...check driver name?
  221. */
  222. return((rc == NO_ERROR) && ((DiskData.fsDeviceAttr & 0x0001) == 0));
  223. } /* is_cdrom_drive */
  224. char **__PHYSFS_platformDetectAvailableCDs(void)
  225. {
  226. ULONG dummy;
  227. ULONG drivemap;
  228. ULONG i, bit;
  229. APIRET rc;
  230. char **retval;
  231. PHYSFS_uint32 cd_count = 0;
  232. retval = (char **) malloc(sizeof (char *));
  233. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  234. *retval = NULL;
  235. rc = DosQueryCurrentDisk(&dummy, &drivemap);
  236. BAIL_IF_MACRO(os2err(rc) != NO_ERROR, NULL, retval);
  237. /* !!! FIXME: the a, b, and c drives are almost certainly NOT cdroms... */
  238. for (i = 0, bit = 1; i < 26; i++, bit << 1)
  239. {
  240. if (drivemap & bit) /* this logical drive exists. */
  241. {
  242. if ((is_cdrom_drive(i)) && (disc_is_inserted(i)))
  243. {
  244. char **tmp = realloc(retval, sizeof (char *) * (cd_count + 1));
  245. if (tmp)
  246. {
  247. char *str = (char *) malloc(4);
  248. retval = tmp;
  249. retval[cd_count - 1] = str;
  250. if (str)
  251. {
  252. str[0] = ('A' + i);
  253. str[1] = ':';
  254. str[2] = '\\';
  255. str[3] = '\0';
  256. cd_count++;
  257. } /* if */
  258. } /* if */
  259. } /* if */
  260. } /* if */
  261. } /* for */
  262. return(retval);
  263. } /* __PHYSFS_platformDetectAvailableCDs */
  264. char *__PHYSFS_platformCalcBaseDir(const char *argv0)
  265. {
  266. char *retval = (char *) malloc(strlen(baseDir) + 1);
  267. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  268. strcpy(retval, baseDir); /* calculated at init time. */
  269. return(retval);
  270. } /* __PHYSFS_platformCalcBaseDir */
  271. char *__PHYSFS_platformGetUserName(void)
  272. {
  273. return(NULL); /* (*shrug*) */
  274. } /* __PHYSFS_platformGetUserName */
  275. char *__PHYSFS_platformGetUserDir(void)
  276. {
  277. return(__PHYSFS_platformCalcBaseDir(NULL));
  278. } /* __PHYSFS_platformGetUserDir */
  279. int __PHYSFS_platformStricmp(const char *x, const char *y)
  280. {
  281. int ux, uy;
  282. do
  283. {
  284. ux = toupper((int) *x);
  285. uy = toupper((int) *y);
  286. if (ux > uy)
  287. return(1);
  288. else if (ux < uy)
  289. return(-1);
  290. x++;
  291. y++;
  292. } while ((ux) && (uy));
  293. return(0);
  294. } /* __PHYSFS_platformStricmp */
  295. int __PHYSFS_platformExists(const char *fname)
  296. {
  297. FILESTATUS3 fs;
  298. APIRET rc = DosQueryPathInfo(fname, FIL_STANDARD, &fs, sizeof (fs));
  299. return(os2err(rc) == NO_ERROR);
  300. } /* __PHYSFS_platformExists */
  301. int __PHYSFS_platformIsSymLink(const char *fname)
  302. {
  303. return(0); /* no symlinks in OS/2. */
  304. } /* __PHYSFS_platformIsSymlink */
  305. int __PHYSFS_platformIsDirectory(const char *fname)
  306. {
  307. FILESTATUS3 fs;
  308. APIRET rc = DosQueryPathInfo(fname, FIL_STANDARD, &fs, sizeof (fs));
  309. BAIL_IF_MACRO(os2err(rc) != NO_ERROR, NULL, 0)
  310. return((fs.attrFile & FILE_DIRECTORY) != 0);
  311. } /* __PHYSFS_platformIsDirectory */
  312. char *__PHYSFS_platformCvtToDependent(const char *prepend,
  313. const char *dirName,
  314. const char *append)
  315. {
  316. int len = ((prepend) ? strlen(prepend) : 0) +
  317. ((append) ? strlen(append) : 0) +
  318. strlen(dirName) + 1;
  319. char *retval = malloc(len);
  320. char *p;
  321. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  322. if (prepend)
  323. strcpy(retval, prepend);
  324. else
  325. retval[0] = '\0';
  326. strcat(retval, dirName);
  327. if (append)
  328. strcat(retval, append);
  329. for (p = strchr(retval, '/'); p != NULL; p = strchr(p + 1, '/'))
  330. *p = '\\';
  331. return(retval);
  332. } /* __PHYSFS_platformCvtToDependent */
  333. LinkedStringList *__PHYSFS_platformEnumerateFiles(const char *dirname,
  334. int omitSymLinks)
  335. {
  336. char spec[CCHMAXPATH];
  337. LinkedStringList *retval = NULL, *p = NULL;
  338. FINDFILEBUF3 fb;
  339. HDIR hdir = HDIR_CREATE;
  340. ULONG count = 1;
  341. APIRET rc;
  342. BAIL_IF_MACRO(strlen(dirname) > sizeof (spec) - 4, ERR_OS_ERROR, NULL);
  343. strcpy(spec, dname)
  344. strcat(spec, "*.*");
  345. rc = DosFindFirst(spec, &hdir,
  346. FILE_DIRECTORY | FILE_ARCHIVED |
  347. FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM,
  348. &fb, sizeof (fb), &count, FIL_STANDARD)
  349. BAIL_IF_MACRO(os2err(rc) != NO_ERROR, NULL, 0);
  350. while (count == 1)
  351. {
  352. if (strcmp(fb.achName, ".") == 0)
  353. continue;
  354. if (strcmp(fb.achName, "..") == 0)
  355. continue;
  356. retval = __PHYSFS_addToLinkedStringList(retval, &p, fb.achName, -1);
  357. DosFindNext(hdir, &fb, sizeof (fb), &count);
  358. } /* while */
  359. DosFindClose(hdir);
  360. return(retval);
  361. } /* __PHYSFS_platformEnumerateFiles */
  362. char *__PHYSFS_platformCurrentDir(void)
  363. {
  364. char *retval;
  365. ULONG currentDisk;
  366. ULONG dummy;
  367. ULONG pathSize = 0;
  368. APIRET rc;
  369. BYTE byte;
  370. rc = DosQueryCurrentDisk(&currentDisk, &dummy);
  371. BAIL_IF_MACRO(os2err(rc) != NO_ERROR, NULL, NULL);
  372. /* The first call just tells us how much space we need for the string. */
  373. rc = DosQueryCurrentDir(currentDisk, &byte, &pathSize);
  374. pathSize++; /* Add space for null terminator. */
  375. retval = (char *) malloc(pathSize + 3); /* plus "x:\\" */
  376. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  377. /* Actually get the string this time. */
  378. rc = DosQueryCurrentDir(currentDisk, (PBYTE) (retval + 3), &pathSize);
  379. if (os2err(rc) != NO_ERROR)
  380. {
  381. free(retval);
  382. return(NULL);
  383. } /* if */
  384. retval[0] = ('A' + (currentDisk - 1));
  385. retval[1] = ':';
  386. retval[2] = '\\';
  387. return(retval);
  388. } /* __PHYSFS_platformCurrentDir */
  389. char *__PHYSFS_platformRealPath(const char *path)
  390. {
  391. char buf[CCHMAXPATH];
  392. char *retval;
  393. APIRET rc = DosQueryPathInfo(fname, FIL_QUERYFULLNAME, buf, sizeof (buf));
  394. BAIL_IF_MACRO(os2err(rc) != NO_ERROR, NULL, NULL);
  395. retval = (char *) malloc(strlen(buf) + 1);
  396. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  397. strcpy(retval, buf);
  398. return(retval);
  399. } /* __PHYSFS_platformRealPath */
  400. int __PHYSFS_platformMkDir(const char *path)
  401. {
  402. return(os2err(DosCreateDir(path, NULL)) == NO_ERROR);
  403. } /* __PHYSFS_platformMkDir */
  404. void *__PHYSFS_platformOpenRead(const char *filename)
  405. {
  406. ULONG actionTaken = 0;
  407. HFILE hfile = NULLHANDLE;
  408. /*
  409. * File must be opened SHARE_DENYWRITE and ACCESS_READONLY, otherwise
  410. * DosQueryFileInfo() will fail if we try to get a file length, etc.
  411. */
  412. os2err(DosOpen(filename, &hfile, &actionTaken, 0, FILE_NORMAL,
  413. OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_FAIL_IF_NEW,
  414. OPEN_FLAGS_FAIL_ON_ERROR | OPEN_FLAGS_NO_LOCALITY |
  415. OPEN_FLAGS_NO_INHERIT | OPEN_SHARE_DENYWRITE |
  416. OPEN_ACCESS_READONLY, NULL));
  417. return((void *) hfile);
  418. } /* __PHYSFS_platformOpenRead */
  419. void *__PHYSFS_platformOpenWrite(const char *filename)
  420. {
  421. ULONG actionTaken = 0;
  422. HFILE hfile = NULLHANDLE;
  423. /*
  424. * File must be opened SHARE_DENYWRITE and ACCESS_READWRITE, otherwise
  425. * DosQueryFileInfo() will fail if we try to get a file length, etc.
  426. */
  427. os2err(DosOpen(filename, &hfile, &actionTaken, 0, FILE_NORMAL,
  428. OPEN_ACTION_REPLACE_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW,
  429. OPEN_FLAGS_FAIL_ON_ERROR | OPEN_FLAGS_NO_LOCALITY |
  430. OPEN_FLAGS_NO_INHERIT | OPEN_SHARE_DENYWRITE |
  431. OPEN_ACCESS_READWRITE, NULL));
  432. return((void *) hfile);
  433. } /* __PHYSFS_platformOpenWrite */
  434. void *__PHYSFS_platformOpenAppend(const char *filename)
  435. {
  436. ULONG dummy = 0;
  437. HFILE hfile = NULLHANDLE;
  438. /*
  439. * File must be opened SHARE_DENYWRITE and ACCESS_READWRITE, otherwise
  440. * DosQueryFileInfo() will fail if we try to get a file length, etc.
  441. */
  442. rc = os2err(DosOpen(filename, &hfile, &dummy, 0, FILE_NORMAL,
  443. OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW,
  444. OPEN_FLAGS_FAIL_ON_ERROR | OPEN_FLAGS_NO_LOCALITY |
  445. OPEN_FLAGS_NO_INHERIT | OPEN_SHARE_DENYWRITE |
  446. OPEN_ACCESS_READWRITE, NULL));
  447. if (rc == NO_ERROR)
  448. {
  449. if (os2err(DosSetFilePtr(hfile, 0, FILE_END, &dummy)) != NO_ERROR)
  450. {
  451. DosClose(hfile);
  452. hfile = NULLHANDLE;
  453. } /* if */
  454. } /* if */
  455. return((void *) hfile);
  456. } /* __PHYSFS_platformOpenAppend */
  457. PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buffer,
  458. PHYSFS_uint32 size, PHYSFS_uint32 count)
  459. {
  460. HFILE hfile = (HFILE) opaque;
  461. PHYSFS_sint64 retval;
  462. ULONG br;
  463. for (retval = 0; retval < count; retval++)
  464. {
  465. APIRET rc = os2err(DosRead(hfile, buffer, size, &br));
  466. if (br < size)
  467. {
  468. DosSetFilePtr(hfile, -bw, FILE_CURRENT, &br); /* try to cleanup. */
  469. return(retval);
  470. } /* if */
  471. buffer = (void *) ( ((char *) buffer) + size) );
  472. } /* for */
  473. return(retval);
  474. } /* __PHYSFS_platformRead */
  475. PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buffer,
  476. PHYSFS_uint32 size, PHYSFS_uint32 count)
  477. {
  478. HFILE hfile = (HFILE) opaque;
  479. PHYSFS_sint64 retval;
  480. ULONG bw;
  481. for (retval = 0; retval < count; retval++)
  482. {
  483. APIRET rc = os2err(DosWrite(hfile, buffer, size, &bw));
  484. if (bw < size)
  485. {
  486. DosSetFilePtr(hfile, -bw, FILE_CURRENT, &bw); /* try to cleanup. */
  487. return(retval);
  488. } /* if */
  489. buffer = (void *) ( ((char *) buffer) + size) );
  490. } /* for */
  491. return(retval);
  492. } /* __PHYSFS_platformWrite */
  493. int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos)
  494. {
  495. ULONG dummy;
  496. HFILE hfile = (HFILE) opaque;
  497. LONG dist = (LONG) pos;
  498. /* hooray for 32-bit filesystem limits! :) */
  499. BAIL_IF_MACRO((PHYSFS_uint64) dist != pos, ERR_SEEK_OUT_OF_RANGE, 0);
  500. return(os2err(DosSetFilePtr(hfile, dist, FILE_BEGIN, &dummy)) == NO_ERROR);
  501. } /* __PHYSFS_platformSeek */
  502. PHYSFS_sint64 __PHYSFS_platformTell(void *opaque)
  503. {
  504. ULONG pos;
  505. HFILE hfile = (HFILE) opaque;
  506. APIRET rc = os2err(DosSetFilePtr(hfile, 0, FILE_CURRENT, &pos));
  507. BAIL_IF_MACRO(rc != NO_ERROR, NULL, -1);
  508. return((PHYSFS_sint64) pos);
  509. } /* __PHYSFS_platformTell */
  510. PHYSFS_sint64 __PHYSFS_platformFileLength(void *opaque)
  511. {
  512. FILESTATUS3 fs;
  513. HFILE hfile = (HFILE) opaque;
  514. APIRET rc = DosQueryFileInfo(hfile, FIL_STANDARD, &fs, sizeof (fs));
  515. BAIL_IF_MACRO(os2err(rc) != NO_ERROR, NULL, -1);
  516. return((PHYSFS_sint64) fs.cbFile);
  517. } /* __PHYSFS_platformFileLength */
  518. int __PHYSFS_platformEOF(void *opaque)
  519. {
  520. PHYSFS_sint64 len, pos;
  521. len = __PHYSFS_platformFileLength(opaque);
  522. BAIL_IF_MACRO(len == -1, NULL, 1); /* (*shrug*) */
  523. pos = __PHYSFS_platformTell(opaque);
  524. BAIL_IF_MACRO(pos == -1, NULL, 1); /* (*shrug*) */
  525. return(pos >= len);
  526. } /* __PHYSFS_platformEOF */
  527. int __PHYSFS_platformFlush(void *opaque)
  528. {
  529. return(os2err(DosResetBuffers((HFILE) opaque) == NO_ERROR));
  530. } /* __PHYSFS_platformFlush */
  531. int __PHYSFS_platformClose(void *opaque)
  532. {
  533. return(os2err(DosClose((HFILE) opaque) == NO_ERROR));
  534. } /* __PHYSFS_platformClose */
  535. int __PHYSFS_platformDelete(const char *path)
  536. {
  537. if (__PHYSFS_platformIsDirectory(path))
  538. return(os2err(DosDeleteDir(path)) == NO_ERROR);
  539. return(os2err(DosDelete(path) == NO_ERROR));
  540. } /* __PHYSFS_platformDelete */
  541. PHYSFS_sint64 __PHYSFS_platformGetLastModTime(const char *fname)
  542. {
  543. struct tm tm;
  544. FILESTATUS3 fs;
  545. APIRET rc = DosQueryPathInfo(fname, FIL_STANDARD, &fs, sizeof (fs));
  546. BAIL_IF_MACRO(os2err(rc) != NO_ERROR, NULL, -1);
  547. /* Convert to a format that mktime() can grok... */
  548. tm.tm_sec = ((PHYSFS_uint32) fs.ftimeLastWrite.twosecs) * 2;
  549. tm.tm_min = fs.ftimeLastWrite.minutes;
  550. tm.tm_hour = fs.ftimeLastWrite.hours;
  551. tm.tm_mday = fs.fdateLastWrite.day;
  552. tm.tm_mon = fs.fdateLastWrite.month;
  553. tm.tm_year = ((PHYSFS_uint32) fs.fdateLastWrite.year) + 80;
  554. tm.tm_wday = -1 /*st_localtz.wDayOfWeek*/;
  555. tm.tm_yday = -1;
  556. tm.tm_isdst = -1;
  557. /* Convert to a format PhysicsFS can grok... */
  558. retval = (PHYSFS_sint64) mktime(&tm);
  559. BAIL_IF_MACRO(retval == -1, strerror(errno), -1);
  560. return(retval);
  561. } /* __PHYSFS_platformGetLastModTime */
  562. /* Much like my college days, try to sleep for 10 milliseconds at a time... */
  563. void __PHYSFS_platformTimeslice(void)
  564. {
  565. DosSleep(10);
  566. } /* __PHYSFS_platformTimeslice(void) */
  567. PHYSFS_uint64 __PHYSFS_platformGetThreadID(void)
  568. {
  569. TIB tib;
  570. PIB pib;
  571. /*
  572. * Allegedly, this API never fails, but we'll punt and return a
  573. * default value (zero might as well do) if it does.
  574. */
  575. BAIL_IF_MACRO(os2err(DosGetInfoBlocks(&tib, &pib)) != NO_ERROR, 0, 0);
  576. return((PHYSFS_uint64) tib.tib_ordinal);
  577. } /* __PHYSFS_platformGetThreadID */
  578. void *__PHYSFS_platformCreateMutex(void)
  579. {
  580. HMTX hmtx = NULLHANDLE;
  581. os2err(DosCreateMutexSem(NULL, &hmtx, 0, 0));
  582. return((void *) hmtx);
  583. } /* __PHYSFS_platformCreateMutex */
  584. void __PHYSFS_platformDestroyMutex(void *mutex)
  585. {
  586. DosCloseMutexSem((HMTX) mutex);
  587. } /* __PHYSFS_platformDestroyMutex */
  588. int __PHYSFS_platformGrabMutex(void *mutex)
  589. {
  590. /* Do _NOT_ call os2err() (which sets the physfs error msg) in here! */
  591. return(DosRequestMutexSem((HMTX) mutex, SEM_INDEFINITE_WAIT) == NO_ERROR);
  592. } /* __PHYSFS_platformGrabMutex */
  593. void __PHYSFS_platformReleaseMutex(void *mutex)
  594. {
  595. DosReleaseMutexSem((HMTX) mutex);
  596. } /* __PHYSFS_platformReleaseMutex */
  597. #endif /* defined OS2 */
  598. /* end of os2.c ... */