os2.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  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 a "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. /* looks like "CD01" in ASCII (littleendian)...used for an ioctl. */
  219. #define CD01 0x31304443
  220. static int is_cdrom_drive(ULONG drive)
  221. {
  222. PHYSFS_uint32 param, data;
  223. ULONG ul1, ul2;
  224. APIRET rc;
  225. HFILE hfile = NULLHANDLE;
  226. char drivename[3] = { 'A' + drive, ':', '\0' };
  227. rc = DosOpen(drivename, &hfile, &ul1, 0, 0,
  228. OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_FAIL_IF_NEW,
  229. OPEN_FLAGS_DASD | OPEN_FLAGS_FAIL_ON_ERROR |
  230. OPEN_FLAGS_NOINHERIT | OPEN_SHARE_DENYNONE, NULL);
  231. BAIL_IF_MACRO(rc != NO_ERROR, NULL, 0);
  232. data = 0;
  233. param = PHYSFS_swapULE32(CD01);
  234. ul1 = ul2 = sizeof (PHYSFS_uint32);
  235. rc = DosDevIOCtl(hfile, IOCTL_CDROMDISK, CDROMDISK_GETDRIVER,
  236. &param, sizeof (param), &ul1, &data, sizeof (data), &ul2);
  237. DosClose(hfile);
  238. return((rc == NO_ERROR) && (PHYSFS_swapULE32(data) == CD01));
  239. } /* is_cdrom_drive */
  240. char **__PHYSFS_platformDetectAvailableCDs(void)
  241. {
  242. ULONG dummy;
  243. ULONG drivemap;
  244. ULONG i, bit;
  245. APIRET rc;
  246. char **retval;
  247. PHYSFS_uint32 cd_count = 1; /* we count the NULL entry. */
  248. retval = (char **) malloc(sizeof (char *));
  249. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  250. *retval = NULL;
  251. rc = DosQueryCurrentDisk(&dummy, &drivemap);
  252. BAIL_IF_MACRO(os2err(rc) != NO_ERROR, NULL, retval);
  253. /* !!! FIXME: the a, b, and c drives are almost certainly NOT cdroms... */
  254. for (i = 0, bit = 1; i < 26; i++, bit <<= 1)
  255. {
  256. if (drivemap & bit) /* this logical drive exists. */
  257. {
  258. if ((is_cdrom_drive(i)) && (disc_is_inserted(i)))
  259. {
  260. char **tmp = realloc(retval, sizeof (char *) * (cd_count + 1));
  261. if (tmp)
  262. {
  263. char *str = (char *) malloc(4);
  264. retval = tmp;
  265. retval[cd_count - 1] = str;
  266. if (str)
  267. {
  268. str[0] = ('A' + i);
  269. str[1] = ':';
  270. str[2] = '\\';
  271. str[3] = '\0';
  272. cd_count++;
  273. } /* if */
  274. } /* if */
  275. } /* if */
  276. } /* if */
  277. } /* for */
  278. retval[cd_count - 1] = NULL;
  279. return(retval);
  280. } /* __PHYSFS_platformDetectAvailableCDs */
  281. char *__PHYSFS_platformCalcBaseDir(const char *argv0)
  282. {
  283. char *retval = (char *) malloc(strlen(baseDir) + 1);
  284. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  285. strcpy(retval, baseDir); /* calculated at init time. */
  286. return(retval);
  287. } /* __PHYSFS_platformCalcBaseDir */
  288. char *__PHYSFS_platformGetUserName(void)
  289. {
  290. return(NULL); /* (*shrug*) */
  291. } /* __PHYSFS_platformGetUserName */
  292. char *__PHYSFS_platformGetUserDir(void)
  293. {
  294. return(__PHYSFS_platformCalcBaseDir(NULL));
  295. } /* __PHYSFS_platformGetUserDir */
  296. int __PHYSFS_platformStricmp(const char *x, const char *y)
  297. {
  298. int ux, uy;
  299. do
  300. {
  301. ux = toupper((int) *x);
  302. uy = toupper((int) *y);
  303. if (ux > uy)
  304. return(1);
  305. else if (ux < uy)
  306. return(-1);
  307. x++;
  308. y++;
  309. } while ((ux) && (uy));
  310. return(0);
  311. } /* __PHYSFS_platformStricmp */
  312. int __PHYSFS_platformExists(const char *fname)
  313. {
  314. FILESTATUS3 fs;
  315. APIRET rc = DosQueryPathInfo(fname, FIL_STANDARD, &fs, sizeof (fs));
  316. return(os2err(rc) == NO_ERROR);
  317. } /* __PHYSFS_platformExists */
  318. int __PHYSFS_platformIsSymLink(const char *fname)
  319. {
  320. return(0); /* no symlinks in OS/2. */
  321. } /* __PHYSFS_platformIsSymlink */
  322. int __PHYSFS_platformIsDirectory(const char *fname)
  323. {
  324. FILESTATUS3 fs;
  325. APIRET rc = DosQueryPathInfo(fname, FIL_STANDARD, &fs, sizeof (fs));
  326. BAIL_IF_MACRO(os2err(rc) != NO_ERROR, NULL, 0)
  327. return((fs.attrFile & FILE_DIRECTORY) != 0);
  328. } /* __PHYSFS_platformIsDirectory */
  329. char *__PHYSFS_platformCvtToDependent(const char *prepend,
  330. const char *dirName,
  331. const char *append)
  332. {
  333. int len = ((prepend) ? strlen(prepend) : 0) +
  334. ((append) ? strlen(append) : 0) +
  335. strlen(dirName) + 1;
  336. char *retval = malloc(len);
  337. char *p;
  338. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  339. if (prepend)
  340. strcpy(retval, prepend);
  341. else
  342. retval[0] = '\0';
  343. strcat(retval, dirName);
  344. if (append)
  345. strcat(retval, append);
  346. for (p = strchr(retval, '/'); p != NULL; p = strchr(p + 1, '/'))
  347. *p = '\\';
  348. return(retval);
  349. } /* __PHYSFS_platformCvtToDependent */
  350. LinkedStringList *__PHYSFS_platformEnumerateFiles(const char *dirname,
  351. int omitSymLinks)
  352. {
  353. char spec[CCHMAXPATH];
  354. LinkedStringList *ret = NULL, *p = NULL;
  355. FILEFINDBUF3 fb;
  356. HDIR hdir = HDIR_CREATE;
  357. ULONG count = 1;
  358. APIRET rc;
  359. BAIL_IF_MACRO(strlen(dirname) > sizeof (spec) - 5, ERR_OS_ERROR, NULL);
  360. strcpy(spec, dirname);
  361. strcat(spec, (spec[strlen(spec) - 1] != '\\') ? "\\*.*" : "*.*");
  362. rc = DosFindFirst(spec, &hdir,
  363. FILE_DIRECTORY | FILE_ARCHIVED |
  364. FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM,
  365. &fb, sizeof (fb), &count, FIL_STANDARD);
  366. BAIL_IF_MACRO(os2err(rc) != NO_ERROR, NULL, 0);
  367. while (count == 1)
  368. {
  369. if ((strcmp(fb.achName, ".") != 0) && (strcmp(fb.achName, "..") != 0))
  370. ret = __PHYSFS_addToLinkedStringList(ret, &p, fb.achName, -1);
  371. DosFindNext(hdir, &fb, sizeof (fb), &count);
  372. } /* while */
  373. DosFindClose(hdir);
  374. return(ret);
  375. } /* __PHYSFS_platformEnumerateFiles */
  376. char *__PHYSFS_platformCurrentDir(void)
  377. {
  378. char *retval;
  379. ULONG currentDisk;
  380. ULONG dummy;
  381. ULONG pathSize = 0;
  382. APIRET rc;
  383. BYTE byte;
  384. rc = DosQueryCurrentDisk(&currentDisk, &dummy);
  385. BAIL_IF_MACRO(os2err(rc) != NO_ERROR, NULL, NULL);
  386. /* The first call just tells us how much space we need for the string. */
  387. rc = DosQueryCurrentDir(currentDisk, &byte, &pathSize);
  388. pathSize++; /* Add space for null terminator. */
  389. retval = (char *) malloc(pathSize + 3); /* plus "x:\\" */
  390. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  391. /* Actually get the string this time. */
  392. rc = DosQueryCurrentDir(currentDisk, (PBYTE) (retval + 3), &pathSize);
  393. if (os2err(rc) != NO_ERROR)
  394. {
  395. free(retval);
  396. return(NULL);
  397. } /* if */
  398. retval[0] = ('A' + (currentDisk - 1));
  399. retval[1] = ':';
  400. retval[2] = '\\';
  401. return(retval);
  402. } /* __PHYSFS_platformCurrentDir */
  403. char *__PHYSFS_platformRealPath(const char *path)
  404. {
  405. char buf[CCHMAXPATH];
  406. char *retval;
  407. APIRET rc = DosQueryPathInfo(path, FIL_QUERYFULLNAME, buf, sizeof (buf));
  408. BAIL_IF_MACRO(os2err(rc) != NO_ERROR, NULL, NULL);
  409. retval = (char *) malloc(strlen(buf) + 1);
  410. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  411. strcpy(retval, buf);
  412. return(retval);
  413. } /* __PHYSFS_platformRealPath */
  414. int __PHYSFS_platformMkDir(const char *path)
  415. {
  416. return(os2err(DosCreateDir(path, NULL)) == NO_ERROR);
  417. } /* __PHYSFS_platformMkDir */
  418. void *__PHYSFS_platformOpenRead(const char *filename)
  419. {
  420. ULONG actionTaken = 0;
  421. HFILE hfile = NULLHANDLE;
  422. /*
  423. * File must be opened SHARE_DENYWRITE and ACCESS_READONLY, otherwise
  424. * DosQueryFileInfo() will fail if we try to get a file length, etc.
  425. */
  426. os2err(DosOpen(filename, &hfile, &actionTaken, 0, FILE_NORMAL,
  427. OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_FAIL_IF_NEW,
  428. OPEN_FLAGS_FAIL_ON_ERROR | OPEN_FLAGS_NO_LOCALITY |
  429. OPEN_FLAGS_NOINHERIT | OPEN_SHARE_DENYWRITE |
  430. OPEN_ACCESS_READONLY, NULL));
  431. return((void *) hfile);
  432. } /* __PHYSFS_platformOpenRead */
  433. void *__PHYSFS_platformOpenWrite(const char *filename)
  434. {
  435. ULONG actionTaken = 0;
  436. HFILE hfile = NULLHANDLE;
  437. /*
  438. * File must be opened SHARE_DENYWRITE and ACCESS_READWRITE, otherwise
  439. * DosQueryFileInfo() will fail if we try to get a file length, etc.
  440. */
  441. os2err(DosOpen(filename, &hfile, &actionTaken, 0, FILE_NORMAL,
  442. OPEN_ACTION_REPLACE_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW,
  443. OPEN_FLAGS_FAIL_ON_ERROR | OPEN_FLAGS_NO_LOCALITY |
  444. OPEN_FLAGS_NOINHERIT | OPEN_SHARE_DENYWRITE |
  445. OPEN_ACCESS_READWRITE, NULL));
  446. return((void *) hfile);
  447. } /* __PHYSFS_platformOpenWrite */
  448. void *__PHYSFS_platformOpenAppend(const char *filename)
  449. {
  450. ULONG dummy = 0;
  451. HFILE hfile = NULLHANDLE;
  452. APIRET rc;
  453. /*
  454. * File must be opened SHARE_DENYWRITE and ACCESS_READWRITE, otherwise
  455. * DosQueryFileInfo() will fail if we try to get a file length, etc.
  456. */
  457. rc = os2err(DosOpen(filename, &hfile, &dummy, 0, FILE_NORMAL,
  458. OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW,
  459. OPEN_FLAGS_FAIL_ON_ERROR | OPEN_FLAGS_NO_LOCALITY |
  460. OPEN_FLAGS_NOINHERIT | OPEN_SHARE_DENYWRITE |
  461. OPEN_ACCESS_READWRITE, NULL));
  462. if (rc == NO_ERROR)
  463. {
  464. if (os2err(DosSetFilePtr(hfile, 0, FILE_END, &dummy)) != NO_ERROR)
  465. {
  466. DosClose(hfile);
  467. hfile = NULLHANDLE;
  468. } /* if */
  469. } /* if */
  470. return((void *) hfile);
  471. } /* __PHYSFS_platformOpenAppend */
  472. PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buffer,
  473. PHYSFS_uint32 size, PHYSFS_uint32 count)
  474. {
  475. HFILE hfile = (HFILE) opaque;
  476. PHYSFS_sint64 retval;
  477. ULONG br;
  478. for (retval = 0; retval < count; retval++)
  479. {
  480. os2err(DosRead(hfile, buffer, size, &br));
  481. if (br < size)
  482. {
  483. DosSetFilePtr(hfile, -br, FILE_CURRENT, &br); /* try to cleanup. */
  484. return(retval);
  485. } /* if */
  486. buffer = (void *) ( ((char *) buffer) + size );
  487. } /* for */
  488. return(retval);
  489. } /* __PHYSFS_platformRead */
  490. PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buffer,
  491. PHYSFS_uint32 size, PHYSFS_uint32 count)
  492. {
  493. HFILE hfile = (HFILE) opaque;
  494. PHYSFS_sint64 retval;
  495. ULONG bw;
  496. for (retval = 0; retval < count; retval++)
  497. {
  498. os2err(DosWrite(hfile, buffer, size, &bw));
  499. if (bw < size)
  500. {
  501. DosSetFilePtr(hfile, -bw, FILE_CURRENT, &bw); /* try to cleanup. */
  502. return(retval);
  503. } /* if */
  504. buffer = (void *) ( ((char *) buffer) + size );
  505. } /* for */
  506. return(retval);
  507. } /* __PHYSFS_platformWrite */
  508. int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos)
  509. {
  510. ULONG dummy;
  511. HFILE hfile = (HFILE) opaque;
  512. LONG dist = (LONG) pos;
  513. /* hooray for 32-bit filesystem limits! :) */
  514. BAIL_IF_MACRO((PHYSFS_uint64) dist != pos, ERR_SEEK_OUT_OF_RANGE, 0);
  515. return(os2err(DosSetFilePtr(hfile, dist, FILE_BEGIN, &dummy)) == NO_ERROR);
  516. } /* __PHYSFS_platformSeek */
  517. PHYSFS_sint64 __PHYSFS_platformTell(void *opaque)
  518. {
  519. ULONG pos;
  520. HFILE hfile = (HFILE) opaque;
  521. APIRET rc = os2err(DosSetFilePtr(hfile, 0, FILE_CURRENT, &pos));
  522. BAIL_IF_MACRO(rc != NO_ERROR, NULL, -1);
  523. return((PHYSFS_sint64) pos);
  524. } /* __PHYSFS_platformTell */
  525. PHYSFS_sint64 __PHYSFS_platformFileLength(void *opaque)
  526. {
  527. FILESTATUS3 fs;
  528. HFILE hfile = (HFILE) opaque;
  529. APIRET rc = DosQueryFileInfo(hfile, FIL_STANDARD, &fs, sizeof (fs));
  530. BAIL_IF_MACRO(os2err(rc) != NO_ERROR, NULL, -1);
  531. return((PHYSFS_sint64) fs.cbFile);
  532. } /* __PHYSFS_platformFileLength */
  533. int __PHYSFS_platformEOF(void *opaque)
  534. {
  535. PHYSFS_sint64 len, pos;
  536. len = __PHYSFS_platformFileLength(opaque);
  537. BAIL_IF_MACRO(len == -1, NULL, 1); /* (*shrug*) */
  538. pos = __PHYSFS_platformTell(opaque);
  539. BAIL_IF_MACRO(pos == -1, NULL, 1); /* (*shrug*) */
  540. return(pos >= len);
  541. } /* __PHYSFS_platformEOF */
  542. int __PHYSFS_platformFlush(void *opaque)
  543. {
  544. return(os2err(DosResetBuffer((HFILE) opaque) == NO_ERROR));
  545. } /* __PHYSFS_platformFlush */
  546. int __PHYSFS_platformClose(void *opaque)
  547. {
  548. return(os2err(DosClose((HFILE) opaque) == NO_ERROR));
  549. } /* __PHYSFS_platformClose */
  550. int __PHYSFS_platformDelete(const char *path)
  551. {
  552. if (__PHYSFS_platformIsDirectory(path))
  553. return(os2err(DosDeleteDir(path)) == NO_ERROR);
  554. return(os2err(DosDelete(path) == NO_ERROR));
  555. } /* __PHYSFS_platformDelete */
  556. PHYSFS_sint64 __PHYSFS_platformGetLastModTime(const char *fname)
  557. {
  558. PHYSFS_sint64 retval;
  559. struct tm tm;
  560. FILESTATUS3 fs;
  561. APIRET rc = DosQueryPathInfo(fname, FIL_STANDARD, &fs, sizeof (fs));
  562. BAIL_IF_MACRO(os2err(rc) != NO_ERROR, NULL, -1);
  563. /* Convert to a format that mktime() can grok... */
  564. tm.tm_sec = ((PHYSFS_uint32) fs.ftimeLastWrite.twosecs) * 2;
  565. tm.tm_min = fs.ftimeLastWrite.minutes;
  566. tm.tm_hour = fs.ftimeLastWrite.hours;
  567. tm.tm_mday = fs.fdateLastWrite.day;
  568. tm.tm_mon = fs.fdateLastWrite.month;
  569. tm.tm_year = ((PHYSFS_uint32) fs.fdateLastWrite.year) + 80;
  570. tm.tm_wday = -1 /*st_localtz.wDayOfWeek*/;
  571. tm.tm_yday = -1;
  572. tm.tm_isdst = -1;
  573. /* Convert to a format PhysicsFS can grok... */
  574. retval = (PHYSFS_sint64) mktime(&tm);
  575. BAIL_IF_MACRO(retval == -1, strerror(errno), -1);
  576. return(retval);
  577. } /* __PHYSFS_platformGetLastModTime */
  578. /* Much like my college days, try to sleep for 10 milliseconds at a time... */
  579. void __PHYSFS_platformTimeslice(void)
  580. {
  581. DosSleep(10);
  582. } /* __PHYSFS_platformTimeslice(void) */
  583. PHYSFS_uint64 __PHYSFS_platformGetThreadID(void)
  584. {
  585. PTIB ptib;
  586. PPIB ppib;
  587. /*
  588. * Allegedly, this API never fails, but we'll punt and return a
  589. * default value (zero might as well do) if it does.
  590. */
  591. BAIL_IF_MACRO(os2err(DosGetInfoBlocks(&ptib, &ppib)) != NO_ERROR, 0, 0);
  592. return((PHYSFS_uint64) ptib->tib_ordinal);
  593. } /* __PHYSFS_platformGetThreadID */
  594. void *__PHYSFS_platformCreateMutex(void)
  595. {
  596. HMTX hmtx = NULLHANDLE;
  597. os2err(DosCreateMutexSem(NULL, &hmtx, 0, 0));
  598. return((void *) hmtx);
  599. } /* __PHYSFS_platformCreateMutex */
  600. void __PHYSFS_platformDestroyMutex(void *mutex)
  601. {
  602. DosCloseMutexSem((HMTX) mutex);
  603. } /* __PHYSFS_platformDestroyMutex */
  604. int __PHYSFS_platformGrabMutex(void *mutex)
  605. {
  606. /* Do _NOT_ call os2err() (which sets the physfs error msg) in here! */
  607. return(DosRequestMutexSem((HMTX) mutex, SEM_INDEFINITE_WAIT) == NO_ERROR);
  608. } /* __PHYSFS_platformGrabMutex */
  609. void __PHYSFS_platformReleaseMutex(void *mutex)
  610. {
  611. DosReleaseMutexSem((HMTX) mutex);
  612. } /* __PHYSFS_platformReleaseMutex */
  613. #endif /* defined OS2 */
  614. /* end of os2.c ... */