os2.c 21 KB

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