os2.c 22 KB

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