os2.c 23 KB

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