Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1258 lines
28 KiB

  1. /* File: D:\WACKER\tdll\file_msc.c (Created: 26-Dec-1993)
  2. *
  3. * Copyright 1994 by Hilgraeve Inc. -- Monroe, MI
  4. * All rights reserved
  5. *
  6. * $Revision: 11 $
  7. * $Date: 7/08/02 6:41p $
  8. */
  9. #include <windows.h>
  10. #pragma hdrstop
  11. #include "stdtyp.h"
  12. #include "mc.h"
  13. #include "sf.h"
  14. #include "tdll.h"
  15. #include "sess_ids.h"
  16. #include <tdll\assert.h>
  17. #include "session.h"
  18. #include "open_msc.h"
  19. #include "htchar.h"
  20. #include "file_msc.h"
  21. #include "file_msc.hh"
  22. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  23. *
  24. * F I L E _ M S C . C
  25. *
  26. * This file contains functions that are needed to deal with files, names of
  27. * files, lists of files and just about anything else about files.
  28. *
  29. *=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  30. STATIC_FUNC int fmBFLinternal(void **pData,
  31. int *pCnt,
  32. LPCTSTR pszName,
  33. int nSubdir,
  34. LPCTSTR pszDirectory);
  35. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  36. * FUNCTION:
  37. * CreateFilesDirsHdl
  38. *
  39. * DESCRIPTION:
  40. * This function is called to create the files and directory handle.
  41. *
  42. * PARAMETERS:
  43. * hSession -- the session handle
  44. *
  45. * RETURNS:
  46. * A pointer to the HFILES handle.
  47. */
  48. HFILES CreateFilesDirsHdl(const HSESSION hSession)
  49. {
  50. FD_DATA *pFD;
  51. int nRet;
  52. pFD = (FD_DATA *)malloc(sizeof(FD_DATA));
  53. assert(pFD);
  54. if (pFD)
  55. {
  56. memset(pFD, 0, sizeof(FD_DATA));
  57. nRet = InitializeFilesDirsHdl(hSession, (HFILES)pFD);
  58. if (nRet)
  59. goto CFDHexit;
  60. }
  61. return (HFILES)pFD;
  62. CFDHexit:
  63. if (pFD)
  64. {
  65. if (pFD->pszInternalSendDirectory)
  66. {
  67. free(pFD->pszInternalSendDirectory);
  68. pFD->pszInternalSendDirectory = NULL;
  69. }
  70. if (pFD->pszTransferSendDirectory)
  71. {
  72. free(pFD->pszTransferSendDirectory);
  73. pFD->pszTransferSendDirectory = NULL;
  74. }
  75. if (pFD->pszInternalRecvDirectory)
  76. {
  77. free(pFD->pszInternalRecvDirectory);
  78. pFD->pszInternalRecvDirectory = NULL;
  79. }
  80. if (pFD->pszTransferRecvDirectory)
  81. {
  82. free(pFD->pszTransferRecvDirectory);
  83. pFD->pszTransferRecvDirectory = NULL;
  84. }
  85. free(pFD);
  86. pFD = NULL;
  87. }
  88. return (HFILES)0;
  89. }
  90. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  91. * FUNCTION:
  92. * InitializeFilesDirs
  93. *
  94. * DESCRIPTION:
  95. * This function is called to put the files and directorys handle into a
  96. * known and safe state.
  97. *
  98. * PARAMETERS:
  99. * hSession -- the session handle
  100. * hFile -- the files and directory handle
  101. *
  102. * RETURNS:
  103. * ZERO if everything is OK, otherwise an error code.
  104. */
  105. INT InitializeFilesDirsHdl(const HSESSION hSession, HFILES hFile)
  106. {
  107. FD_DATA *pFD;
  108. LPTSTR pszSname;
  109. LPTSTR pszRname;
  110. int nSize;
  111. TCHAR acDir[FNAME_LEN];
  112. pFD = (FD_DATA *)hFile;
  113. assert(pFD);
  114. pszSname = pszRname = (LPTSTR)0;
  115. if (pFD)
  116. {
  117. if (pFD->pszInternalSendDirectory)
  118. {
  119. free(pFD->pszInternalSendDirectory);
  120. pFD->pszInternalSendDirectory = NULL;
  121. }
  122. if (pFD->pszTransferSendDirectory)
  123. {
  124. free(pFD->pszTransferSendDirectory);
  125. pFD->pszTransferSendDirectory = (LPTSTR)0;
  126. }
  127. if (pFD->pszInternalRecvDirectory)
  128. {
  129. free(pFD->pszInternalRecvDirectory);
  130. pFD->pszInternalRecvDirectory = NULL;
  131. }
  132. if (pFD->pszTransferRecvDirectory)
  133. {
  134. free(pFD->pszTransferRecvDirectory);
  135. pFD->pszTransferRecvDirectory = (LPTSTR)0;
  136. }
  137. memset(pFD, 0, sizeof(FD_DATA));
  138. pFD->hSession = hSession;
  139. //Changed to use working path rather than current path - mpt 8-18-99
  140. if ( !GetWorkingDirectory( acDir, FNAME_LEN ) )
  141. {
  142. GetCurrentDirectory(FNAME_LEN, acDir);
  143. }
  144. nSize = StrCharGetByteCount(acDir) + sizeof(TCHAR);
  145. pszSname = malloc(nSize);
  146. if (pszSname == (LPTSTR)0)
  147. goto IFDexit;
  148. pszRname = malloc(nSize);
  149. if (pszRname == (LPTSTR)0)
  150. goto IFDexit;
  151. if (pFD->pszInternalSendDirectory)
  152. {
  153. free(pFD->pszInternalSendDirectory);
  154. pFD->pszInternalSendDirectory = NULL;
  155. }
  156. pFD->pszInternalSendDirectory = pszSname;
  157. StrCharCopyN(pFD->pszInternalSendDirectory, acDir, nSize);
  158. if (pFD->pszTransferSendDirectory)
  159. {
  160. free(pFD->pszTransferSendDirectory);
  161. pFD->pszTransferSendDirectory = (LPTSTR)0;
  162. }
  163. if (pFD->pszInternalRecvDirectory)
  164. {
  165. free(pFD->pszInternalRecvDirectory);
  166. pFD->pszInternalRecvDirectory = NULL;
  167. }
  168. pFD->pszInternalRecvDirectory = pszRname;
  169. StrCharCopyN(pFD->pszInternalRecvDirectory, acDir, nSize);
  170. if (pFD->pszTransferRecvDirectory)
  171. {
  172. free(pFD->pszTransferRecvDirectory);
  173. pFD->pszTransferRecvDirectory = (LPTSTR)0;
  174. }
  175. }
  176. return 0;
  177. IFDexit:
  178. if (pszSname)
  179. {
  180. free(pszSname);
  181. pszSname = NULL;
  182. }
  183. if (pszRname)
  184. {
  185. free(pszRname);
  186. pszRname = NULL;
  187. }
  188. return FM_ERR_NO_MEM;
  189. }
  190. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  191. * FUNCTION:
  192. * LoadFilesDirs
  193. *
  194. * DESCRIPTION:
  195. * This function is called to read whatever values are in the session file
  196. * into the files and directorys handle
  197. *
  198. * PARAMETERS:
  199. * hFile -- the files and directory handle
  200. *
  201. * RETURNS:
  202. * ZERO if everything is OK, otherwise an error code.
  203. */
  204. INT LoadFilesDirsHdl(HFILES hFile)
  205. {
  206. INT nRet = 0;
  207. FD_DATA *pFD;
  208. long lSize;
  209. LPTSTR pszStr;
  210. pFD = (FD_DATA *)hFile;
  211. assert(pFD);
  212. if (pFD == (FD_DATA *)0)
  213. return FM_ERR_BAD_HANDLE;
  214. InitializeFilesDirsHdl(pFD->hSession, hFile);
  215. if (nRet == 0)
  216. {
  217. lSize = 0;
  218. sfGetSessionItem(sessQuerySysFileHdl(pFD->hSession),
  219. SFID_XFR_SEND_DIR,
  220. &lSize,
  221. NULL);
  222. if (lSize != 0)
  223. {
  224. pszStr = (LPTSTR)malloc(lSize);
  225. if (pszStr)
  226. {
  227. sfGetSessionItem(sessQuerySysFileHdl(pFD->hSession),
  228. SFID_XFR_SEND_DIR,
  229. &lSize,
  230. pszStr);
  231. if (pFD->pszTransferSendDirectory)
  232. {
  233. free(pFD->pszTransferSendDirectory);
  234. pFD->pszTransferSendDirectory = NULL;
  235. }
  236. pFD->pszTransferSendDirectory = pszStr;
  237. }
  238. else
  239. {
  240. nRet = FM_ERR_NO_MEM;
  241. }
  242. }
  243. }
  244. if (nRet == 0)
  245. {
  246. lSize = 0;
  247. sfGetSessionItem(sessQuerySysFileHdl(pFD->hSession),
  248. SFID_XFR_RECV_DIR,
  249. &lSize,
  250. NULL);
  251. if (lSize != 0)
  252. {
  253. pszStr = (LPTSTR)malloc(lSize);
  254. if (pszStr)
  255. {
  256. sfGetSessionItem(sessQuerySysFileHdl(pFD->hSession),
  257. SFID_XFR_RECV_DIR,
  258. &lSize,
  259. pszStr);
  260. if (pFD->pszTransferRecvDirectory)
  261. {
  262. free(pFD->pszTransferRecvDirectory);
  263. pFD->pszTransferRecvDirectory = NULL;
  264. }
  265. pFD->pszTransferRecvDirectory = pszStr;
  266. }
  267. else
  268. {
  269. nRet = FM_ERR_NO_MEM;
  270. }
  271. }
  272. }
  273. return nRet;
  274. }
  275. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  276. * FUNCTION:
  277. * DestroyFilesDirsHdl
  278. *
  279. * DESCRIPTION:
  280. * This function is called to free all the memory that is in a Files and
  281. * Directorys handle. Gone. History. Toast.
  282. *
  283. * PARAMETERS:
  284. * hFile -- the files and directorys handle
  285. *
  286. * RETURNS:
  287. * ZERO if everything is OK, otherwise an error code.
  288. */
  289. INT DestroyFilesDirsHdl(const HFILES hFile)
  290. {
  291. INT nRet = 0;
  292. FD_DATA *pFD;
  293. pFD = (FD_DATA *)hFile;
  294. assert(pFD);
  295. if (pFD == (FD_DATA *)0)
  296. return FM_ERR_BAD_HANDLE;
  297. if (pFD->pszInternalSendDirectory)
  298. {
  299. free(pFD->pszInternalSendDirectory);
  300. pFD->pszInternalSendDirectory = NULL;
  301. }
  302. if (pFD->pszTransferSendDirectory)
  303. {
  304. free(pFD->pszTransferSendDirectory);
  305. pFD->pszTransferSendDirectory = NULL;
  306. }
  307. if (pFD->pszInternalRecvDirectory)
  308. {
  309. free(pFD->pszInternalRecvDirectory);
  310. pFD->pszInternalRecvDirectory = NULL;
  311. }
  312. if (pFD->pszTransferRecvDirectory)
  313. {
  314. free(pFD->pszTransferRecvDirectory);
  315. pFD->pszTransferRecvDirectory = NULL;
  316. }
  317. free(pFD);
  318. pFD = NULL;
  319. return 0;
  320. }
  321. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  322. * FUNCTION:
  323. * SaveFilesDirsHdl
  324. *
  325. * DESCRIPTION:
  326. * This function is called to save out to the session file all of the data
  327. * that has changed in the Files and Directorys handle.
  328. *
  329. * PARAMETERS:
  330. * hFile -- the files and directorys handle
  331. *
  332. * RETURNS:
  333. * ZERO if everything is OK, otherwise an error code.
  334. */
  335. INT SaveFilesDirsHdl(const HFILES hFile)
  336. {
  337. FD_DATA *pFD;
  338. long lSize;
  339. pFD = (FD_DATA *)hFile;
  340. assert(pFD);
  341. if (pFD == (FD_DATA *)0)
  342. return FM_ERR_BAD_HANDLE;
  343. if (pFD->pszTransferSendDirectory)
  344. {
  345. lSize = StrCharGetByteCount(pFD->pszTransferSendDirectory) + 1;
  346. sfPutSessionItem(sessQuerySysFileHdl(pFD->hSession),
  347. SFID_XFR_SEND_DIR,
  348. lSize,
  349. pFD->pszTransferSendDirectory);
  350. }
  351. if (pFD->pszTransferRecvDirectory)
  352. {
  353. lSize = StrCharGetByteCount(pFD->pszTransferRecvDirectory) + 1;
  354. sfPutSessionItem(sessQuerySysFileHdl(pFD->hSession),
  355. SFID_XFR_RECV_DIR,
  356. lSize,
  357. pFD->pszTransferRecvDirectory);
  358. }
  359. return 0;
  360. }
  361. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  362. * FUNCTION:
  363. * filesQuerySendDirectory
  364. *
  365. * DESCRIPTION:
  366. * This function returns a pointer to the current default transfer send
  367. * directory.
  368. *
  369. * PARAMETERS:
  370. * hFile -- the files and directorys handle
  371. *
  372. * RETURNS:
  373. * A pointer to the current default transfer send directory
  374. *
  375. */
  376. LPCTSTR filesQuerySendDirectory(HFILES hFile)
  377. {
  378. FD_DATA *pFD;
  379. pFD = (FD_DATA *)hFile;
  380. assert(pFD);
  381. assert(pFD->pszInternalSendDirectory);
  382. if (pFD->pszTransferSendDirectory == (LPTSTR)0)
  383. return (LPCTSTR)pFD->pszInternalSendDirectory;
  384. if (StrCharGetStrLength(pFD->pszTransferSendDirectory) == 0)
  385. return (LPCTSTR)pFD->pszInternalSendDirectory;
  386. return (LPCTSTR)pFD->pszTransferSendDirectory;
  387. }
  388. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  389. * FUNCTION:
  390. * filesQueryRecvDirectory
  391. *
  392. * DESCRIPTION:
  393. * This function returns a pointer to the current default transfer recv
  394. * directory.
  395. *
  396. * PARAMETERS:
  397. * hFile -- the files and directorys handle
  398. *
  399. * RETURNS:
  400. * A pointer to the current default recv directory
  401. *
  402. */
  403. LPCTSTR filesQueryRecvDirectory(HFILES hFile)
  404. {
  405. FD_DATA *pFD;
  406. pFD = (FD_DATA *)hFile;
  407. assert(pFD);
  408. assert(pFD->pszInternalRecvDirectory);
  409. if (pFD->pszTransferRecvDirectory == (LPTSTR)0)
  410. return (LPCTSTR)pFD->pszInternalRecvDirectory;
  411. if (StrCharGetStrLength(pFD->pszTransferRecvDirectory) == 0)
  412. return (LPCTSTR)pFD->pszInternalRecvDirectory;
  413. return (LPCTSTR)pFD->pszTransferRecvDirectory;
  414. }
  415. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  416. * FUNCTION:
  417. * filesSetSendDirectory
  418. *
  419. * DESCRIPTION:
  420. * This function is called to change (maybe) the current default sending
  421. * directory.
  422. *
  423. * PARAMETERS:
  424. * hFile -- the files and directorys handle
  425. * pszDir -- pointer to the new directory path
  426. *
  427. * RETURNS:
  428. * Nothing.
  429. *
  430. */
  431. VOID filesSetSendDirectory(HFILES hFile, LPCTSTR pszDir)
  432. {
  433. LPTSTR pszTmp;
  434. FD_DATA *pFD;
  435. int pszTmpLen;
  436. pFD = (FD_DATA *)hFile;
  437. assert(pFD);
  438. pszTmpLen = StrCharGetByteCount(pszDir) + 1;
  439. pszTmp = (LPTSTR)malloc(pszTmpLen);
  440. assert(pszTmp);
  441. if (pszTmp == NULL)
  442. return;
  443. StrCharCopyN(pszTmp, pszDir, pszTmpLen);
  444. if (pFD->pszTransferSendDirectory)
  445. {
  446. free(pFD->pszTransferSendDirectory);
  447. pFD->pszTransferSendDirectory = NULL;
  448. }
  449. pFD->pszTransferSendDirectory = pszTmp;
  450. }
  451. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  452. * FUNCTION:
  453. * filesSetRecvDirectory
  454. *
  455. * DESCRIPTION:
  456. * This function is called to change (maybe) the current default receiving
  457. * directory.
  458. *
  459. * PARAMETERS:
  460. * hFile -- the files and directorys handle
  461. * pszDir -- pointer to the new directory path
  462. *
  463. * RETURNS:
  464. * Nothing.
  465. *
  466. */
  467. VOID filesSetRecvDirectory(HFILES hFile, LPCTSTR pszDir)
  468. {
  469. LPTSTR pszTmp;
  470. FD_DATA *pFD;
  471. int pszTmpLen;
  472. pFD = (FD_DATA *)hFile;
  473. assert(pFD);
  474. pszTmpLen = StrCharGetByteCount(pszDir) + 1;
  475. pszTmp = (LPTSTR)malloc(pszTmpLen);
  476. assert(pszTmp);
  477. if (pszTmp == NULL)
  478. return;
  479. StrCharCopyN(pszTmp, pszDir, pszTmpLen);
  480. if (pFD->pszTransferRecvDirectory)
  481. {
  482. free(pFD->pszTransferRecvDirectory);
  483. pFD->pszTransferRecvDirectory = NULL;
  484. }
  485. pFD->pszTransferRecvDirectory = pszTmp;
  486. }
  487. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  488. * FUNCTION:
  489. * fileBuildFileList
  490. *
  491. * DESCRIPTION:
  492. * This function is called to build a list of all the files that match
  493. * a given mask.
  494. *
  495. * PARAMETERS:
  496. * pData -- pointer to where to store the pointer to the data block
  497. * pCnt -- pointer to where the item count is returned
  498. * pszName -- file name mask used to build list
  499. * nSubdir -- if TRUE, search subdirectorys
  500. * pszDirectory -- directory to start search from
  501. *
  502. * RETURNS:
  503. * ZERO if everything is OK, otherwise an error code
  504. *
  505. */
  506. int fileBuildFileList(void **pData,
  507. int *pCnt,
  508. LPCTSTR pszName,
  509. int nSubdir,
  510. LPCTSTR pszDirectory)
  511. {
  512. int nRet = 0;
  513. void *pLocalData = NULL;
  514. int nLocalCnt = 0;
  515. LPTSTR pszStr;
  516. LPTSTR *pszArray;
  517. TCHAR pszLocalDirectory[FNAME_LEN];
  518. /* Make sure the directory string terminates correctly */
  519. StrCharCopyN(pszLocalDirectory, pszDirectory, FNAME_LEN);
  520. pszStr = StrCharLast(pszLocalDirectory);
  521. if (*pszStr != TEXT('\\'))
  522. {
  523. /* Make sure the last character is a "\" */
  524. StrCharCat(pszStr, TEXT("\\"));
  525. }
  526. pLocalData = malloc(sizeof(LPTSTR) * FM_CHUNK_SIZE);
  527. if (pLocalData == NULL)
  528. nRet = FM_ERR_NO_MEM;
  529. if (nRet == 0)
  530. {
  531. nRet = fmBFLinternal(&pLocalData,
  532. &nLocalCnt,
  533. pszName,
  534. nSubdir,
  535. pszLocalDirectory);
  536. }
  537. if (nRet == 0)
  538. {
  539. /* OK, no problem */
  540. *pData = pLocalData;
  541. *pCnt = nLocalCnt;
  542. }
  543. else
  544. {
  545. /* Error, clean up first and then go away */
  546. if (pLocalData)
  547. {
  548. pszArray = (LPTSTR *)pLocalData;
  549. while (--nLocalCnt >= 0)
  550. {
  551. free(*pszArray++);
  552. *pszArray = NULL;
  553. }
  554. free(pLocalData);
  555. pLocalData = NULL;
  556. }
  557. }
  558. return nRet;
  559. }
  560. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  561. * FUNCTION:
  562. * fmBFLinternal
  563. *
  564. * DESCRIPTION:
  565. * This is the internal function that the previous function calls to do the
  566. * actual work.
  567. *
  568. * PARAMETERS:
  569. * The same as above.
  570. *
  571. * RETURNS:
  572. * ZERO if everything is OK, otherwise an error code
  573. *
  574. */
  575. STATIC_FUNC int fmBFLinternal(void **pData,
  576. int *pCnt,
  577. LPCTSTR pszName,
  578. int nSubdir,
  579. LPCTSTR pszDirectory)
  580. {
  581. int nRet = 0;
  582. int nSize;
  583. WIN32_FIND_DATA stF;
  584. HANDLE sH = INVALID_HANDLE_VALUE;
  585. LPTSTR pszBuildName;
  586. LPTSTR pszStr;
  587. LPTSTR *pszArray;
  588. pszBuildName = (LPTSTR)malloc(FNAME_LEN * sizeof(TCHAR));
  589. if (pszBuildName == NULL)
  590. {
  591. nRet = FM_ERR_NO_MEM;
  592. goto fmBFLexit;
  593. }
  594. StrCharCopyN(pszBuildName, pszDirectory, FNAME_LEN);
  595. StrCharCat(pszBuildName, pszName);
  596. sH = FindFirstFile(pszBuildName, &stF);
  597. if (sH != INVALID_HANDLE_VALUE)
  598. {
  599. /* Handle is OK, we have something to work on */
  600. do {
  601. /* Is it a directory ? If it is, skip it until later */
  602. if (stF.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  603. continue;
  604. /* Must be a file. */
  605. if (stF.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
  606. continue;
  607. if (stF.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM)
  608. continue;
  609. /* Add the file to the list */
  610. if ((*pCnt > 0) && ((*pCnt % FM_CHUNK_SIZE) == 0))
  611. {
  612. TCHAR* pTemppData = *pData;
  613. /* realloc the chunk */
  614. nSize = *pCnt + FM_CHUNK_SIZE;
  615. pTemppData = (TCHAR*)realloc(*pData, nSize * sizeof(LPTSTR) );
  616. if (pTemppData == NULL)
  617. {
  618. nRet = FM_ERR_NO_MEM;
  619. goto fmBFLexit;
  620. }
  621. else
  622. {
  623. *pData = pTemppData;
  624. }
  625. }
  626. nSize = StrCharGetByteCount(pszDirectory) +
  627. StrCharGetByteCount(stF.cFileName);
  628. nSize += 1;
  629. nSize *= sizeof(TCHAR);
  630. pszStr = (LPTSTR)malloc(nSize);
  631. if (pszStr == (LPTSTR)0)
  632. {
  633. nRet = FM_ERR_NO_MEM;
  634. goto fmBFLexit;
  635. }
  636. StrCharCopyN(pszStr, pszDirectory, nSize);
  637. StrCharCat(pszStr, stF.cFileName);
  638. pszArray = (LPTSTR *)*pData;
  639. pszArray[*pCnt] = pszStr;
  640. *pCnt += 1;
  641. } while (FindNextFile(sH, &stF));
  642. FindClose(sH);
  643. sH = INVALID_HANDLE_VALUE;
  644. }
  645. else
  646. {
  647. nRet = FM_ERR_BAD_HANDLE;
  648. goto fmBFLexit;
  649. }
  650. if (nSubdir)
  651. {
  652. StrCharCopyN(pszBuildName, pszDirectory, FNAME_LEN);
  653. StrCharCat(pszBuildName, TEXT("*.*")); /* This may need to change */
  654. sH = FindFirstFile(pszBuildName, &stF);
  655. if (sH != INVALID_HANDLE_VALUE)
  656. {
  657. /* Handle is OK, we have something to work on */
  658. do {
  659. /* Is it a directory ? If it is, go recursive */
  660. if (stF.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  661. {
  662. if (StrCharCmp(stF.cFileName, TEXT(".")) == 0)
  663. continue;
  664. if (StrCharCmp(stF.cFileName, TEXT("..")) == 0)
  665. continue;
  666. StrCharCopyN(pszBuildName, pszDirectory, FNAME_LEN);
  667. StrCharCat(pszBuildName, stF.cFileName);
  668. StrCharCat(pszBuildName, TEXT("\\"));
  669. fmBFLinternal(pData,
  670. pCnt,
  671. pszName,
  672. nSubdir,
  673. pszBuildName);
  674. }
  675. } while (FindNextFile(sH, &stF));
  676. FindClose(sH);
  677. sH = INVALID_HANDLE_VALUE;
  678. }
  679. else
  680. {
  681. nRet = FM_ERR_BAD_HANDLE;
  682. goto fmBFLexit;
  683. }
  684. }
  685. fmBFLexit:
  686. /* NOTE: all returns must come thru here */
  687. if (sH != INVALID_HANDLE_VALUE)
  688. FindClose(sH);
  689. if (pszBuildName)
  690. {
  691. free(pszBuildName);
  692. pszBuildName = NULL;
  693. }
  694. return nRet;
  695. }
  696. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  697. * FUNCTION:
  698. * fileFinalizeName
  699. *
  700. * DESCRIPTION:
  701. * This function takes a possibly incomplete file name and trys to convert
  702. * it to a fully qualified name, if possible, based upon the mode request.
  703. *
  704. * PARAMETERS:
  705. * hSession -- the almost universal session handle
  706. * pszOldname -- a pointer to the old name string
  707. * pszOlddir -- a pointer to an optional path
  708. * pszNewname -- a pointer to where the new string should go
  709. * nMode -- what should be done to the string, currently ignored
  710. *
  711. * RETURNS:
  712. * TRUE if a conversion was completed and copied, FALSE if nothing was copied.
  713. */
  714. int fileFinalizeName(LPTSTR pszOldname,
  715. LPTSTR pszOlddir,
  716. LPTSTR pszNewname,
  717. const size_t cb)
  718. {
  719. TCHAR *pachFile;
  720. TCHAR achCurDir[MAX_PATH];
  721. assert(cb);
  722. assert(pszNewname);
  723. assert(pszOldname);
  724. achCurDir[0] = TEXT('\0');
  725. // If we're given a directory, save the current directory and
  726. // set the current directory to the one given.
  727. //
  728. if (pszOlddir && *pszOlddir != TEXT('\0'))
  729. {
  730. if (GetCurrentDirectory(sizeof(achCurDir), achCurDir) == 0)
  731. {
  732. assert(0);
  733. return FALSE;
  734. }
  735. if (SetCurrentDirectory(pszOlddir) == FALSE)
  736. {
  737. assert(0);
  738. return FALSE;
  739. }
  740. }
  741. // This function does the correct job of building a full path
  742. // name and works with UNC names.
  743. //
  744. if (GetFullPathName(pszOldname, cb, pszNewname, &pachFile) == 0)
  745. {
  746. assert(0);
  747. return FALSE;
  748. }
  749. // Restore the current directory we saved above.
  750. //
  751. if (achCurDir[0] != TEXT('\0'))
  752. {
  753. if (SetCurrentDirectory(achCurDir) == FALSE)
  754. {
  755. assert (0);
  756. return FALSE;
  757. }
  758. }
  759. return TRUE;
  760. }
  761. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  762. *
  763. * FUNCTION:
  764. * mfFinalizeDIR
  765. *
  766. * DESCRIPTION:
  767. * This function is called to clean up a directory name. At the present time
  768. * it doesn't do very much.
  769. *
  770. * PARAMETERS:
  771. * hSession -- the almost universal session handle
  772. * pszOldname -- a pointer to the old directory name
  773. * pszNewname -- a pointer to where the new string should go
  774. *
  775. * RETURNS:
  776. * TRUE if a copy was completed, FALSE if nothing was copied.
  777. */
  778. int fileFinalizeDIR(HSESSION hSession,
  779. LPTSTR pszOldname,
  780. LPTSTR pszNewname)
  781. {
  782. LPTSTR pszPtr;
  783. LPTSTR pszFoo;
  784. StrCharCopyN(pszNewname, pszOldname, FNAME_LEN);
  785. pszPtr = StrCharNext(pszNewname);
  786. pszFoo = StrCharNext(pszPtr);
  787. if ((StrCharGetStrLength(pszNewname) == 2) &&
  788. (*pszPtr == TEXT(':')))
  789. {
  790. StrCharCat(pszNewname, TEXT("\\"));
  791. }
  792. else if ((StrCharGetStrLength(pszNewname) == 3) &&
  793. (*pszPtr == TEXT(':')) &&
  794. (*pszFoo == TEXT('\\')))
  795. {
  796. /* Do nothing */
  797. }
  798. else
  799. {
  800. pszPtr = StrCharLast(pszNewname);
  801. if (*pszPtr == TEXT('\\'))
  802. *pszPtr = TEXT('\0');
  803. }
  804. return 1;
  805. }
  806. #if defined(BMP_FROM_FILE)
  807. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  808. * FUNCTION:
  809. * fileReadBitmapFromFile
  810. *
  811. * DESCRIPTION:
  812. * This function takes a filename, opens the file and attempts to interpret
  813. * the file as a bitmap file, turning it into a bitmap.
  814. *
  815. * PARAMETERS:
  816. * hDC -- device context used to create the bitmap
  817. * pszName -- the name of the file
  818. *
  819. * RETURNS:
  820. * A bitmap handle or NULL.
  821. *
  822. */
  823. HBITMAP fileReadBitmapFromFile(HDC hDC, LPTSTR pszName, int fCmp)
  824. {
  825. HBITMAP hBmp = (HBITMAP)0;
  826. DWORD dwRead;
  827. DWORD dwSize;
  828. HANDLE hfbm;
  829. int hcbm;
  830. OFSTRUCT stOF;
  831. BITMAPFILEHEADER bmfh;
  832. BITMAPINFOHEADER bmih;
  833. BITMAPINFO *lpbmi;
  834. VOID *lpvBits;
  835. lpbmi = NULL;
  836. lpvBits = NULL;
  837. if (fCmp)
  838. {
  839. memset(&stOF, 0, sizeof(OFSTRUCT));
  840. stOF.cBytes = sizeof(OFSTRUCT);
  841. hcbm = LZOpenFile(pszName,
  842. &stOF,
  843. OF_READ);
  844. if (hcbm < 0)
  845. return hBmp;
  846. }
  847. else
  848. {
  849. hfbm = CreateFile(pszName,
  850. GENERIC_READ,
  851. FILE_SHARE_READ,
  852. NULL,
  853. OPEN_EXISTING,
  854. FILE_ATTRIBUTE_READONLY,
  855. NULL);
  856. if (hfbm == INVALID_HANDLE_VALUE)
  857. return hBmp;
  858. }
  859. /* Retrieve the BITMAPFILEHEADER structure */
  860. memset(&bmfh, 0, sizeof(BITMAPFILEHEADER));
  861. if (fCmp)
  862. {
  863. dwRead = 0;
  864. dwRead = LZRead(hcbm,
  865. (unsigned char *)&bmfh,
  866. sizeof(BITMAPFILEHEADER));
  867. /* this is necessary because of a garbage return value */
  868. dwRead = sizeof(BITMAPFILEHEADER);
  869. }
  870. else
  871. {
  872. ReadFile(hfbm,
  873. &bmfh,
  874. sizeof(BITMAPFILEHEADER),
  875. &dwRead,
  876. NULL);
  877. }
  878. if (dwRead != sizeof(BITMAPFILEHEADER))
  879. goto fError;
  880. /* Retrieve the BITMAPINFOHEADER structure */
  881. memset(&bmih, 0, sizeof(BITMAPINFOHEADER));
  882. if (fCmp)
  883. {
  884. dwRead = 0;
  885. dwRead = LZRead(hcbm,
  886. (unsigned char *)&bmih,
  887. sizeof(BITMAPINFOHEADER));
  888. /* this is necessary because of a garbage return value */
  889. dwRead = sizeof(BITMAPINFOHEADER);
  890. }
  891. else
  892. {
  893. ReadFile(hfbm,
  894. &bmih,
  895. sizeof(BITMAPINFOHEADER),
  896. &dwRead,
  897. NULL);
  898. }
  899. if (dwRead != sizeof(BITMAPINFOHEADER))
  900. goto fError;
  901. /* allocate space for the BITMAPINFO structure */
  902. dwSize = sizeof(BITMAPINFOHEADER) +
  903. ((1 << bmih.biBitCount) * sizeof(RGBQUAD));
  904. lpbmi = malloc(dwSize);
  905. if (lpbmi == NULL)
  906. goto fError;
  907. /* load BITMAPINFOHEADER into the BITMAPINFO structure */
  908. lpbmi->bmiHeader.biSize = bmih.biSize;
  909. lpbmi->bmiHeader.biWidth = bmih.biWidth;
  910. lpbmi->bmiHeader.biHeight = bmih.biHeight;
  911. lpbmi->bmiHeader.biPlanes = bmih.biPlanes;
  912. lpbmi->bmiHeader.biBitCount = bmih.biBitCount;
  913. lpbmi->bmiHeader.biCompression = bmih.biCompression;
  914. lpbmi->bmiHeader.biSizeImage = bmih.biSizeImage;
  915. lpbmi->bmiHeader.biXPelsPerMeter = bmih.biXPelsPerMeter;
  916. lpbmi->bmiHeader.biYPelsPerMeter = bmih.biYPelsPerMeter;
  917. lpbmi->bmiHeader.biClrUsed = bmih.biClrUsed;
  918. lpbmi->bmiHeader.biClrImportant = bmih.biClrImportant;
  919. /* read the color table */
  920. dwSize = (1 << bmih.biBitCount) * sizeof(RGBQUAD);
  921. if (fCmp)
  922. {
  923. dwRead = 0;
  924. dwRead = LZRead(hcbm,
  925. (unsigned char *)lpbmi->bmiColors,
  926. dwSize);
  927. /* this is necessary because of a garbage return value */
  928. dwRead = dwSize;
  929. }
  930. else
  931. {
  932. ReadFile(hfbm,
  933. lpbmi->bmiColors,
  934. dwSize,
  935. &dwRead,
  936. NULL);
  937. }
  938. if (dwSize != dwRead)
  939. goto fError;
  940. /* allocate memory for the bitmap data */
  941. dwSize = bmfh.bfSize - bmfh.bfOffBits;
  942. lpvBits = malloc(dwSize);
  943. if (lpvBits == NULL)
  944. goto fError;
  945. /* read in the bitmap data */
  946. if (fCmp)
  947. {
  948. dwRead = 0;
  949. dwRead = LZRead(hcbm,
  950. lpvBits,
  951. dwSize);
  952. /* this is necessary because of a garbage return value */
  953. dwRead = dwSize;
  954. }
  955. else
  956. {
  957. ReadFile(hfbm,
  958. lpvBits,
  959. dwSize,
  960. &dwRead,
  961. NULL);
  962. }
  963. if (dwSize != dwRead)
  964. goto fError;
  965. /* create the bitmap handle */
  966. hBmp = CreateDIBitmap(hDC,
  967. &bmih,
  968. CBM_INIT,
  969. lpvBits,
  970. lpbmi,
  971. DIB_RGB_COLORS);
  972. /* either it worked or it didn't */
  973. fError:
  974. /* Clean up everything here */
  975. if (lpbmi != NULL)
  976. {
  977. free(lpbmi);
  978. lpbmi = NULL;
  979. }
  980. if (lpvBits != NULL)
  981. {
  982. free(lpvBits);
  983. lpvBits = NULL;
  984. }
  985. if (fCmp)
  986. {
  987. LZClose(hcbm);
  988. }
  989. else
  990. {
  991. CloseHandle(hfbm);
  992. }
  993. return hBmp;
  994. }
  995. #endif
  996. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  997. * FUNCTION: GetFileSizeFromName
  998. *
  999. * DESCRIPTION:
  1000. * Returns the size of a named file. (The GetFileSize Win32 API call
  1001. * requires the file to be open, this doesn't).
  1002. * Note: the WIN32 API is structured to support 64 bits file size values
  1003. * so this may need to be updated at some point.
  1004. *
  1005. * PARAMETERS:
  1006. * pszName -- the name of the file.
  1007. * pulFileSize -- Pointer to the var. that receives the file size.
  1008. * (If NULL, this function can be used to test for the
  1009. * existense of a file).
  1010. *
  1011. * RETURNS:
  1012. * TRUE if file is found, FALSE if not
  1013. */
  1014. int GetFileSizeFromName(TCHAR *pszName, unsigned long * const pulFileSize)
  1015. {
  1016. WIN32_FIND_DATA stFData;
  1017. HANDLE hFind;
  1018. int fReturnValue = FALSE;
  1019. hFind = FindFirstFile(pszName, &stFData);
  1020. if (hFind != INVALID_HANDLE_VALUE)
  1021. {
  1022. DWORD dwMask;
  1023. /* This is just a guess. If you need to change it, do so. */
  1024. dwMask = FILE_ATTRIBUTE_DIRECTORY |
  1025. FILE_ATTRIBUTE_HIDDEN |
  1026. FILE_ATTRIBUTE_SYSTEM;
  1027. if ((stFData.dwFileAttributes & dwMask) == 0)
  1028. {
  1029. fReturnValue = TRUE;
  1030. // Strictly speaking, file sizes can now be 64 bits.
  1031. assert(stFData.nFileSizeHigh == 0);
  1032. if (pulFileSize)
  1033. *pulFileSize = (unsigned long)stFData.nFileSizeLow;
  1034. }
  1035. FindClose(hFind);
  1036. }
  1037. return fReturnValue;
  1038. }
  1039. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  1040. * FUNCTION:
  1041. * SetFileSize
  1042. *
  1043. * DESCRIPTION:
  1044. *
  1045. * PARAMETERS:
  1046. *
  1047. * RETURNS:
  1048. */
  1049. int SetFileSize(const TCHAR *pszName, unsigned long ulFileSize)
  1050. {
  1051. HANDLE hFile;
  1052. int nRet = -1;
  1053. /* Yes, we need to open the file */
  1054. hFile = CreateFile(pszName,
  1055. GENERIC_WRITE,
  1056. FILE_SHARE_WRITE,
  1057. 0,
  1058. OPEN_EXISTING,
  1059. 0,
  1060. 0);
  1061. if (hFile == INVALID_HANDLE_VALUE)
  1062. return -1; /* No such file */
  1063. if (SetFilePointer(hFile, ulFileSize, NULL, FILE_BEGIN) == ulFileSize)
  1064. {
  1065. if (SetEndOfFile(hFile))
  1066. nRet = 0;
  1067. }
  1068. CloseHandle(hFile);
  1069. return nRet;
  1070. }
  1071. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  1072. * FUNCTION:
  1073. * ValidateFileName
  1074. *
  1075. * DESCRIPTION:
  1076. * Determine whether a file pathname is valid by attempting to open it.
  1077. *
  1078. * PARAMETERS:
  1079. *
  1080. * LPSTR pszName - the name/pathname
  1081. *
  1082. * RETURNS:
  1083. *
  1084. * 0, if a file with the specified name could not be opened/created,
  1085. * 1, if it could.
  1086. *
  1087. */
  1088. int ValidateFileName(LPSTR pszName)
  1089. {
  1090. HANDLE hfile = 0;
  1091. if (GetFileSizeFromName(pszName, NULL))
  1092. {
  1093. hfile = CreateFile(pszName,
  1094. GENERIC_READ,
  1095. FILE_SHARE_WRITE,
  1096. NULL,
  1097. OPEN_EXISTING,
  1098. 0,
  1099. NULL);
  1100. }
  1101. else
  1102. {
  1103. hfile = CreateFile(pszName,
  1104. GENERIC_READ,
  1105. FILE_SHARE_READ,
  1106. NULL,
  1107. CREATE_NEW,
  1108. FILE_FLAG_DELETE_ON_CLOSE,
  1109. NULL);
  1110. }
  1111. if(hfile != INVALID_HANDLE_VALUE)
  1112. {
  1113. CloseHandle(hfile);
  1114. return(1);
  1115. }
  1116. else
  1117. {
  1118. return(0);
  1119. }
  1120. }
  1121. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  1122. * FUNCTION:
  1123. *
  1124. * DESCRIPTION:
  1125. *
  1126. * PARAMETERS:
  1127. *
  1128. * RETURNS:
  1129. *
  1130. */