os2.c 22 KB

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