ODPlat.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530
  1. /* OpenDoors Online Software Programming Toolkit
  2. * (C) Copyright 1991 - 1999 by Brian Pirie.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. *
  18. *
  19. * File: ODPlat.c
  20. *
  21. * Description: Contains platform-specific utility functions. Non-platform
  22. * specific utility functions are implemented in odutil.c
  23. *
  24. * Revisions: Date Ver Who Change
  25. * ---------------------------------------------------------------
  26. * Oct 14, 1994 6.00 BP Created, with od_yield().
  27. * Nov 01, 1994 6.00 BP Added new directory access functions.
  28. * Dec 09, 1994 6.00 BP Eliminate access to old dir functions.
  29. * Dec 31, 1994 6.00 BP Added timing, file delete functions.
  30. * Dec 31, 1994 6.00 BP Remove #ifndef USEINLINE DOS code.
  31. * Dec 31, 1994 6.00 BP Added ODMultitasker and ODPlatInit()
  32. * Nov 14, 1995 6.00 BP 32-bit portability.
  33. * Nov 16, 1995 6.00 BP Removed oddoor.h, added odcore.h.
  34. * Nov 17, 1995 6.00 BP Added multithreading functions.
  35. * Nov 21, 1995 6.00 BP Ported to Win32.
  36. * Dec 12, 1995 6.00 BP Added entry, exit and kernel macros.
  37. * Dec 13, 1995 6.00 BP Added ODThreadWaitForExit().
  38. * Dec 13, 1995 6.00 BP Added ODThreadGetCurrent().
  39. * Dec 19, 1995 6.00 BP Fixed ODThreadGetCurrent() (Win32).
  40. * Dec 30, 1995 6.00 BP Added ODCALL for calling convention.
  41. * Jan 23, 1996 6.00 BP Added ODProcessExit().
  42. * Jan 30, 1996 6.00 BP Replaced od_yield() with od_sleep().
  43. * Jan 30, 1996 6.00 BP Add semaphore timeout.
  44. * Jan 31, 1996 6.00 BP Add ODTimerLeft(), rm ODTimerSleep().
  45. * Feb 19, 1996 6.00 BP Changed version number to 6.00.
  46. * Mar 03, 1996 6.10 BP Begin version 6.10.
  47. * Mar 06, 1996 6.10 BP Prevent TC calls N_LXMUL@ & N_LXDIV@.
  48. * Mar 19, 1996 6.10 BP MSVC15 source-level compatibility.
  49. * Aug 10, 2003 6.23 SH *nix support
  50. */
  51. #define BUILDING_OPENDOORS
  52. #include <stdio.h>
  53. #include <stdlib.h>
  54. #include <time.h>
  55. #include <ctype.h>
  56. #include <string.h>
  57. #include "OpenDoor.h"
  58. #ifdef ODPLAT_NIX
  59. #include <sys/time.h>
  60. #include <glob.h>
  61. #include <sys/types.h>
  62. #include <sys/stat.h>
  63. #include <unistd.h>
  64. #endif
  65. #include "ODGen.h"
  66. #include "ODCore.h"
  67. #include "ODPlat.h"
  68. #include "ODUtil.h"
  69. #include "ODSwap.h"
  70. #include "ODKrnl.h"
  71. #ifdef ODPLAT_WIN32
  72. #include "windows.h"
  73. #endif /* ODPLAT_WIN32 */
  74. /* Multitasker type, only availvable under DOS. */
  75. #ifdef ODPLAT_DOS
  76. tODMultitasker ODMultitasker = kMultitaskerNone;
  77. static void ODPlatYield(void);
  78. #endif /* ODPLAT_DOS */
  79. /* ----------------------------------------------------------------------------
  80. * ODPlatInit()
  81. *
  82. * Performs any initialization required to use the utility functions supplied
  83. * by this module.
  84. *
  85. * Parameters: none
  86. *
  87. * Return: void
  88. */
  89. void ODPlatInit(void)
  90. {
  91. #ifdef ODPLAT_DOS
  92. /* If this is the DOS version of OpenDoors, then ODPlatInit() must */
  93. /* determine what multitasker we are running under. */
  94. /* Check whether running under OS/2. */
  95. ASM mov ah, 0x30
  96. ASM int 0x21
  97. ASM cmp al, 0x0a
  98. ASM jl NoOS2
  99. /* If we get to this point, then OS/2 has been detected. */
  100. ODMultitasker = kMultitaskerOS2;
  101. return;
  102. NoOS2:
  103. /* Check whether we are running under DesqView. */
  104. ASM mov cx, 0x4445
  105. ASM mov dx, 0x5351
  106. ASM mov ax, 0x2b01
  107. ASM int 0x21
  108. ASM cmp al, 0xff
  109. ASM je NoDesqView
  110. /* If we get to this point, then DesqView has been detected. */
  111. ODMultitasker = kMultitaskerDV;
  112. NoDesqView:
  113. /* Check whether we are running under Windows. */
  114. ASM push di
  115. ASM push si
  116. ASM mov ax, 0x1600
  117. ASM int 0x2f
  118. ASM pop si
  119. ASM pop di
  120. ASM cmp al, 0x00
  121. ASM je NoWindows
  122. ASM cmp al, 0x80
  123. ASM je NoWindows
  124. /* If we get to this point, then Windows has been detected. */
  125. ODMultitasker = kMultitaskerWin;
  126. NoWindows:
  127. ODMultitasker = kMultitaskerNone;
  128. #endif /* ODPLAT_DOS */
  129. }
  130. /* ----------------------------------------------------------------------------
  131. * ODPlatYield() *** PRIVATE FUNCTION ***
  132. *
  133. * Yields control to other tasks when running as a DOS application under a
  134. * multitasking system.
  135. *
  136. * Parameters: none
  137. *
  138. * Return: void
  139. */
  140. #ifdef ODPLAT_DOS
  141. static void ODPlatYield(void)
  142. {
  143. switch(ODMultitasker)
  144. {
  145. case kMultitaskerDV:
  146. ASM mov ax, 0x1000
  147. ASM int 0x15
  148. break;
  149. case kMultitaskerWin:
  150. ASM mov ax, 0x1680
  151. ASM int 0x2f
  152. break;
  153. case kMultitaskerOS2:
  154. default:
  155. ASM int 0x28
  156. }
  157. }
  158. #endif /* ODPLAT_DOS */
  159. /* ========================================================================= */
  160. /* Multithreading and synchronization support. */
  161. /* ========================================================================= */
  162. #ifdef OD_MULTITHREADED
  163. /* ----------------------------------------------------------------------------
  164. * ODThreadCreate()
  165. *
  166. * Starts a new thread of concurrent execution.
  167. *
  168. * Parameters: phThread - Pointer to the location where the handle to the
  169. * new thread should be stored.
  170. *
  171. * pfThreadProc - Function to call to begin execution of the
  172. * thread.
  173. *
  174. * pThreadParam - Parameter to pass to the thread function when
  175. * it is called.
  176. *
  177. * Return: kOCRCSuccess on success, or an error code on failure.
  178. */
  179. tODResult ODThreadCreate(tODThreadHandle *phThread,
  180. ptODThreadProc *pfThreadProc, void *pThreadParam)
  181. {
  182. #ifdef ODPLAT_WIN32
  183. DWORD dwThreadID;
  184. HANDLE hNewThread;
  185. ASSERT(phThread != NULL);
  186. ASSERT(pfThreadProc != NULL);
  187. /* Attempt to create the new thread. */
  188. hNewThread = CreateThread(NULL, 0, pfThreadProc, pThreadParam,
  189. 0, &dwThreadID);
  190. /* Check for thread creation failure. */
  191. if(hNewThread == NULL)
  192. {
  193. return(kODRCGeneralFailure);
  194. }
  195. /* Pass newly created thread's handle back to the caller. */
  196. *phThread = hNewThread;
  197. /* Return with success. */
  198. return(kODRCSuccess);
  199. #endif /* ODPLAT_WIN32 */
  200. }
  201. /* ----------------------------------------------------------------------------
  202. * ODThreadExit()
  203. *
  204. * Causes the calling thread to be terminated.
  205. *
  206. * Parameters: none
  207. *
  208. * Return: Never returns!
  209. */
  210. void ODThreadExit()
  211. {
  212. #ifdef ODPLAT_WIN32
  213. ExitThread(0);
  214. #endif /* ODPLAT_WIN32 */
  215. /* We should never get here. */
  216. ASSERT(FALSE);
  217. }
  218. /* ----------------------------------------------------------------------------
  219. * ODThreadTerminate()
  220. *
  221. * Terminates the specified thread.
  222. *
  223. * Parameters: hThread - Handle to the thread to be terminated.
  224. *
  225. * Return: kOCRCSuccess on success, or an error code on failure.
  226. */
  227. tODResult ODThreadTerminate(tODThreadHandle hThread)
  228. {
  229. ASSERT(hThread != NULL);
  230. #ifdef ODPLAT_WIN32
  231. return(TerminateThread(hThread, 0) ? kODRCSuccess : kODRCGeneralFailure);
  232. #endif /* ODPLAT_WIN32 */
  233. }
  234. /* ----------------------------------------------------------------------------
  235. * ODThreadSuspend()
  236. *
  237. * Pauses execution of the specified thread, until the ODThreadResume()
  238. * function is called.
  239. *
  240. * Parameters: hThread - Handle to the thread to be suspended.
  241. *
  242. * Return: kOCRCSuccess on success, or an error code on failure.
  243. */
  244. tODResult ODThreadSuspend(tODThreadHandle hThread)
  245. {
  246. ASSERT(hThread != NULL);
  247. #ifdef ODPLAT_WIN32
  248. return(SuspendThread(hThread) == 0xFFFFFFFF ? kODRCGeneralFailure
  249. : kODRCSuccess);
  250. #endif /* ODPLAT_WIN32 */
  251. }
  252. /* ----------------------------------------------------------------------------
  253. * ODThreadResume()
  254. *
  255. * Continues execution of a thread previously paused by a call to
  256. * ODThreadSuspend().
  257. *
  258. * Parameters: hThread - Handle to the thread to be resumed.
  259. *
  260. * Return: kOCRCSuccess on success, or an error code on failure.
  261. */
  262. tODResult ODThreadResume(tODThreadHandle hThread)
  263. {
  264. ASSERT(hThread != NULL);
  265. #ifdef ODPLAT_WIN32
  266. return(ResumeThread(hThread) == 0xFFFFFFFF ? kODRCGeneralFailure
  267. : kODRCSuccess);
  268. #endif /* ODPLAT_WIN32 */
  269. }
  270. /* ----------------------------------------------------------------------------
  271. * ODThreadSetPriority()
  272. *
  273. * Changes the execution priority of a thread. Since the exact semantics of
  274. * thread priorities are different for each platform, this function should
  275. * be used carefully. The caller should assume that no thread will run if there
  276. * exists a non-blocked thread with a higher priority.
  277. *
  278. * Parameters: hThread - Handle to the thread to change the priority of.
  279. *
  280. * ThreadPriority - New priority to assign to the thread.
  281. *
  282. * Return: kOCRCSuccess on success, or an error code on failure.
  283. */
  284. tODResult ODThreadSetPriority(tODThreadHandle hThread,
  285. tODThreadPriority ThreadPriority)
  286. {
  287. #ifdef ODPLAT_WIN32
  288. int nWindowsThreadPriority;
  289. ASSERT(hThread != NULL);
  290. /* Determine the Windows thread priority to assign to the thread. */
  291. switch(ThreadPriority)
  292. {
  293. case OD_PRIORITY_LOWEST:
  294. nWindowsThreadPriority = THREAD_PRIORITY_LOWEST;
  295. break;
  296. case OD_PRIORITY_BELOW_NORMAL:
  297. nWindowsThreadPriority = THREAD_PRIORITY_BELOW_NORMAL;
  298. break;
  299. case OD_PRIORITY_NORMAL:
  300. nWindowsThreadPriority = THREAD_PRIORITY_NORMAL;
  301. break;
  302. case OD_PRIORITY_ABOVE_NORMAL:
  303. nWindowsThreadPriority = THREAD_PRIORITY_ABOVE_NORMAL;
  304. break;
  305. case OD_PRIORITY_HIGHEST:
  306. nWindowsThreadPriority = THREAD_PRIORITY_HIGHEST;
  307. break;
  308. default:
  309. ASSERT(FALSE);
  310. }
  311. /* Update the thread's priority. */
  312. return(SetThreadPriority(hThread, nWindowsThreadPriority)
  313. ? kODRCSuccess : kODRCGeneralFailure);
  314. #endif /* ODPLAT_WIN32 */
  315. }
  316. /* ----------------------------------------------------------------------------
  317. * ODThreadWaitForExit()
  318. *
  319. * Blocks until the specified thread is terminated.
  320. *
  321. * Parameters: hThread - Handle to the thread to wait for.
  322. *
  323. * Return: void
  324. */
  325. void ODThreadWaitForExit(tODThreadHandle hThread)
  326. {
  327. #ifdef ODPLAT_WIN32
  328. WaitForSingleObject(hThread, INFINITE);
  329. #endif /* ODPLAT_WIN32 */
  330. }
  331. /* ----------------------------------------------------------------------------
  332. * ODThreadGetCurrent()
  333. *
  334. * Obtains a handle to the thread that called this function.
  335. *
  336. * Parameters: None.
  337. *
  338. * Return: Handle to the current thread.
  339. */
  340. tODThreadHandle ODThreadGetCurrent(void)
  341. {
  342. #ifdef ODPLAT_WIN32
  343. HANDLE hDuplicate;
  344. if(!DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
  345. GetCurrentProcess(), &hDuplicate, 0, FALSE, DUPLICATE_SAME_ACCESS))
  346. {
  347. return(NULL);
  348. }
  349. return(hDuplicate);
  350. #endif /* ODPLAT_WIN32 */
  351. }
  352. /* ----------------------------------------------------------------------------
  353. * ODSemaphoreAlloc()
  354. *
  355. * Allocates a semaphore synchronization object.
  356. *
  357. * Parameters: phSemaphore - Pointer to location where the handle to the
  358. * newly created semaphore should be stored.
  359. *
  360. * nInitialCount - Initial value to assign to the semaphore.
  361. *
  362. * nMaximumCount - Maximum value that the semaphore may have
  363. * (if supported by the current platform).
  364. *
  365. * Return: kOCRCSuccess on success, or an error code on failure.
  366. */
  367. tODResult ODSemaphoreAlloc(tODSemaphoreHandle *phSemaphore, INT nInitialCount,
  368. INT nMaximumCount)
  369. {
  370. ASSERT(phSemaphore != NULL);
  371. ASSERT(nInitialCount >= 0);
  372. ASSERT(nMaximumCount >= nInitialCount);
  373. #ifdef ODPLAT_WIN32
  374. *phSemaphore = CreateSemaphore(NULL, (LONG)nInitialCount,
  375. (LONG)nMaximumCount, NULL);
  376. return(*phSemaphore == NULL ? kODRCGeneralFailure : kODRCSuccess);
  377. #endif /* ODPLAT_WIN32 */
  378. }
  379. /* ----------------------------------------------------------------------------
  380. * ODSemaphoreFree()
  381. *
  382. * Deallocates a semaphore that we previously created by ODSemaphoreAlloc().
  383. *
  384. * Parameters: hSemaphore - Handle to semaphore to deallocate.
  385. *
  386. * Return: void
  387. */
  388. void ODSemaphoreFree(tODSemaphoreHandle hSemaphore)
  389. {
  390. ASSERT(hSemaphore != NULL);
  391. #ifdef ODPLAT_WIN32
  392. DeleteObject(hSemaphore);
  393. #endif /* ODPLAT_WIN32 */
  394. }
  395. /* ----------------------------------------------------------------------------
  396. * ODSemaphoreUp()
  397. *
  398. * Increments the count of the specified semaphore.
  399. *
  400. * Parameters: hSemaphore - Semaphore to increment.
  401. *
  402. * nIncrementBy - Amount to add to the semaphore's current value.
  403. *
  404. * Return: void
  405. */
  406. void ODSemaphoreUp(tODSemaphoreHandle hSemaphore, INT nIncrementBy)
  407. {
  408. ASSERT(hSemaphore != NULL);
  409. ASSERT(nIncrementBy > 0);
  410. #ifdef ODPLAT_WIN32
  411. ReleaseSemaphore(hSemaphore, nIncrementBy, NULL);
  412. #endif /* ODPLAT_WIN32 */
  413. }
  414. /* ----------------------------------------------------------------------------
  415. * ODSemaphoreDown()
  416. *
  417. * Decrements the count of the specified semaphore. A semaphore may never have
  418. * a value less than 0. Hence, an attempt to decrement the value of a
  419. * semaphore below zero will cause the calling thread to be blocked until some
  420. * other thread increments the semaphore.
  421. *
  422. * Parameters: hSemaphore - Handle to the semaphore to decrement.
  423. *
  424. * Timeout - Maximum time to wait for the semaphore to be
  425. * incremented, or OD_NO_TIMEOUT to prevent this
  426. * function from returning before the semaphore is
  427. * incremented.
  428. *
  429. * Return: kODRCSuccess, or kODRCTimeout if the semaphore was not
  430. * decremented before Timeout milliseconds elapsed.
  431. */
  432. tODResult ODSemaphoreDown(tODSemaphoreHandle hSemaphore, tODMilliSec Timeout)
  433. {
  434. ASSERT(hSemaphore != NULL);
  435. #ifdef ODPLAT_WIN32
  436. if(WaitForSingleObject(hSemaphore, Timeout) != WAIT_OBJECT_0)
  437. {
  438. return(kODRCTimeout);
  439. }
  440. #endif /* ODPLAT_WIN32 */
  441. /* Return with success. */
  442. return(kODRCSuccess);
  443. }
  444. #endif /* OD_MULTITHREADED */
  445. /* ----------------------------------------------------------------------------
  446. * ODProcessExit()
  447. *
  448. * Ends the current process.
  449. *
  450. * Parameters: nExitCode - Exit code to return to the calling process.
  451. *
  452. * Return: Never returns.
  453. */
  454. void ODProcessExit(INT nExitCode)
  455. {
  456. #ifdef ODPLAT_WIN32
  457. ExitProcess(nExitCode);
  458. #else /* !ODPLAT_WIN32 */
  459. exit(nExitCode);
  460. #endif /* !ODPLAT_WIN32 */
  461. }
  462. /* ========================================================================= */
  463. /* Millisecond timer functions. */
  464. /* ========================================================================= */
  465. #ifdef ODPLAT_DOS
  466. /* For the DOS platform, we need to know the number of milliseconds per */
  467. /* clock tick. */
  468. #define MILLISEC_PER_TICK 55 /* (approx. == 1000 / CLOCKS_PER_SEC) */
  469. #endif /* ODPLAT_DOS */
  470. /* ----------------------------------------------------------------------------
  471. * ODTimerStart()
  472. *
  473. * Starts a timer for a specified number of milliseconds. Future calls to
  474. * ODTimerElapsed() can be used to determine whether or not specified time
  475. * has elapsed. Note that while this function accepts its parameter in
  476. * milliseconds, it does not gurantee millisecond resolution. In fact, under
  477. * DOS, this timer mechanism has just under a 55 millisecond resolution, with
  478. * an average error of about 27 milliseconds.
  479. *
  480. * Parameters: pTimer - Pointer to a tODTimer structure that will be later
  481. * passed to ODTimerElapsed().
  482. *
  483. * Duration - Number of milliseconds after which timer should
  484. * elapse.
  485. *
  486. * Return: void
  487. */
  488. void ODTimerStart(tODTimer *pTimer, tODMilliSec Duration)
  489. {
  490. #ifdef ODPLAT_NIX
  491. struct timeval tv;
  492. #endif
  493. ASSERT(pTimer != NULL);
  494. ASSERT(Duration >= 0);
  495. #ifdef ODPLAT_DOS
  496. /* Store timer start time right away. */
  497. pTimer->Start = clock();
  498. /* Calculate duration of timer. */
  499. ODDWordDivide((DWORD *)&pTimer->Duration, NULL, Duration,
  500. MILLISEC_PER_TICK);
  501. #endif /* ODPLAT_DOS */
  502. #ifdef ODPLAT_WIN32
  503. /* Store timer start time now. */
  504. pTimer->Start = GetTickCount();
  505. pTimer->Duration = Duration;
  506. #endif /* ODPLAT_WIN32 */
  507. #ifdef ODPLAT_NIX
  508. gettimeofday(&tv,NULL);
  509. pTimer->Start=tv.tv_sec*1000+tv.tv_usec/1000;
  510. pTimer->Duration = Duration;
  511. #endif
  512. }
  513. /* ----------------------------------------------------------------------------
  514. * ODTimerElapsed()
  515. *
  516. * Determines whether or not a timer set by ODTimerStart() has elapsed.
  517. *
  518. * Parameters: pTimer - Pointer to a tODTimer structure that was populated
  519. * by ODTimerStart().
  520. *
  521. * Return: TRUE if timer has elapsed, FALSE if it has not.
  522. */
  523. BOOL ODTimerElapsed(tODTimer *pTimer)
  524. {
  525. ASSERT(pTimer != NULL);
  526. #ifdef ODPLAT_DOS
  527. return(clock() > pTimer->Start + pTimer->Duration
  528. || clock() < pTimer->Start);
  529. #endif /* ODPLAT_DOS */
  530. #ifdef ODPLAT_WIN32
  531. return(ODTimerLeft(pTimer)==0);
  532. #endif /* ODPLAT_WIN32 */
  533. #ifdef ODPLAT_NIX
  534. return(ODTimerLeft(pTimer)==0);
  535. #endif
  536. }
  537. /* ----------------------------------------------------------------------------
  538. * ODTimerWaitForElapse()
  539. *
  540. * Sleeps until the specified timer elapses.
  541. *
  542. * Parameters: pTimer - Pointer to a tODTimer structure that was populated
  543. * by ODTimerStart().
  544. *
  545. * Return: void
  546. */
  547. void ODTimerWaitForElapse(tODTimer *pTimer)
  548. {
  549. ASSERT(pTimer != NULL);
  550. #ifdef ODPLAT_DOS
  551. /* Under DOS, our timer resolution is low enough (only 18.2 ticks per */
  552. /* second), that we cannot accurately calculate the time to sleep for. */
  553. /* For this reason, we simply loop until the timer has elapsed, yielding */
  554. /* control to other tasks if when the timer has not elapsed. */
  555. /* While timer has not elapsed. */
  556. while(!ODTimerElapsed(pTimer))
  557. {
  558. /* Let other tasks run. */
  559. od_sleep(0);
  560. }
  561. #else /* !ODPLAT_DOS */
  562. /* Under other platforms, timer resolution is high enough that we can */
  563. /* ask the OS to block this thread for the amount of time required */
  564. /* for the timer to elapse. */
  565. od_sleep(ODTimerLeft(pTimer));
  566. #endif /* !ODPLAT_DOS */
  567. }
  568. /* ----------------------------------------------------------------------------
  569. * ODTimerLeft()
  570. *
  571. * Determines the number of milliseconds left before the timer elapses.
  572. *
  573. * Parameters: pTimer - Pointer to a tODTimer structure that was populated
  574. * by ODTimerStart().
  575. *
  576. * Return: Number of milliseconds before timer elapses, or 0 if the timer
  577. * has already elapsed.
  578. */
  579. tODMilliSec ODTimerLeft(tODTimer *pTimer)
  580. {
  581. #ifdef ODPLAT_NIX
  582. struct timeval tv;
  583. time_t nowtick;
  584. #endif
  585. ASSERT(pTimer != NULL);
  586. #ifdef ODPLAT_DOS
  587. {
  588. clock_t Now = clock();
  589. clock_t Left;
  590. /* If timer has elapsed, return 0. */
  591. if(Now > pTimer->Start + pTimer->Duration
  592. || Now < pTimer->Start)
  593. {
  594. return(0);
  595. }
  596. Left = pTimer->Start + pTimer->Duration - Now;
  597. return(ODDWordMultiply(Left, MILLISEC_PER_TICK));
  598. }
  599. #elif defined(ODPLAT_NIX)
  600. gettimeofday(&tv,NULL);
  601. nowtick=tv.tv_sec*1000+(tv.tv_usec/1000);
  602. if(pTimer->Start+pTimer->Duration <= nowtick)
  603. return(0);
  604. return((tODMilliSec)(pTimer->Start + pTimer->Duration - nowtick));
  605. #else /* !ODPLAT_DOS */
  606. {
  607. tODMilliSec Now;
  608. #ifdef ODPLAT_WIN32
  609. Now = GetTickCount();
  610. #endif /* ODPLAT_WIN32 */
  611. /* If timer has elapsed, return 0. */
  612. if(Now > pTimer->Start + pTimer->Duration
  613. || Now < pTimer->Start)
  614. {
  615. return(0);
  616. }
  617. return(pTimer->Start + pTimer->Duration - Now);
  618. }
  619. #endif /* !ODPLAT_DOS */
  620. }
  621. /* ----------------------------------------------------------------------------
  622. * od_sleep()
  623. *
  624. * Sleeps for the specified number of milliseconds, being as friendly to other
  625. * running tasks as possible. Under DOS, this function uses the ODTimerStart()/
  626. * ODTimerElapsed() mechanism, and so its accuracy is limited by the accuracy
  627. * of that mechanism.
  628. *
  629. * Parameters: Milliseconds - Number of milliseconds to sleep. A value of 0
  630. * allows any other waiting processes to run for
  631. * the rest of the current timeslice.
  632. *
  633. * Return: void
  634. */
  635. ODAPIDEF void ODCALL od_sleep(tODMilliSec Milliseconds)
  636. {
  637. #ifdef ODPLAT_NIX
  638. struct timeval tv;
  639. struct timeval start;
  640. time_t started;
  641. time_t left
  642. #endif
  643. /* Log function entry if running in trace mode. */
  644. TRACE(TRACE_API, "od_sleep()");
  645. /* Ensure that OpenDoors is initialized before proceeding. */
  646. if(!bODInitialized) od_init();
  647. OD_API_ENTRY();
  648. #ifdef ODPLAT_DOS
  649. if(Milliseconds == 0)
  650. {
  651. ODPlatYield();
  652. }
  653. else
  654. {
  655. tODTimer SleepTimer;
  656. ODTimerStart(&SleepTimer, Milliseconds);
  657. while(!ODTimerElapsed(&SleepTimer))
  658. {
  659. /* Let other tasks run. */
  660. ODPlatYield();
  661. }
  662. }
  663. #endif /* ODPLAT_DOS */
  664. #ifdef ODPLAT_WIN32
  665. Sleep(Milliseconds);
  666. #endif /* ODPLAT_WIN32 */
  667. #ifdef ODPLAT_NIX
  668. if(Milliseconds==0) {
  669. /* Prevent 100% CPU *only* no delay is actually required here */
  670. tv.tv_sec=0;
  671. tv.tv_usec=1000;
  672. select(0,NULL,NULL,NULL,&tv);
  673. }
  674. else {
  675. gettimeofday(&start,NULL);
  676. started=start.tv_sec*1000+(start.tv_usec/1000);
  677. while(1) {
  678. /* This is timing sensitive and *MUST* wait for at least Milliseconds regardless of 100% CPU or signals */
  679. gettimeofday(&tv,NULL);
  680. left=tv.tv_sec*1000+(tv.tv_usec/1000);
  681. left-=started;
  682. left=Milliseconds-left;
  683. tv.tv_sec = left/1000;
  684. tv.tv_usec = (left*1000)%1000000;
  685. if(tv.tv_sec<0 || tv.tv_usec<0)
  686. break;
  687. if(!select(0,NULL,NULL,NULL,&tv))
  688. break;
  689. }
  690. }
  691. #endif
  692. OD_API_EXIT();
  693. }
  694. /* ========================================================================= */
  695. /* Directory access. */
  696. /* ========================================================================= */
  697. /* Structure for directories entries returned by DOS. */
  698. #ifdef ODPLAT_DOS
  699. typedef struct
  700. {
  701. BYTE abtReserved[21];
  702. BYTE btAttrib;
  703. WORD wFileTime;
  704. WORD wFileDate;
  705. DWORD dwFileSize;
  706. char szFileName[13];
  707. } tDOSDirEntry;
  708. #endif /* ODPLAT_DOS */
  709. /* Dir handle structure. */
  710. typedef struct
  711. {
  712. BOOL bEOF;
  713. #ifdef ODPLAT_DOS
  714. tDOSDirEntry FindBlock;
  715. #endif /* ODPLAT_DOS */
  716. #ifdef ODPLAT_WIN32
  717. HANDLE hWindowsDir;
  718. WIN32_FIND_DATA WindowsDirEntry;
  719. int wAttributes;
  720. #endif /* ODPLAT_WIN32 */
  721. #ifdef ODPLAT_NIX
  722. glob_t g;
  723. int pos;
  724. int wAttributes;
  725. #endif
  726. } tODDirInfo;
  727. /* Directory access private function prototypes. */
  728. #if defined(ODPLAT_DOS) || defined(ODPLAT_WIN32)
  729. static time_t DOSToCTime(WORD wDate, WORD wTime);
  730. #endif
  731. #ifdef ODPLAT_DOS
  732. static INT ODDirDOSFindFirst(CONST char *pszPath, tDOSDirEntry *pBlock,
  733. WORD wAttributes);
  734. static INT ODDirDOSFindNext(tDOSDirEntry *pBlock);
  735. #endif /* ODPLAT_DOS */
  736. #ifdef ODPLAT_WIN32
  737. static BOOL ODDirWinMatchesAttributes(tODDirInfo *pDirInfo);
  738. #endif /* ODPLAT_WIN32 */
  739. /* ----------------------------------------------------------------------------
  740. * ODDirOpen()
  741. *
  742. * Opens a directory for future access using ODDirRead(). On Success,
  743. * ODDirOpen() provides a directory handle that represents a list of directory
  744. * entries that match the specified path and attributes. When finished with
  745. * the directory handle, the caller should release it using ODDirClose().
  746. *
  747. * Parameters: pszPath - Directory with filename (wildcards are supported),
  748. * for which matching files should be found. If there
  749. * are no matching files, ODDirOpen() returns with
  750. * kODRCNoMatch.
  751. *
  752. * nAttributes - One or more of the DIR_ATTRIB_... constants,
  753. * connected by the bitmap-OR (|) operator.
  754. *
  755. * phDir - Pointer to a tODDirHandle, into which ODDirOpen()
  756. * will place a valid directory handle if and only
  757. * if it returns kODRCSuccess.
  758. *
  759. * Return: A tODResult indicating success or reason for failure.
  760. */
  761. tODResult ODDirOpen(CONST char *pszPath, WORD wAttributes, tODDirHandle *phDir)
  762. {
  763. tODDirInfo *pDirInfo;
  764. ASSERT(pszPath != NULL);
  765. ASSERT(phDir != NULL);
  766. /* Attempt to allocate a directory information structure. */
  767. if((pDirInfo = malloc(sizeof(tODDirInfo))) == NULL)
  768. {
  769. /* If unable to allocate enough memory, return this state to the */
  770. /* caller. */
  771. return(kODRCNoMemory);
  772. }
  773. /* Initialize directory information structure. */
  774. pDirInfo->bEOF = FALSE;
  775. #ifdef ODPLAT_DOS
  776. /* Read the first matching directory entry structure. */
  777. if(ODDirDOSFindFirst(pszPath, &pDirInfo->FindBlock, wAttributes))
  778. {
  779. /* If unable to read directory entry, release directory information */
  780. /* structure, and return indicating that there are no matching files. */
  781. free(pDirInfo);
  782. return(kODRCNoMatch);
  783. }
  784. #endif /* ODPLAT_DOS */
  785. #ifdef ODPLAT_WIN32
  786. /* Store a copy of the attributes passed to open function. */
  787. pDirInfo->wAttributes = wAttributes;
  788. /* Attempt to read first directory entry. */
  789. pDirInfo->hWindowsDir = FindFirstFile(pszPath, &pDirInfo->WindowsDirEntry);
  790. if(pDirInfo->hWindowsDir == INVALID_HANDLE_VALUE)
  791. {
  792. /* If unable to read directory entry, release directory information */
  793. /* structure, and return indicating that there are no matching files. */
  794. free(pDirInfo);
  795. return(kODRCNoMatch);
  796. }
  797. /* If first file doesn't match specified attributes, then find one that */
  798. /* does. */
  799. /* Find next matching entry, if any. */
  800. while(!ODDirWinMatchesAttributes(pDirInfo))
  801. {
  802. if(!FindNextFile(pDirInfo->hWindowsDir, &pDirInfo->WindowsDirEntry))
  803. {
  804. /* If unable to find matching directory entry, then release */
  805. /* structure, return indicating that there are no matching files. */
  806. free(pDirInfo);
  807. return(kODRCNoMatch);
  808. }
  809. }
  810. #endif /* ODPLAT_WIN32 */
  811. #ifdef ODPLAT_NIX
  812. if(glob(pszPath,GLOB_NOSORT,NULL,&(pDirInfo->g)))
  813. return(kODRCNoMatch);
  814. if(pDirInfo->g.gl_pathc==0) {
  815. globfree(&(pDirInfo->g));
  816. return(kODRCNoMatch);
  817. }
  818. pDirInfo->pos=0;
  819. pDirInfo->wAttributes = wAttributes;
  820. #endif
  821. /* Now that open operation is complete, give the caller a directory */
  822. /* handle. */
  823. *phDir = ODPTR2HANDLE(pDirInfo, tODDirInfo);
  824. /* Return with success. */
  825. return(kODRCSuccess);
  826. }
  827. /* ----------------------------------------------------------------------------
  828. * ODDirRead()
  829. *
  830. * Reads the next directory entry from an open directory, placing the directory
  831. * information into the tODDirEntry structure pointed to by pDirEntry.
  832. *
  833. * Parameters: hDir - Handle to an open directory, as provided by
  834. * ODDirOpen().
  835. *
  836. * pDirEntry - Pointer to structure into which directory entry
  837. * information should be placed.
  838. *
  839. * Return: A tODResult indicating success or reason for failure. After the
  840. * last directory entry has been read, all subsequent calls to
  841. * ODDirRead() will return kODRCEndOfFile.
  842. */
  843. tODResult ODDirRead(tODDirHandle hDir, tODDirEntry *pDirEntry)
  844. {
  845. tODDirInfo *pDirInfo = ODHANDLE2PTR(hDir, tODDirInfo);
  846. #ifdef ODPLAT_WIN32
  847. WORD wDOSDate;
  848. WORD wDOSTime;
  849. #endif /* ODPLAT_WIN32 */
  850. #ifdef ODPLAT_NIX
  851. struct stat st;
  852. #endif
  853. ASSERT(pDirEntry != NULL);
  854. ASSERT(pDirInfo != NULL);
  855. /* Check whether the last directory entry has been returned yet. */
  856. if(pDirInfo->bEOF)
  857. {
  858. /* Return this state information to the caller. */
  859. return(kODRCEndOfFile);
  860. }
  861. #ifdef ODPLAT_DOS
  862. /* Provide the caller with the information from the previously read */
  863. /* directory entry. */
  864. /* Copy the filename to the caller's structure. */
  865. ODStringCopy(pDirEntry->szFileName, pDirInfo->FindBlock.szFileName,
  866. DIR_FILENAME_SIZE);
  867. /* Copy the attributes to the caller's structure. */
  868. pDirEntry->wAttributes = pDirInfo->FindBlock.btAttrib;
  869. /* Copy the file size to the caller's structure. */
  870. pDirEntry->dwFileSize = pDirInfo->FindBlock.dwFileSize;
  871. /* Determine the last file write time, in C library time format. */
  872. pDirEntry->LastWriteTime = DOSToCTime(pDirInfo->FindBlock.wFileDate,
  873. pDirInfo->FindBlock.wFileTime);
  874. /* Read next directory entry, if any. */
  875. pDirInfo->bEOF = ODDirDOSFindNext(&pDirInfo->FindBlock);
  876. #endif /* ODPLAT_DOS */
  877. #ifdef ODPLAT_WIN32
  878. /* Provide the caller with the information from the previously read */
  879. /* directory entry. */
  880. /* Copy filename from Win32 8.3 filename. */
  881. if(strlen(pDirInfo->WindowsDirEntry.cAlternateFileName) == 0)
  882. {
  883. ODStringCopy(pDirEntry->szFileName,
  884. pDirInfo->WindowsDirEntry.cFileName, DIR_FILENAME_SIZE);
  885. }
  886. else
  887. {
  888. ODStringCopy(pDirEntry->szFileName,
  889. pDirInfo->WindowsDirEntry.cAlternateFileName, DIR_FILENAME_SIZE);
  890. }
  891. /* Copy attribute bits. */
  892. pDirEntry->wAttributes = DIR_ATTRIB_NORMAL;
  893. if(pDirInfo->WindowsDirEntry.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE)
  894. {
  895. pDirEntry->wAttributes |= DIR_ATTRIB_ARCH;
  896. }
  897. if(pDirInfo->WindowsDirEntry.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  898. {
  899. pDirEntry->wAttributes |= DIR_ATTRIB_DIREC;
  900. }
  901. if(pDirInfo->WindowsDirEntry.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
  902. {
  903. pDirEntry->wAttributes |= DIR_ATTRIB_HIDDEN;
  904. }
  905. if(pDirInfo->WindowsDirEntry.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
  906. {
  907. pDirEntry->wAttributes |= DIR_ATTRIB_RDONLY;
  908. }
  909. if(pDirInfo->WindowsDirEntry.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM)
  910. {
  911. pDirEntry->wAttributes |= DIR_ATTRIB_SYSTEM;
  912. }
  913. /* Copy the file size to the caller's structure. */
  914. pDirEntry->dwFileSize = (long)pDirInfo->WindowsDirEntry.nFileSizeLow;
  915. /* Determine the last file write time, in C library time format. */
  916. FileTimeToDosDateTime(&pDirInfo->WindowsDirEntry.ftLastWriteTime, &wDOSDate,
  917. &wDOSTime);
  918. pDirEntry->LastWriteTime = DOSToCTime(wDOSDate, wDOSTime);
  919. /* Find next matching entry, if any. */
  920. do
  921. {
  922. if(!FindNextFile(pDirInfo->hWindowsDir, &pDirInfo->WindowsDirEntry))
  923. {
  924. pDirInfo->bEOF = TRUE;
  925. }
  926. } while(!ODDirWinMatchesAttributes(pDirInfo));
  927. #endif /* ODPLAT_WIN32 */
  928. #ifdef ODPLAT_NIX
  929. while(!pDirInfo->bEOF) {
  930. if(strrchr(pDirInfo->g.gl_pathv[pDirInfo->pos],DIRSEP)==NULL)
  931. strcpy(pDirEntry->szFileName,pDirInfo->g.gl_pathv[pDirInfo->pos]);
  932. else
  933. strcpy(pDirEntry->szFileName,strrchr(pDirInfo->g.gl_pathv[pDirInfo->pos],DIRSEP));
  934. stat(pDirInfo->g.gl_pathv[pDirInfo->pos],&st);
  935. pDirEntry->wAttributes=DIR_ATTRIB_NORMAL;
  936. if(st.st_mode & S_IFDIR)
  937. pDirEntry->wAttributes |= DIR_ATTRIB_DIREC;
  938. if(!st.st_mode & S_IWUSR)
  939. pDirEntry->wAttributes |= DIR_ATTRIB_RDONLY;
  940. if(!st.st_mode & S_IRUSR)
  941. pDirEntry->wAttributes |= DIR_ATTRIB_SYSTEM;
  942. pDirEntry->LastWriteTime=st.st_mtime;
  943. pDirEntry->dwFileSize=st.st_size;
  944. pDirInfo->pos++;
  945. if(pDirInfo->pos==pDirInfo->g.gl_pathc)
  946. pDirInfo->bEOF=TRUE;
  947. if(pDirEntry->wAttributes==pDirInfo->wAttributes)
  948. return(kODRCSuccess);
  949. if(pDirInfo->bEOF==TRUE)
  950. return(kODRCEndOfFile);
  951. }
  952. #endif
  953. /* Return with success. */
  954. return(kODRCSuccess);
  955. }
  956. /* ----------------------------------------------------------------------------
  957. * ODDirClose()
  958. *
  959. * Closes and open directory handle.
  960. *
  961. * Parameters: hDir - Handle to an open directory handle, as provided by the
  962. * ODDirOpen() function.
  963. *
  964. * Return: void
  965. */
  966. void ODDirClose(tODDirHandle hDir)
  967. {
  968. tODDirInfo *pDirInfo = ODHANDLE2PTR(hDir, tODDirInfo);
  969. ASSERT(pDirInfo != NULL);
  970. #ifdef ODPLAT_WIN32
  971. /* Under Win32, close directory handle. */
  972. FindClose(pDirInfo->hWindowsDir);
  973. #endif /* ODPLAT_WIN32 */
  974. #ifdef ODPLAT_NIX
  975. globfree(&(pDirInfo->g));
  976. #endif
  977. /* Free the directory information structure. */
  978. free(pDirInfo);
  979. }
  980. #if defined(ODPLAT_DOS) || defined(ODPLAT_WIN32)
  981. /* ----------------------------------------------------------------------------
  982. * DOSToCTime() *** PRIVATE FUNCTION ***
  983. *
  984. * Converts DOS directory entry time format to the C library time format.
  985. *
  986. * Parameters: uDate - Date portion of the time to be converted.
  987. *
  988. * uTime - Time of day portion of the time to be converted.
  989. *
  990. * Return: The specified time, represented as a time_t.
  991. */
  992. static time_t DOSToCTime(WORD wDate, WORD wTime)
  993. {
  994. struct tm TimeStruct;
  995. TimeStruct.tm_sec = (wTime & 0x001f) * 2;
  996. TimeStruct.tm_min = (wTime & 0x07e0) >> 5;
  997. TimeStruct.tm_hour = (wTime & 0xf800) >> 11;
  998. TimeStruct.tm_mday = wDate & 0x001f;
  999. TimeStruct.tm_mon = ((wDate & 0x01e0) >> 5) - 1;
  1000. TimeStruct.tm_year = 80 + ((wDate & 0xfe00) >> 9);
  1001. return(mktime(&TimeStruct));
  1002. }
  1003. #endif
  1004. /* MS-DOS specific functions for directory access. */
  1005. #ifdef ODPLAT_DOS
  1006. /* ----------------------------------------------------------------------------
  1007. * ODDirDOSFindFirst() *** PRIVATE FUNCTION ***
  1008. *
  1009. * MS-DOS specific "Find First" function for reading directory entries. This
  1010. * is essentially just a C-language interface to the interrupt function call
  1011. * that is provided by DOS.
  1012. *
  1013. * Parameters: pszPath - Pointer to string containing directory and
  1014. * filespec to search for.
  1015. *
  1016. * pBlock - Pointer to directory block.
  1017. *
  1018. * nAttributes - Attributes to match, if any.
  1019. *
  1020. * Return: 0 on success, -1 on failure.
  1021. */
  1022. static int ODDirDOSFindFirst(CONST char *pszPath, tDOSDirEntry *pBlock,
  1023. WORD wAttributes)
  1024. {
  1025. int nToReturn;
  1026. ASSERT(pszPath != NULL);
  1027. ASSERT(pBlock != NULL);
  1028. ASM push ds
  1029. ASM mov ah, 0x2f /* Int 0x21, ah=0x2f: Get current DOS DTA */
  1030. ASM int 0x21 /* Get current DTA */
  1031. ASM push bx /* Store offset of current DTA on stack */
  1032. ASM push es /* Store segment of current DTA on stack */
  1033. ASM mov ah, 0x1a /* Int 0x21, ah=0x1a: Set new DOS DTA */
  1034. #ifdef LARGEDATA /* If using far pointers */
  1035. ASM lds dx, pBlock /* Load DS:DX with far address of pBlock */
  1036. #else /* If using near pointers */
  1037. ASM mov dx, pBlock /* Load DX with near address of pBlock */
  1038. #endif
  1039. ASM int 0x21 /* Set DOS DTA */
  1040. ASM mov ah, 0x4e /* Int 0x21, ah=0x4e: DOS findfirst function */
  1041. ASM mov cx, wAttributes /* Load attributes into CX */
  1042. #ifdef LARGEDATA /* If using far pointers */
  1043. ASM lds dx, pszPath /* Load DS:DX with far address in pszPath */
  1044. #else /* If using near pointers */
  1045. ASM mov dx, pszPath /* Load DX with near address in pszPath */
  1046. #endif
  1047. ASM int 0x21 /* Call findfirst function */
  1048. ASM jc error /* If carry flag is set, then an error has ocurred */
  1049. ASM mov word ptr nToReturn, 0 /* If no error, return 0 */
  1050. ASM jmp after_result
  1051. error:
  1052. ASM mov word ptr nToReturn, -1 /* If error, return -1 */
  1053. after_result:
  1054. ASM mov ah, 0x1a /* Int 0x21, ah=0x1a: Set new DOS DTA */
  1055. ASM pop ds /* Pop original DTA segment off of stack */
  1056. ASM pop dx /* Pop original DTA offest from stack */
  1057. ASM int 0x21 /* Reset DOS DTA to original */
  1058. ASM pop ds /* Restore DS stored at function startup */
  1059. return(nToReturn);
  1060. }
  1061. /* ----------------------------------------------------------------------------
  1062. * ODDirDOSFindNext() *** PRIVATE FUNCTION ***
  1063. *
  1064. * MS-DOS specific "Find Next" function for reading directory entries. This
  1065. * is essentially just a C-language interface to the interrupt function call
  1066. * that is provided by DOS.
  1067. *
  1068. * Parameters: pBlock - Pointer to block in which to store next directory
  1069. * entry.
  1070. *
  1071. * Return: 0 on success, -1 on failure.
  1072. */
  1073. static int ODDirDOSFindNext(tDOSDirEntry *pBlock)
  1074. {
  1075. int nToReturn;
  1076. ASSERT(pBlock != NULL);
  1077. ASM push ds /* Save DS */
  1078. ASM mov ah, 0x2f /* Int 0x21, ah=0x2f: Get current DOS DTA */
  1079. ASM int 0x21 /* Get current DTA */
  1080. ASM push bx /* Store offset of current DTA on stack */
  1081. ASM push es /* Store segment of current DTA on stack */
  1082. ASM mov ah, 0x1a /* Int 0x21, ah=0x1a: Set new DOS DTA */
  1083. #ifdef LARGEDATA /* If using far pointers */
  1084. ASM lds dx, pBlock /* Load DS:DX with far address of pBlock */
  1085. #else /* If using near pointers */
  1086. ASM mov dx, pBlock /* Load DX with near address of pBlock */
  1087. #endif
  1088. ASM int 0x21 /* Set DOS DTA */
  1089. ASM mov ah, 0x4f /* Int 0x21, ah=0x4f: DOS findnext function */
  1090. ASM int 0x21 /* Call findfirst function */
  1091. ASM jc error /* If carry flag is set, then an error has ocurred */
  1092. ASM mov word ptr nToReturn, 0 /* If no error, return 0 */
  1093. ASM jmp after_result
  1094. error:
  1095. ASM mov word ptr nToReturn, -1 /* If error, return -1 */
  1096. after_result:
  1097. ASM mov ah, 0x1a /* Int 0x21, ah=0x1a: Set new DOS DTA */
  1098. ASM pop ds /* Pop original DTA segment off of stack */
  1099. ASM pop dx /* Pop original DTA offest from stack */
  1100. ASM int 0x21 /* Reset DOS DTA to original */
  1101. ASM pop ds /* Restore DS stored at function startup */
  1102. return(nToReturn);
  1103. }
  1104. #endif /* ODPLAT_DOS */
  1105. /* Win32 specific private functions for directory access. */
  1106. #ifdef ODPLAT_WIN32
  1107. /* ----------------------------------------------------------------------------
  1108. * ODDirWinMatchesAttributes() *** PRIVATE FUNCTION ***
  1109. *
  1110. * Determines whether or not the directory entry pDirInfo->WindowsDirEntry
  1111. * meets the attribute requirements specified in pDirInfo->nAttributes
  1112. *
  1113. * Parameters: pDirInfo - Pointer to a directory information structure with
  1114. * attribute and directory entry values.
  1115. *
  1116. * Return: TRUE if the file matches the attributes, FALSE if it does not.
  1117. */
  1118. static BOOL ODDirWinMatchesAttributes(tODDirInfo *pDirInfo)
  1119. {
  1120. if((pDirInfo->WindowsDirEntry.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  1121. && !(pDirInfo->wAttributes & DIR_ATTRIB_DIREC))
  1122. {
  1123. return(FALSE);
  1124. }
  1125. if((pDirInfo->WindowsDirEntry.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE)
  1126. && !(pDirInfo->wAttributes & DIR_ATTRIB_ARCH))
  1127. {
  1128. return(FALSE);
  1129. }
  1130. if((pDirInfo->WindowsDirEntry.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
  1131. && !(pDirInfo->wAttributes & DIR_ATTRIB_HIDDEN))
  1132. {
  1133. return(FALSE);
  1134. }
  1135. if((pDirInfo->WindowsDirEntry.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
  1136. && !(pDirInfo->wAttributes & DIR_ATTRIB_RDONLY))
  1137. {
  1138. return(FALSE);
  1139. }
  1140. if((pDirInfo->WindowsDirEntry.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM)
  1141. && !(pDirInfo->wAttributes & DIR_ATTRIB_SYSTEM))
  1142. {
  1143. return(FALSE);
  1144. }
  1145. return(TRUE);
  1146. }
  1147. #endif /* ODPLAT_WIN32 */
  1148. /* ----------------------------------------------------------------------------
  1149. * ODDirChangeCurrent()
  1150. *
  1151. * Changes current directory to the one specified.
  1152. *
  1153. * Parameters: pszPath - String containing path to change to.
  1154. *
  1155. * Return: void
  1156. */
  1157. void ODDirChangeCurrent(char *pszPath)
  1158. {
  1159. #ifdef ODPLAT_DOS
  1160. int nDrive = 0;
  1161. if(pszPath[1] == ':')
  1162. {
  1163. nDrive = (toupper(pszPath[0]) - 'A');
  1164. }
  1165. _setdrvcd(nDrive, (char *)pszPath);
  1166. #endif /* ODPLAT_DOS */
  1167. #ifdef ODPLAT_WIN32
  1168. SetCurrentDirectory(pszPath);
  1169. #endif /* ODPLAT_WIN32 */
  1170. #ifdef ODPLAT_NIX
  1171. chdir(pszPath);
  1172. #endif
  1173. }
  1174. /* ----------------------------------------------------------------------------
  1175. * ODDirGetCurrent()
  1176. *
  1177. * Obtains the name of the current directory, including the current drive
  1178. * designator.
  1179. *
  1180. * Parameters: pszPath - String containing path to change to.
  1181. *
  1182. * nMaxPathChars - Maximum characters in the buffer pointer to by
  1183. * pszPath.
  1184. *
  1185. * Return: void
  1186. */
  1187. void ODDirGetCurrent(char *pszPath, INT nMaxPathChars)
  1188. {
  1189. ASSERT(pszPath != NULL);
  1190. ASSERT(nMaxPathChars > 0);
  1191. #ifdef ODPLAT_DOS
  1192. UNUSED(nMaxPathChars);
  1193. strcpy(pszPath, "X:\\");
  1194. pszPath[0] = 'A' + _getdrv();
  1195. _getcd(0, (char *)pszPath + 3);
  1196. #endif /* ODPLAT_DOS */
  1197. #ifdef ODPLAT_WIN32
  1198. GetCurrentDirectory(nMaxPathChars, pszPath);
  1199. #endif /* ODPLAT_WIN32 */
  1200. #ifdef ODPLAT_NIX
  1201. getcwd(pszPath,nMaxPathChars);
  1202. #endif
  1203. ASSERT((INT)strlen(pszPath) + 1 <= nMaxPathChars);
  1204. }
  1205. /* ========================================================================= */
  1206. /* Misc. Functions */
  1207. /* ========================================================================= */
  1208. /* ----------------------------------------------------------------------------
  1209. * ODFileDelete()
  1210. *
  1211. * Deletes the file with the specified filename.
  1212. *
  1213. * Parameters: pszPath - Filename, possibly with path, to delete.
  1214. *
  1215. * Return: kODRCSuccess on success, or an error code on failure.
  1216. */
  1217. tODResult ODFileDelete(CONST char *pszPath)
  1218. {
  1219. #ifdef ODPLAT_DOS
  1220. {
  1221. tODResult Result;
  1222. ASM push ds
  1223. #ifdef LARGEDATA
  1224. ASM lds dx, pszPath
  1225. #else /* !LARGEDATA */
  1226. ASM mov ax, ss
  1227. ASM mov ds, ax
  1228. ASM mov dx, pszPath
  1229. #endif /* !LARGEDATA */
  1230. ASM mov ah, 0x41
  1231. ASM int 0x21
  1232. ASM jc Failure
  1233. ASM mov word ptr Result, kODRCSuccess
  1234. ASM jmp Done
  1235. Failure:
  1236. ASM mov word ptr Result, kODRCGeneralFailure
  1237. Done:
  1238. ASM pop ds
  1239. return(Result);
  1240. }
  1241. #endif /* ODPLAT_DOS */
  1242. #ifdef ODPLAT_WIN32
  1243. return(DeleteFile(pszPath) ? kODRCSuccess : kODRCGeneralFailure);
  1244. #endif /* ODPLAT_WIN32 */
  1245. #ifdef ODPLAT_NIX
  1246. return(unlink(pszPath));
  1247. #endif
  1248. }
  1249. /* ----------------------------------------------------------------------------
  1250. * ODFileAccessMode()
  1251. *
  1252. * Determines the access permissions of a file.
  1253. *
  1254. * Parameters: pszFilename - Name of file to test.
  1255. *
  1256. * nAccessMode - Indicates which file access mode to test for.
  1257. * A value of 0 indicates existance, 2 indicates
  1258. * write permission, 4 indicates read permission,
  1259. * and 6 indicates read/write permission.
  1260. *
  1261. * Return: FALSE if file can be accessed or TRUE if file cannot be
  1262. * accessed.
  1263. */
  1264. BOOL ODFileAccessMode(char *pszFilename, int nAccessMode)
  1265. {
  1266. FILE *pfFileToTest;
  1267. char *pszModeString;
  1268. tODDirHandle hDir;
  1269. #ifdef ODPLAT_DOS
  1270. BYTE nLength;
  1271. /* If we are looking for the root directory. */
  1272. nLength = strlen(pszFilename);
  1273. if((nLength == 3 && pszFilename[1] == ':' && pszFilename[2] == DIRSEP) ||
  1274. (nLength == 1 && pszFilename[0] == DIRSEP))
  1275. {
  1276. if(nAccessMode == 0)
  1277. {
  1278. int to_return = FALSE;
  1279. #ifdef LARGEDATA
  1280. ASM push ds
  1281. ASM lds dx, pszFilename
  1282. #else
  1283. ASM mov dx, pszFilename
  1284. #endif
  1285. ASM mov ax, 0x4300
  1286. ASM int 0x21
  1287. ASM jnc done
  1288. ASM mov word ptr to_return, TRUE
  1289. done:
  1290. #ifdef LARGEDATA
  1291. ASM pop ds
  1292. #endif
  1293. return(to_return);
  1294. }
  1295. else
  1296. {
  1297. return(TRUE);
  1298. }
  1299. }
  1300. #endif /* ODPLAT_DOS */
  1301. /* If the file doesn't exit, we fail in any mode. */
  1302. if(ODDirOpen(pszFilename,
  1303. DIR_ATTRIB_ARCH | DIR_ATTRIB_RDONLY | DIR_ATTRIB_DIREC,
  1304. &hDir) != kODRCSuccess)
  1305. {
  1306. return(TRUE);
  1307. }
  1308. /* If directory open succeeded, then close it again. */
  1309. ODDirClose(hDir);
  1310. /* If the file does exist, then amode 0 is satisfied. */
  1311. if(nAccessMode == 0) return(FALSE);
  1312. /* If testing for an access permission, determine corresponding fopen() */
  1313. /* mode. */
  1314. switch(nAccessMode)
  1315. {
  1316. case 2:
  1317. pszModeString = "a";
  1318. break;
  1319. case 4:
  1320. pszModeString = "r";
  1321. break;
  1322. default:
  1323. pszModeString = "r+";
  1324. }
  1325. /* Attempt to open the file, if unable to do so return failure. */
  1326. if((pfFileToTest=fopen(pszFilename,pszModeString)) == NULL) return(TRUE);
  1327. /* If file open was successful, close it again, and return success. */
  1328. fclose(pfFileToTest);
  1329. return(FALSE);
  1330. }