fileutil.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. #include "precompile.h"
  2. #include <wchar.h>
  3. #include <errno.h>
  4. #include <assert.h>
  5. #ifdef _WIN32
  6. #include <mbstring.h>
  7. #include <limits.h>
  8. #include <ShellAPI.h>
  9. #else
  10. #include <winpr/wtypes.h>
  11. #include <winpr/error.h>
  12. #include <winpr/file.h>
  13. #include <libgen.h>
  14. #include <sys/types.h>
  15. #include <dirent.h>
  16. #include <sys/stat.h>
  17. #include <fcntl.h>
  18. #endif
  19. #include "array.h"
  20. #include "fileutil.h"
  21. #include "strutil.h"
  22. #include "memutil.h"
  23. #include "dbgutil.h"
  24. #include <winpr/wlog.h>
  25. #define TAG TOOLKIT_TAG("fileutil")
  26. TOOLKIT_API BOOL ExistsDirA(LPCSTR lpDirPath)
  27. {
  28. DWORD dwRet = GetFileAttributesA(lpDirPath);
  29. return (dwRet != INVALID_FILE_ATTRIBUTES) && (dwRet & FILE_ATTRIBUTE_DIRECTORY);
  30. }
  31. TOOLKIT_API BOOL ExistsFileA(LPCSTR lpFilePath)
  32. {
  33. DWORD dwRet = GetFileAttributesA(lpFilePath);
  34. return (dwRet != INVALID_FILE_ATTRIBUTES) && !(dwRet & FILE_ATTRIBUTE_DIRECTORY);
  35. }
  36. TOOLKIT_API DWORD ReadFileSize(LPCSTR pszFile)
  37. {
  38. DWORD fileSize = (DWORD)-1;
  39. #ifndef _MSC_VER
  40. struct stat statbuf;
  41. memset(&statbuf, 0, sizeof(statbuf));
  42. if (stat(pszFile, &statbuf) >= 0) {
  43. fileSize = statbuf.st_size;
  44. }
  45. #else
  46. HANDLE hFile;
  47. hFile = CreateFile(pszFile, 0, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  48. if (hFile != INVALID_HANDLE_VALUE) {
  49. fileSize = GetFileSize(hFile, NULL);
  50. CloseHandle(hFile);
  51. }
  52. #endif
  53. return fileSize;
  54. }
  55. TOOLKIT_API void SureUnixFileAttributeAccessable(LPCSTR pszFile)
  56. {
  57. #ifndef _MSC_VER
  58. mode_t f_attrib = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH | S_IWOTH;
  59. chmod(pszFile, f_attrib);
  60. #endif
  61. }
  62. TOOLKIT_API BOOL CreateDirA(LPCSTR lpDirPath, BOOL bRecursive)
  63. {
  64. char* slashPos = NULL;
  65. char* backSlashPos = NULL;
  66. size_t len = strlen(lpDirPath);
  67. if (len <= 2 || len >= MAX_PATH-2) {
  68. return FALSE;
  69. } else {
  70. if (!bRecursive) {
  71. if (ExistsDirA(lpDirPath))
  72. return TRUE;
  73. return CreateDirectoryA(lpDirPath, NULL);
  74. }
  75. else {
  76. CHAR tmp[MAX_PATH], * p;
  77. CHAR* q = NULL;
  78. q = p = &tmp[0];
  79. strncpy(tmp, lpDirPath, MAX_PATH);
  80. tmp[MAX_PATH - 1] = 0;
  81. if (tmp[len - 1] != BACK_SLASH && tmp[len - 1] != SLASH) {
  82. tmp[len++] = SPLIT_SLASH;
  83. tmp[len] = 0;
  84. }
  85. #ifdef _WIN32
  86. do {
  87. slashPos = strchr(p, SLASH);
  88. backSlashPos = strchr(p, BACK_SLASH);
  89. if (slashPos != NULL && backSlashPos != NULL) {
  90. p = slashPos < backSlashPos ? slashPos : backSlashPos;
  91. }
  92. else if (slashPos != NULL) {
  93. p = slashPos;
  94. }
  95. else {
  96. p = backSlashPos;
  97. }
  98. if (!p) {
  99. break;
  100. }
  101. *p = 0;
  102. if (!ExistsDirA(tmp)) {
  103. if (!CreateDirectoryA(tmp, NULL) && GetLastError() != ERROR_ALREADY_EXISTS)
  104. return FALSE;
  105. }
  106. *p = SPLIT_SLASH;
  107. p++;
  108. }
  109. while (1);
  110. #else
  111. while ((p = strchr(p, SPLIT_SLASH)) != NULL) {
  112. if(p != q) {//linux compatibility.
  113. *p = 0;
  114. if (!ExistsDirA(tmp)) {
  115. if (!CreateDirectoryA(tmp, NULL) && GetLastError() != ERROR_ALREADY_EXISTS)
  116. return FALSE;
  117. }
  118. *p = SPLIT_SLASH;
  119. }
  120. p++;
  121. q = p;
  122. }
  123. #endif
  124. }
  125. }
  126. return TRUE;
  127. }
  128. TOOLKIT_API BOOL CreateDirRecursiveA(LPCSTR lpDirPath) { return CreateDirA(lpDirPath, TRUE); }
  129. TOOLKIT_API BOOL CreateParentDirA(LPCSTR lpPath, BOOL bRecursive)
  130. {
  131. CHAR pdir[MAX_PATH];
  132. const CHAR *p;
  133. p = strrchr(lpPath, '\\');
  134. if (!p) {
  135. p = strrchr(lpPath, '/');
  136. }
  137. if (!p) {
  138. return FALSE;
  139. }
  140. strncpy(pdir, lpPath, p-lpPath);
  141. pdir[p-lpPath] = 0;
  142. return CreateDirA(pdir, bRecursive);
  143. }
  144. TOOLKIT_API BOOL ClearDirRecursiveA(LPCSTR lpDirPath)
  145. {
  146. array_header_t *arr;
  147. BOOL bRet = TRUE;
  148. TOOLKIT_ASSERT(lpDirPath);
  149. arr = fileutil_get_sub_files_a(lpDirPath);
  150. if (arr) {
  151. int i;
  152. for (i = 0; i < arr->nelts; ++i) {
  153. char *tmp = ARRAY_IDX(arr, i, char*);
  154. bRet &= DeleteFileA(tmp);
  155. }
  156. toolkit_array_free2(arr);
  157. }
  158. arr = fileutil_get_sub_dirs_a(lpDirPath);
  159. if (arr) {
  160. int i;
  161. for (i = 0; i < arr->nelts; ++i) {
  162. char *tmp = ARRAY_IDX(arr, i, char*);
  163. bRet &= ClearDirRecursiveA(tmp);
  164. }
  165. toolkit_array_free2(arr);
  166. }
  167. return bRet;
  168. }
  169. TOOLKIT_API BOOL RemoveFileA(LPCSTR pszFile)
  170. {
  171. if(ExistsFileA(pszFile))
  172. {
  173. // remove readonly attribute
  174. DWORD dwRet = GetFileAttributesA(pszFile);
  175. if (dwRet & FILE_ATTRIBUTE_READONLY)
  176. {
  177. dwRet &= (~FILE_ATTRIBUTE_READONLY);
  178. SetFileAttributesA(pszFile, dwRet);
  179. }
  180. // delete it
  181. return DeleteFileA(pszFile);
  182. }
  183. return FALSE;
  184. }
  185. // 移除文件只读属性
  186. TOOLKIT_API BOOL RemoveFileReadOnlyAttributeA(LPCSTR pszFile)
  187. {
  188. if(ExistsFileA(pszFile))
  189. {
  190. // remove readonly attribute
  191. DWORD dwRet = GetFileAttributesA(pszFile);
  192. if (dwRet & FILE_ATTRIBUTE_READONLY)
  193. {
  194. dwRet &= (~FILE_ATTRIBUTE_READONLY);
  195. SetFileAttributesA(pszFile, dwRet);
  196. }
  197. return TRUE;
  198. }
  199. return FALSE;
  200. }
  201. TOOLKIT_API BOOL IsDirectory(LPCSTR pszDir)
  202. {
  203. char szCurPath[MAX_PATH * 2] = { 0 };
  204. #ifdef _WIN32
  205. HANDLE hFile;
  206. WIN32_FIND_DATAA FindFileData = { 0 };
  207. _snprintf(szCurPath, MAX_PATH * 2, "%s//*", pszDir);
  208. hFile = FindFirstFileA(szCurPath, &FindFileData); /**< find first file by given path. */
  209. if (hFile == INVALID_HANDLE_VALUE)
  210. {
  211. return FALSE; /** 如果不能找到第一个文件,那么没有目录 */
  212. }
  213. else
  214. {
  215. FindClose(hFile);
  216. return TRUE;
  217. }
  218. #else
  219. struct stat buf;
  220. snprintf(szCurPath, MAX_PATH * 2, "%s//*", pszDir);
  221. if (0 == stat(szCurPath, &buf))
  222. {
  223. return S_ISDIR(buf.st_mode);
  224. }
  225. return FALSE;
  226. #endif
  227. }
  228. // 递归拷贝所有文件及子文件夹
  229. TOOLKIT_API BOOL CopyDirA(LPCSTR pszSourceDir, LPCSTR pszDestDir)
  230. {
  231. array_header_t *arr;
  232. BOOL bRet = TRUE;
  233. if (!ExistsDirA(pszSourceDir))
  234. return FALSE;
  235. if (!ExistsDirA(pszDestDir))
  236. {
  237. if (!CreateDirRecursiveA(pszDestDir))
  238. return FALSE;
  239. }
  240. arr = fileutil_get_sub_files_a(pszSourceDir);
  241. if (arr)
  242. {
  243. int i;
  244. for (i = 0; i < arr->nelts; ++i)
  245. {
  246. char szDestFile[256] = {0};
  247. char *file = ARRAY_IDX(arr, i, char*);
  248. strcpy(szDestFile, pszDestDir);
  249. if (szDestFile[strlen(szDestFile)-1] != '\\' && szDestFile[strlen(szDestFile) - 1] != '/')
  250. strcat(szDestFile, MB_SPLIT_SLASH_STR);
  251. strcat(szDestFile, strrchr(file, SPLIT_SLASH)+1);
  252. bRet &= CopyFileA(file, szDestFile, FALSE);
  253. }
  254. toolkit_array_free2(arr);
  255. }
  256. arr = fileutil_get_sub_dirs_a(pszSourceDir);
  257. if (arr)
  258. {
  259. int i;
  260. for (i = 0; i < arr->nelts; ++i)
  261. {
  262. char szDestSubDir[256] = {0};
  263. char *dir = ARRAY_IDX(arr, i, char*);
  264. strcpy(szDestSubDir, pszDestDir);
  265. if (szDestSubDir[strlen(szDestSubDir)-1] != '\\' && szDestSubDir[strlen(szDestSubDir) - 1] != '/')
  266. strcat(szDestSubDir, MB_SPLIT_SLASH_STR);
  267. strcat(szDestSubDir, strrchr(dir, SPLIT_SLASH)+1);
  268. bRet &= CopyDirA(dir, szDestSubDir);
  269. }
  270. toolkit_array_free2(arr);
  271. }
  272. return bRet;
  273. }
  274. TOOLKIT_API BOOL RemoveDirRecursiveA(LPCSTR lpDirPath)
  275. {
  276. #ifdef _WIN32
  277. int nRet = 0;
  278. char szDir[256] = {0};
  279. SHFILEOPSTRUCT fo;
  280. memset(&fo, 0, sizeof(fo));
  281. memcpy(szDir, lpDirPath, strlen(lpDirPath));
  282. fo.wFunc = FO_DELETE;
  283. fo.pFrom = szDir;
  284. fo.fFlags = FOF_NO_UI;
  285. nRet = SHFileOperationA(&fo);
  286. return nRet ==0 ? TRUE: FALSE;
  287. #else
  288. array_header_t* arr;
  289. BOOL bRet = TRUE;
  290. TOOLKIT_ASSERT(lpDirPath);
  291. arr = fileutil_get_sub_files_a(lpDirPath);
  292. if (arr) {
  293. int i;
  294. for (i = 0; i < arr->nelts; ++i) {
  295. char* tmp = ARRAY_IDX(arr, i, char*);
  296. bRet &= DeleteFileA(tmp);
  297. }
  298. toolkit_array_free2(arr);
  299. }
  300. arr = fileutil_get_sub_dirs_a(lpDirPath);
  301. if (arr) {
  302. int i;
  303. for (i = 0; i < arr->nelts; ++i) {
  304. char* tmp = ARRAY_IDX(arr, i, char*);
  305. bRet &= RemoveDirRecursiveA(tmp);
  306. }
  307. toolkit_array_free2(arr);
  308. }
  309. if (bRet) {
  310. bRet = RemoveDirectoryA(lpDirPath);
  311. }
  312. return bRet;
  313. #endif
  314. }
  315. TOOLKIT_API HANDLE ExtCreateFileA(LPCSTR lpFileName,
  316. DWORD dwDesiredAccess,
  317. DWORD dwShareMode,
  318. LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  319. DWORD dwCreationDisposition,
  320. DWORD dwFlagsAndAttributes,
  321. HANDLE hTemplateFile)
  322. {
  323. if (!lpFileName)
  324. return INVALID_HANDLE_VALUE;
  325. if (lpFileName[0] != '\\')
  326. CreateParentDirA(lpFileName, TRUE);
  327. return CreateFileA(lpFileName,
  328. dwDesiredAccess,
  329. dwShareMode,
  330. lpSecurityAttributes,
  331. dwCreationDisposition,
  332. dwFlagsAndAttributes,
  333. hTemplateFile);
  334. }
  335. TOOLKIT_API int fileutil_copy_file(const char* dest_file_path, const char* src_file_path)
  336. {
  337. if (CopyFileA(src_file_path, dest_file_path, FALSE)) {
  338. return 0;
  339. }
  340. return -1;
  341. }
  342. TOOLKIT_API void fileutil_delete_file(const char* filename)
  343. {
  344. #ifdef _WIN32
  345. DeleteFileA(filename);
  346. #else
  347. unlink(filename);
  348. #endif //_WIN32
  349. }
  350. /*
  351. * isfile: only fetch file type elem or not.
  352. * limitation: max elem count to satsfy.
  353. */
  354. static array_header_t *fileutil_get_sub_a(const char *path, int isfile, int limitation)
  355. {
  356. #if 1
  357. array_header_t* arr = NULL;
  358. char tmp[MAX_PATH];
  359. int npath;
  360. if (!path)
  361. return NULL;
  362. npath = strlen(path);
  363. if (npath < 2)
  364. return NULL;
  365. strcpy(tmp, path);
  366. if (tmp[npath - 1] != '\\' && tmp[npath - 1] != '/') {
  367. tmp[npath++] = SPLIT_SLASH;
  368. tmp[npath] = 0;
  369. }
  370. tmp[npath++] = '*';
  371. tmp[npath] = 0;
  372. arr = array_make(3, sizeof(char*));
  373. {
  374. WIN32_FIND_DATAA fd;
  375. HANDLE hFind = FindFirstFileA(tmp, &fd);
  376. if (hFind != INVALID_HANDLE_VALUE) {
  377. tmp[--npath] = 0;
  378. do {
  379. int isdir = !!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
  380. if (isdir) {
  381. if (strcmp(fd.cFileName, ".") == 0 || strcmp(fd.cFileName, "..") == 0) {
  382. continue;
  383. }
  384. }
  385. /*[if require file while is not dir] or [not require file but is dir]*/
  386. if (isfile ^ isdir) {
  387. char* v = malloc(sizeof(char) * (npath + strlen(fd.cFileName) + 1));
  388. strcpy(v, tmp);
  389. strcat(v, fd.cFileName);
  390. ARRAY_PUSH(arr, char*) = v;
  391. }
  392. } while (FindNextFileA(hFind, &fd) && arr->nelts < limitation);
  393. FindClose(hFind);
  394. }
  395. }
  396. #else
  397. array_header_t* arr = NULL;
  398. char tmp[MAX_PATH];
  399. char tmp2[MAX_PATH];
  400. DIR* d = NULL;
  401. struct dirent* dp = NULL;
  402. struct stat st;
  403. int npath;
  404. if (!path)
  405. return NULL;
  406. npath = strlen(path);
  407. if (npath < 1)
  408. return NULL;
  409. strcpy(tmp, path);
  410. if (tmp[npath - 1] == '/') {
  411. tmp[npath - 1] = '\0';
  412. }
  413. if (stat(tmp, &st) < 0 || !S_ISDIR(st.st_mode)) {
  414. return NULL;
  415. }
  416. if (!(d = opendir(tmp))) {
  417. return NULL;
  418. }
  419. if (tmp[npath - 1] != '/') {
  420. tmp[npath++] = '/';
  421. tmp[npath] = 0;
  422. }
  423. arr = array_make(3, sizeof(char*));
  424. while ((dp = readdir(d)) != NULL && arr->nelts < limitation)
  425. {
  426. if((!strncmp(dp->d_name, ".", 1)) || (!strncmp(dp->d_name, "..", 2))) {
  427. continue;
  428. }
  429. snprintf(tmp2, sizeof(tmp2) - 1, "%s%s", tmp, dp->d_name);
  430. stat(tmp2, &st);
  431. int isdir = !!(S_ISDIR(st.st_mode));
  432. if (isfile ^ isdir) {
  433. char* v = malloc(sizeof(char) * (npath + strlen(dp->d_name) + 1));
  434. strcpy(v, tmp);
  435. strcat(v, dp->d_name);
  436. ARRAY_PUSH(arr, char*) = v;
  437. }
  438. }
  439. closedir(d);
  440. #endif //1
  441. return arr;
  442. }
  443. TOOLKIT_API array_header_t *fileutil_get_sub_files_a(const char *path)
  444. {
  445. return fileutil_get_sub_a(path, 1, INT_MAX);
  446. }
  447. TOOLKIT_API array_header_t *fileutil_get_sub_dirs_a(const char *path)
  448. {
  449. return fileutil_get_sub_a(path, 0, INT_MAX);
  450. }
  451. TOOLKIT_API array_header_t *fileutil_get_sub_files2_a(const char *path, int limitation)
  452. {
  453. return fileutil_get_sub_a(path, 1, limitation);
  454. }
  455. TOOLKIT_API array_header_t *fileutil_get_sub_dirs2_a(const char *path, int limitation)
  456. {
  457. return fileutil_get_sub_a(path, 0, limitation);
  458. }
  459. TOOLKIT_API FILE *fileutil_transaction_fopen(const char *filename, const char *mode)
  460. {
  461. DWORD dwAttr;
  462. char t[MAX_PATH];
  463. int write_flag;
  464. if (!filename || !mode) {
  465. errno = EINVAL;
  466. return NULL;
  467. }
  468. write_flag = (int)strchr(mode, 'w');
  469. strcpy(t, filename);
  470. strcat(t, ".bak");
  471. dwAttr = GetFileAttributesA(filename);
  472. if (dwAttr != INVALID_FILE_ATTRIBUTES && dwAttr & FILE_ATTRIBUTE_DIRECTORY) {
  473. errno = EIO;
  474. return NULL;
  475. }
  476. if (dwAttr != INVALID_FILE_ATTRIBUTES) {
  477. if (!(dwAttr & FILE_ATTRIBUTE_READONLY)) {
  478. BOOL bRet = DeleteFileA(filename);
  479. if (bRet) {
  480. if (!write_flag) {
  481. if (ExistsFileA(t)) {
  482. bRet = CopyFileA(t, filename, FALSE);
  483. if (bRet)
  484. bRet = SetFileAttributesA(filename, FILE_ATTRIBUTE_NORMAL);
  485. }
  486. }
  487. }
  488. if (!bRet) {
  489. errno = EIO;
  490. return NULL;
  491. }
  492. } else {
  493. if (write_flag) {
  494. BOOL bRet = TRUE;
  495. if (ExistsFileA(t)) {
  496. bRet = SetFileAttributesA(t, dwAttr & ~FILE_ATTRIBUTE_READONLY);
  497. }
  498. if (bRet) {
  499. bRet = CopyFileA(filename, t, FALSE);
  500. }
  501. if (!bRet) {
  502. errno = EIO;
  503. return NULL;
  504. }
  505. }
  506. }
  507. } else {
  508. if (!write_flag) {
  509. BOOL bRet = TRUE;
  510. if (ExistsFileA(t)) {
  511. bRet = CopyFileA(t, filename, FALSE);
  512. } else {
  513. bRet = FALSE;
  514. }
  515. if (!bRet) {
  516. errno = EIO;
  517. return NULL;
  518. }
  519. }
  520. }
  521. return fopen(filename, mode);
  522. }
  523. TOOLKIT_API int fileutil_transaction_fclose(const char *filename, FILE *fp)
  524. {
  525. int rc = fclose(fp);
  526. if (!rc && filename) {
  527. DWORD dwAttr = GetFileAttributesA(filename);
  528. if (dwAttr != INVALID_FILE_ATTRIBUTES) {
  529. if (!(dwAttr & FILE_ATTRIBUTE_READONLY))
  530. SetFileAttributesA(filename, dwAttr | FILE_ATTRIBUTE_READONLY);
  531. }
  532. }
  533. return rc;
  534. }
  535. #ifndef _WIN32
  536. void _splitpath(const char* path, char* drive, char* dir, char* fname, char* ext)
  537. {
  538. char resolved_path[4096];
  539. char* p;
  540. char* q;
  541. char* last_slash = NULL;
  542. char* dot = NULL;
  543. size_t len, sub_len;
  544. if (drive) {
  545. *drive = '\0';
  546. }
  547. if (path == NULL) {
  548. if (dir) { *dir = '\0'; }
  549. if (fname) { *fname = '\0'; }
  550. if (ext) { *ext = '\0'; }
  551. return;
  552. }
  553. if (realpath(path, resolved_path) == NULL) {
  554. WLog_ERR(TAG, "realpath failed: %s", strerror(errno));
  555. strcpy(resolved_path, path);
  556. }
  557. else {
  558. //WLog_DBG(TAG, "resolved_path: %s", resolved_path);
  559. }
  560. len = strlen(resolved_path);
  561. if (resolved_path[0] == '/') {
  562. p = strchr(resolved_path + 1, '/');
  563. if (p != NULL) {
  564. *p = '\0';
  565. }
  566. if (drive) {
  567. strcpy(drive, resolved_path);
  568. }
  569. if (p != NULL) {
  570. *p = '/';
  571. }
  572. else {
  573. p = resolved_path + len;
  574. }
  575. } else {
  576. p = resolved_path;
  577. }
  578. q = p;
  579. for (last_slash = NULL; p && *p != '\0'; ++p) {
  580. if (*p == '/') {
  581. last_slash = p + 1;
  582. if (dot) {
  583. dot = NULL;
  584. }
  585. }
  586. else if(*p == '.') {
  587. dot = p;
  588. }
  589. }
  590. if (last_slash) {
  591. if (dir) {
  592. sub_len = last_slash - q;
  593. strncpy(dir, q, sub_len);
  594. *(dir + sub_len) = '\0';
  595. }
  596. q = last_slash;
  597. }
  598. else if(dir){
  599. *dir = '\0';
  600. }
  601. if (dot) {
  602. if (fname) {
  603. sub_len = dot - q;
  604. strncpy(fname, q, sub_len);
  605. *(fname + sub_len) = '\0';
  606. }
  607. if (ext) {
  608. sub_len = p - dot;
  609. strncpy(ext, dot, sub_len);
  610. *(ext + sub_len) = '\0';
  611. }
  612. }
  613. else {
  614. if (fname) {
  615. sub_len = p - q;
  616. strncpy(fname, q, sub_len);
  617. *(fname + sub_len) = '\0';
  618. }
  619. if (ext) {
  620. *ext = '\0';
  621. }
  622. }
  623. }
  624. #endif //NOT _WIN32