os2.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  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_stricmpASCII(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_platformExists(const char *fname)
  247. {
  248. FILESTATUS3 fs;
  249. APIRET rc = DosQueryPathInfo(fname, FIL_STANDARD, &fs, sizeof (fs));
  250. return(os2err(rc) == NO_ERROR);
  251. } /* __PHYSFS_platformExists */
  252. int __PHYSFS_platformIsSymLink(const char *fname)
  253. {
  254. return(0); /* no symlinks in OS/2. */
  255. } /* __PHYSFS_platformIsSymlink */
  256. int __PHYSFS_platformIsDirectory(const char *fname)
  257. {
  258. FILESTATUS3 fs;
  259. APIRET rc = DosQueryPathInfo(fname, FIL_STANDARD, &fs, sizeof (fs));
  260. BAIL_IF_MACRO(os2err(rc) != NO_ERROR, NULL, 0)
  261. return((fs.attrFile & FILE_DIRECTORY) != 0);
  262. } /* __PHYSFS_platformIsDirectory */
  263. /* !!! FIXME: can we lose the malloc here? */
  264. char *__PHYSFS_platformCvtToDependent(const char *prepend,
  265. const char *dirName,
  266. const char *append)
  267. {
  268. int len = ((prepend) ? strlen(prepend) : 0) +
  269. ((append) ? strlen(append) : 0) +
  270. strlen(dirName) + 1;
  271. char *retval = allocator.Malloc(len);
  272. char *p;
  273. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  274. if (prepend)
  275. strcpy(retval, prepend);
  276. else
  277. retval[0] = '\0';
  278. strcat(retval, dirName);
  279. if (append)
  280. strcat(retval, append);
  281. for (p = strchr(retval, '/'); p != NULL; p = strchr(p + 1, '/'))
  282. *p = '\\';
  283. return(retval);
  284. } /* __PHYSFS_platformCvtToDependent */
  285. void __PHYSFS_platformEnumerateFiles(const char *dirname,
  286. int omitSymLinks,
  287. PHYSFS_EnumFilesCallback callback,
  288. const char *origdir,
  289. void *callbackdata)
  290. {
  291. char spec[CCHMAXPATH];
  292. FILEFINDBUF3 fb;
  293. HDIR hdir = HDIR_CREATE;
  294. ULONG count = 1;
  295. APIRET rc;
  296. if (strlen(dirname) > sizeof (spec) - 5)
  297. {
  298. __PHYSFS_setError(ERR_BAD_FILENAME);
  299. return;
  300. } /* if */
  301. strcpy(spec, dirname);
  302. strcat(spec, (spec[strlen(spec) - 1] != '\\') ? "\\*.*" : "*.*");
  303. rc = DosFindFirst(spec, &hdir,
  304. FILE_DIRECTORY | FILE_ARCHIVED |
  305. FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM,
  306. &fb, sizeof (fb), &count, FIL_STANDARD);
  307. if (os2err(rc) != NO_ERROR)
  308. return;
  309. while (count == 1)
  310. {
  311. if ((strcmp(fb.achName, ".") != 0) && (strcmp(fb.achName, "..") != 0))
  312. callback(callbackdata, origdir, fb.achName);
  313. DosFindNext(hdir, &fb, sizeof (fb), &count);
  314. } /* while */
  315. DosFindClose(hdir);
  316. } /* __PHYSFS_platformEnumerateFiles */
  317. char *__PHYSFS_platformCurrentDir(void)
  318. {
  319. char *retval;
  320. ULONG currentDisk;
  321. ULONG dummy;
  322. ULONG pathSize = 0;
  323. APIRET rc;
  324. BYTE byte;
  325. rc = DosQueryCurrentDisk(&currentDisk, &dummy);
  326. BAIL_IF_MACRO(os2err(rc) != NO_ERROR, NULL, NULL);
  327. /* The first call just tells us how much space we need for the string. */
  328. rc = DosQueryCurrentDir(currentDisk, &byte, &pathSize);
  329. pathSize++; /* Add space for null terminator. */
  330. retval = (char *) allocator.Malloc(pathSize + 3); /* plus "x:\\" */
  331. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  332. /* Actually get the string this time. */
  333. rc = DosQueryCurrentDir(currentDisk, (PBYTE) (retval + 3), &pathSize);
  334. if (os2err(rc) != NO_ERROR)
  335. {
  336. allocator.Free(retval);
  337. return(NULL);
  338. } /* if */
  339. retval[0] = ('A' + (currentDisk - 1));
  340. retval[1] = ':';
  341. retval[2] = '\\';
  342. return(retval);
  343. } /* __PHYSFS_platformCurrentDir */
  344. char *__PHYSFS_platformRealPath(const char *path)
  345. {
  346. char buf[CCHMAXPATH];
  347. char *retval;
  348. APIRET rc = DosQueryPathInfo(path, FIL_QUERYFULLNAME, buf, sizeof (buf));
  349. BAIL_IF_MACRO(os2err(rc) != NO_ERROR, NULL, NULL);
  350. retval = (char *) allocator.Malloc(strlen(buf) + 1);
  351. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  352. strcpy(retval, buf);
  353. return(retval);
  354. } /* __PHYSFS_platformRealPath */
  355. int __PHYSFS_platformMkDir(const char *path)
  356. {
  357. return(os2err(DosCreateDir(path, NULL)) == NO_ERROR);
  358. } /* __PHYSFS_platformMkDir */
  359. void *__PHYSFS_platformOpenRead(const char *filename)
  360. {
  361. ULONG actionTaken = 0;
  362. HFILE hfile = NULLHANDLE;
  363. /*
  364. * File must be opened SHARE_DENYWRITE and ACCESS_READONLY, otherwise
  365. * DosQueryFileInfo() will fail if we try to get a file length, etc.
  366. */
  367. os2err(DosOpen(filename, &hfile, &actionTaken, 0, FILE_NORMAL,
  368. OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_FAIL_IF_NEW,
  369. OPEN_FLAGS_FAIL_ON_ERROR | OPEN_FLAGS_NO_LOCALITY |
  370. OPEN_FLAGS_NOINHERIT | OPEN_SHARE_DENYWRITE |
  371. OPEN_ACCESS_READONLY, NULL));
  372. return((void *) hfile);
  373. } /* __PHYSFS_platformOpenRead */
  374. void *__PHYSFS_platformOpenWrite(const char *filename)
  375. {
  376. ULONG actionTaken = 0;
  377. HFILE hfile = NULLHANDLE;
  378. /*
  379. * File must be opened SHARE_DENYWRITE and ACCESS_READWRITE, otherwise
  380. * DosQueryFileInfo() will fail if we try to get a file length, etc.
  381. */
  382. os2err(DosOpen(filename, &hfile, &actionTaken, 0, FILE_NORMAL,
  383. OPEN_ACTION_REPLACE_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW,
  384. OPEN_FLAGS_FAIL_ON_ERROR | OPEN_FLAGS_NO_LOCALITY |
  385. OPEN_FLAGS_NOINHERIT | OPEN_SHARE_DENYWRITE |
  386. OPEN_ACCESS_READWRITE, NULL));
  387. return((void *) hfile);
  388. } /* __PHYSFS_platformOpenWrite */
  389. void *__PHYSFS_platformOpenAppend(const char *filename)
  390. {
  391. ULONG dummy = 0;
  392. HFILE hfile = NULLHANDLE;
  393. APIRET rc;
  394. /*
  395. * File must be opened SHARE_DENYWRITE and ACCESS_READWRITE, otherwise
  396. * DosQueryFileInfo() will fail if we try to get a file length, etc.
  397. */
  398. rc = os2err(DosOpen(filename, &hfile, &dummy, 0, FILE_NORMAL,
  399. OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW,
  400. OPEN_FLAGS_FAIL_ON_ERROR | OPEN_FLAGS_NO_LOCALITY |
  401. OPEN_FLAGS_NOINHERIT | OPEN_SHARE_DENYWRITE |
  402. OPEN_ACCESS_READWRITE, NULL));
  403. if (rc == NO_ERROR)
  404. {
  405. if (os2err(DosSetFilePtr(hfile, 0, FILE_END, &dummy)) != NO_ERROR)
  406. {
  407. DosClose(hfile);
  408. hfile = NULLHANDLE;
  409. } /* if */
  410. } /* if */
  411. return((void *) hfile);
  412. } /* __PHYSFS_platformOpenAppend */
  413. PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buffer,
  414. PHYSFS_uint32 size, PHYSFS_uint32 count)
  415. {
  416. HFILE hfile = (HFILE) opaque;
  417. PHYSFS_sint64 retval;
  418. ULONG br;
  419. for (retval = 0; retval < count; retval++)
  420. {
  421. os2err(DosRead(hfile, buffer, size, &br));
  422. if (br < size)
  423. {
  424. DosSetFilePtr(hfile, -br, FILE_CURRENT, &br); /* try to cleanup. */
  425. return(retval);
  426. } /* if */
  427. buffer = (void *) ( ((char *) buffer) + size );
  428. } /* for */
  429. return(retval);
  430. } /* __PHYSFS_platformRead */
  431. PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buffer,
  432. PHYSFS_uint32 size, PHYSFS_uint32 count)
  433. {
  434. HFILE hfile = (HFILE) opaque;
  435. PHYSFS_sint64 retval;
  436. ULONG bw;
  437. for (retval = 0; retval < count; retval++)
  438. {
  439. os2err(DosWrite(hfile, buffer, size, &bw));
  440. if (bw < size)
  441. {
  442. DosSetFilePtr(hfile, -bw, FILE_CURRENT, &bw); /* try to cleanup. */
  443. return(retval);
  444. } /* if */
  445. buffer = (void *) ( ((char *) buffer) + size );
  446. } /* for */
  447. return(retval);
  448. } /* __PHYSFS_platformWrite */
  449. int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos)
  450. {
  451. ULONG dummy;
  452. HFILE hfile = (HFILE) opaque;
  453. LONG dist = (LONG) pos;
  454. /* hooray for 32-bit filesystem limits! :) */
  455. BAIL_IF_MACRO((PHYSFS_uint64) dist != pos, ERR_SEEK_OUT_OF_RANGE, 0);
  456. return(os2err(DosSetFilePtr(hfile, dist, FILE_BEGIN, &dummy)) == NO_ERROR);
  457. } /* __PHYSFS_platformSeek */
  458. PHYSFS_sint64 __PHYSFS_platformTell(void *opaque)
  459. {
  460. ULONG pos;
  461. HFILE hfile = (HFILE) opaque;
  462. APIRET rc = os2err(DosSetFilePtr(hfile, 0, FILE_CURRENT, &pos));
  463. BAIL_IF_MACRO(rc != NO_ERROR, NULL, -1);
  464. return((PHYSFS_sint64) pos);
  465. } /* __PHYSFS_platformTell */
  466. PHYSFS_sint64 __PHYSFS_platformFileLength(void *opaque)
  467. {
  468. FILESTATUS3 fs;
  469. HFILE hfile = (HFILE) opaque;
  470. APIRET rc = DosQueryFileInfo(hfile, FIL_STANDARD, &fs, sizeof (fs));
  471. BAIL_IF_MACRO(os2err(rc) != NO_ERROR, NULL, -1);
  472. return((PHYSFS_sint64) fs.cbFile);
  473. } /* __PHYSFS_platformFileLength */
  474. int __PHYSFS_platformEOF(void *opaque)
  475. {
  476. PHYSFS_sint64 len, pos;
  477. len = __PHYSFS_platformFileLength(opaque);
  478. BAIL_IF_MACRO(len == -1, NULL, 1); /* (*shrug*) */
  479. pos = __PHYSFS_platformTell(opaque);
  480. BAIL_IF_MACRO(pos == -1, NULL, 1); /* (*shrug*) */
  481. return(pos >= len);
  482. } /* __PHYSFS_platformEOF */
  483. int __PHYSFS_platformFlush(void *opaque)
  484. {
  485. return(os2err(DosResetBuffer((HFILE) opaque)) == NO_ERROR);
  486. } /* __PHYSFS_platformFlush */
  487. int __PHYSFS_platformClose(void *opaque)
  488. {
  489. return(os2err(DosClose((HFILE) opaque)) == NO_ERROR);
  490. } /* __PHYSFS_platformClose */
  491. int __PHYSFS_platformDelete(const char *path)
  492. {
  493. if (__PHYSFS_platformIsDirectory(path))
  494. return(os2err(DosDeleteDir(path)) == NO_ERROR);
  495. return(os2err(DosDelete(path)) == NO_ERROR);
  496. } /* __PHYSFS_platformDelete */
  497. PHYSFS_sint64 __PHYSFS_platformGetLastModTime(const char *fname)
  498. {
  499. PHYSFS_sint64 retval;
  500. struct tm tm;
  501. FILESTATUS3 fs;
  502. APIRET rc = DosQueryPathInfo(fname, FIL_STANDARD, &fs, sizeof (fs));
  503. BAIL_IF_MACRO(os2err(rc) != NO_ERROR, NULL, -1);
  504. /* Convert to a format that mktime() can grok... */
  505. tm.tm_sec = ((PHYSFS_uint32) fs.ftimeLastWrite.twosecs) * 2;
  506. tm.tm_min = fs.ftimeLastWrite.minutes;
  507. tm.tm_hour = fs.ftimeLastWrite.hours;
  508. tm.tm_mday = fs.fdateLastWrite.day;
  509. tm.tm_mon = fs.fdateLastWrite.month;
  510. tm.tm_year = ((PHYSFS_uint32) fs.fdateLastWrite.year) + 80;
  511. tm.tm_wday = -1 /*st_localtz.wDayOfWeek*/;
  512. tm.tm_yday = -1;
  513. tm.tm_isdst = -1;
  514. /* Convert to a format PhysicsFS can grok... */
  515. retval = (PHYSFS_sint64) mktime(&tm);
  516. BAIL_IF_MACRO(retval == -1, strerror(errno), -1);
  517. return(retval);
  518. } /* __PHYSFS_platformGetLastModTime */
  519. void *__PHYSFS_platformGetThreadID(void)
  520. {
  521. PTIB ptib;
  522. PPIB ppib;
  523. /*
  524. * Allegedly, this API never fails, but we'll punt and return a
  525. * default value (zero might as well do) if it does.
  526. */
  527. BAIL_IF_MACRO(os2err(DosGetInfoBlocks(&ptib, &ppib)) != NO_ERROR, 0, 0);
  528. return((void *) ptib->tib_ordinal);
  529. } /* __PHYSFS_platformGetThreadID */
  530. void *__PHYSFS_platformCreateMutex(void)
  531. {
  532. HMTX hmtx = NULLHANDLE;
  533. os2err(DosCreateMutexSem(NULL, &hmtx, 0, 0));
  534. return((void *) hmtx);
  535. } /* __PHYSFS_platformCreateMutex */
  536. void __PHYSFS_platformDestroyMutex(void *mutex)
  537. {
  538. DosCloseMutexSem((HMTX) mutex);
  539. } /* __PHYSFS_platformDestroyMutex */
  540. int __PHYSFS_platformGrabMutex(void *mutex)
  541. {
  542. /* Do _NOT_ call os2err() (which sets the physfs error msg) in here! */
  543. return(DosRequestMutexSem((HMTX) mutex, SEM_INDEFINITE_WAIT) == NO_ERROR);
  544. } /* __PHYSFS_platformGrabMutex */
  545. void __PHYSFS_platformReleaseMutex(void *mutex)
  546. {
  547. DosReleaseMutexSem((HMTX) mutex);
  548. } /* __PHYSFS_platformReleaseMutex */
  549. /* !!! FIXME: Don't use C runtime for allocators? */
  550. int __PHYSFS_platformSetDefaultAllocator(PHYSFS_Allocator *a)
  551. {
  552. return(0); /* just use malloc() and friends. */
  553. } /* __PHYSFS_platformSetDefaultAllocator */
  554. #endif /* PHYSFS_PLATFORM_OS2 */
  555. /* end of os2.c ... */