os2.c 21 KB

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