🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
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.

633 lines
16 KiB

  1. /*
  2. * A test program to make sure that dirent works correctly.
  3. *
  4. * Copyright (C) 1998-2019 Toni Ronkko
  5. * This file is part of dirent. Dirent may be freely distributed
  6. * under the MIT license. For all details and documentation, see
  7. * https://github.com/tronkko/dirent
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #ifdef _MSC_VER
  13. # include <direct.h>
  14. # define chdir(x) _chdir(x)
  15. #else
  16. # include <unistd.h>
  17. #endif
  18. #include <sys/stat.h>
  19. #include <dirent.h>
  20. #include <errno.h>
  21. #undef NDEBUG
  22. #include <assert.h>
  23. int
  24. main(
  25. int argc, char *argv[])
  26. {
  27. (void) argc;
  28. (void) argv;
  29. /* File type macros */
  30. {
  31. assert (DTTOIF(DT_REG) == S_IFREG);
  32. assert (DTTOIF(DT_DIR) == S_IFDIR);
  33. assert (DTTOIF(DT_FIFO) == S_IFIFO);
  34. assert (DTTOIF(DT_SOCK) == S_IFSOCK);
  35. assert (DTTOIF(DT_CHR) == S_IFCHR);
  36. assert (DTTOIF(DT_BLK) == S_IFBLK);
  37. assert (IFTODT(S_IFREG) == DT_REG);
  38. assert (IFTODT(S_IFDIR) == DT_DIR);
  39. assert (IFTODT(S_IFIFO) == DT_FIFO);
  40. assert (IFTODT(S_IFSOCK) == DT_SOCK);
  41. assert (IFTODT(S_IFCHR) == DT_CHR);
  42. assert (IFTODT(S_IFBLK) == DT_BLK);
  43. }
  44. /* Basic directory retrieval */
  45. {
  46. DIR *dir;
  47. struct dirent *ent;
  48. int found = 0;
  49. /* Open directory */
  50. dir = opendir ("tests/1");
  51. if (dir == NULL) {
  52. fprintf (stderr, "Directory tests/1 not found\n");
  53. abort ();
  54. }
  55. /* Read entries */
  56. while ((ent = readdir (dir)) != NULL) {
  57. /* Check each file */
  58. if (strcmp (ent->d_name, ".") == 0) {
  59. /* Directory itself */
  60. #ifdef _DIRENT_HAVE_D_TYPE
  61. assert (ent->d_type == DT_DIR);
  62. #endif
  63. #ifdef _DIRENT_HAVE_D_NAMLEN
  64. assert (ent->d_namlen == 1);
  65. #endif
  66. #ifdef _D_EXACT_NAMLEN
  67. assert (_D_EXACT_NAMLEN(ent) == 1);
  68. #endif
  69. #ifdef _D_ALLOC_NAMLEN
  70. assert (_D_ALLOC_NAMLEN(ent) > 1);
  71. #endif
  72. found += 1;
  73. } else if (strcmp (ent->d_name, "..") == 0) {
  74. /* Parent directory */
  75. #ifdef _DIRENT_HAVE_D_TYPE
  76. assert (ent->d_type == DT_DIR);
  77. #endif
  78. #ifdef _DIRENT_HAVE_D_NAMLEN
  79. assert (ent->d_namlen == 2);
  80. #endif
  81. #ifdef _D_EXACT_NAMLEN
  82. assert (_D_EXACT_NAMLEN(ent) == 2);
  83. #endif
  84. #ifdef _D_ALLOC_NAMLEN
  85. assert (_D_ALLOC_NAMLEN(ent) > 2);
  86. #endif
  87. found += 2;
  88. } else if (strcmp (ent->d_name, "file") == 0) {
  89. /* Regular file */
  90. #ifdef _DIRENT_HAVE_D_TYPE
  91. assert (ent->d_type == DT_REG);
  92. #endif
  93. #ifdef _DIRENT_HAVE_D_NAMLEN
  94. assert (ent->d_namlen == 4);
  95. #endif
  96. #ifdef _D_EXACT_NAMLEN
  97. assert (_D_EXACT_NAMLEN(ent) == 4);
  98. #endif
  99. #ifdef _D_ALLOC_NAMLEN
  100. assert (_D_ALLOC_NAMLEN(ent) > 4);
  101. #endif
  102. found += 4;
  103. } else if (strcmp (ent->d_name, "dir") == 0) {
  104. /* Just a directory */
  105. #ifdef _DIRENT_HAVE_D_TYPE
  106. assert (ent->d_type == DT_DIR);
  107. #endif
  108. #ifdef _DIRENT_HAVE_D_NAMLEN
  109. assert (ent->d_namlen == 3);
  110. #endif
  111. #ifdef _D_EXACT_NAMLEN
  112. assert (_D_EXACT_NAMLEN(ent) == 3);
  113. #endif
  114. #ifdef _D_ALLOC_NAMLEN
  115. assert (_D_ALLOC_NAMLEN(ent) > 3);
  116. #endif
  117. found += 8;
  118. } else {
  119. /* Other file */
  120. fprintf (stderr, "Unexpected file %s\n", ent->d_name);
  121. abort ();
  122. }
  123. }
  124. /* Make sure that all files were found */
  125. assert (found == 0xf);
  126. closedir (dir);
  127. }
  128. /* Function opendir() fails if directory doesn't exist */
  129. {
  130. DIR *dir;
  131. /* Open directory */
  132. dir = opendir ("tests/invalid");
  133. assert (dir == NULL);
  134. assert (errno == ENOENT);
  135. }
  136. /* Function opendir() fails if pathname is really a file */
  137. {
  138. DIR *dir;
  139. /* Open directory */
  140. dir = opendir ("tests/1/file");
  141. assert (dir == NULL);
  142. assert (errno == ENOTDIR);
  143. }
  144. /* Function opendir() fails if pathname is a zero-length string */
  145. {
  146. DIR *dir;
  147. /* Open directory */
  148. dir = opendir ("");
  149. assert (dir == NULL);
  150. assert (errno == ENOENT);
  151. }
  152. /* Rewind of directory stream */
  153. {
  154. DIR *dir;
  155. struct dirent *ent;
  156. int found = 0;
  157. /* Open directory */
  158. dir = opendir ("tests/1");
  159. assert (dir != NULL);
  160. /* Read entries */
  161. while ((ent = readdir (dir)) != NULL) {
  162. /* Check each file */
  163. if (strcmp (ent->d_name, ".") == 0) {
  164. /* Directory itself */
  165. found += 1;
  166. } else if (strcmp (ent->d_name, "..") == 0) {
  167. /* Parent directory */
  168. found += 2;
  169. } else if (strcmp (ent->d_name, "file") == 0) {
  170. /* Regular file */
  171. found += 4;
  172. } else if (strcmp (ent->d_name, "dir") == 0) {
  173. /* Just a directory */
  174. found += 8;
  175. } else {
  176. /* Other file */
  177. fprintf (stderr, "Unexpected file %s\n", ent->d_name);
  178. abort ();
  179. }
  180. }
  181. /* Make sure that all files were found */
  182. assert (found == 0xf);
  183. /* Rewind stream and read entries again */
  184. rewinddir (dir);
  185. found = 0;
  186. /* Read entries */
  187. while ((ent = readdir (dir)) != NULL) {
  188. /* Check each file */
  189. if (strcmp (ent->d_name, ".") == 0) {
  190. /* Directory itself */
  191. found += 1;
  192. } else if (strcmp (ent->d_name, "..") == 0) {
  193. /* Parent directory */
  194. found += 2;
  195. } else if (strcmp (ent->d_name, "file") == 0) {
  196. /* Regular file */
  197. found += 4;
  198. } else if (strcmp (ent->d_name, "dir") == 0) {
  199. /* Just a directory */
  200. found += 8;
  201. } else {
  202. /* Other file */
  203. fprintf (stderr, "Unexpected file %s\n", ent->d_name);
  204. abort ();
  205. }
  206. }
  207. /* Make sure that all files were found */
  208. assert (found == 0xf);
  209. closedir (dir);
  210. }
  211. /* Rewind with intervening change of working directory */
  212. {
  213. DIR *dir;
  214. struct dirent *ent;
  215. int found = 0;
  216. int errorcode;
  217. /* Open directory */
  218. dir = opendir ("tests/1");
  219. assert (dir != NULL);
  220. /* Read entries */
  221. while ((ent = readdir (dir)) != NULL) {
  222. /* Check each file */
  223. if (strcmp (ent->d_name, ".") == 0) {
  224. /* Directory itself */
  225. found += 1;
  226. } else if (strcmp (ent->d_name, "..") == 0) {
  227. /* Parent directory */
  228. found += 2;
  229. } else if (strcmp (ent->d_name, "file") == 0) {
  230. /* Regular file */
  231. found += 4;
  232. } else if (strcmp (ent->d_name, "dir") == 0) {
  233. /* Just a directory */
  234. found += 8;
  235. } else {
  236. /* Other file */
  237. fprintf (stderr, "Unexpected file %s\n", ent->d_name);
  238. abort ();
  239. }
  240. }
  241. /* Make sure that all files were found */
  242. assert (found == 0xf);
  243. /* Change working directory */
  244. errorcode = chdir ("tests");
  245. assert (errorcode == 0);
  246. /* Rewind stream and read entries again */
  247. rewinddir (dir);
  248. found = 0;
  249. /* Read entries */
  250. while ((ent = readdir (dir)) != NULL) {
  251. /* Check each file */
  252. if (strcmp (ent->d_name, ".") == 0) {
  253. /* Directory itself */
  254. found += 1;
  255. } else if (strcmp (ent->d_name, "..") == 0) {
  256. /* Parent directory */
  257. found += 2;
  258. } else if (strcmp (ent->d_name, "file") == 0) {
  259. /* Regular file */
  260. found += 4;
  261. } else if (strcmp (ent->d_name, "dir") == 0) {
  262. /* Just a directory */
  263. found += 8;
  264. } else {
  265. /* Other file */
  266. fprintf (stderr, "Unexpected file %s\n", ent->d_name);
  267. abort ();
  268. }
  269. }
  270. /* Make sure that all files were found */
  271. assert (found == 0xf);
  272. /* Restore working directory */
  273. errorcode = chdir ("..");
  274. assert (errorcode == 0);
  275. closedir (dir);
  276. }
  277. /* Long file name */
  278. {
  279. DIR *dir;
  280. struct dirent *ent;
  281. int found = 0;
  282. /* Open directory */
  283. dir = opendir ("tests/2");
  284. if (dir == NULL) {
  285. fprintf (stderr, "Directory tests/2 not found\n");
  286. abort ();
  287. }
  288. /* Read entries */
  289. while ((ent = readdir (dir)) != NULL) {
  290. /* Check each file */
  291. if (strcmp (ent->d_name, ".") == 0) {
  292. /* Directory itself */
  293. found += 1;
  294. } else if (strcmp (ent->d_name, "..") == 0) {
  295. /* Parent directory */
  296. found += 2;
  297. } else if (strcmp (ent->d_name, "file.txt") == 0) {
  298. /* Regular 8+3 filename */
  299. #ifdef _DIRENT_HAVE_D_TYPE
  300. assert (ent->d_type == DT_REG);
  301. #endif
  302. #ifdef _DIRENT_HAVE_D_NAMLEN
  303. assert (ent->d_namlen == 8);
  304. #endif
  305. #ifdef _D_EXACT_NAMLEN
  306. assert (_D_EXACT_NAMLEN(ent) == 8);
  307. #endif
  308. #ifdef _D_ALLOC_NAMLEN
  309. assert (_D_ALLOC_NAMLEN(ent) > 8);
  310. #endif
  311. found += 4;
  312. } else if (strcmp (ent->d_name, "Testfile-1.2.3.dat") == 0) {
  313. /* Long file name with multiple dots */
  314. #ifdef _DIRENT_HAVE_D_TYPE
  315. assert (ent->d_type == DT_REG);
  316. #endif
  317. #ifdef _DIRENT_HAVE_D_NAMLEN
  318. assert (ent->d_namlen == 18);
  319. #endif
  320. #ifdef _D_EXACT_NAMLEN
  321. assert (_D_EXACT_NAMLEN(ent) == 18);
  322. #endif
  323. #ifdef _D_ALLOC_NAMLEN
  324. assert (_D_ALLOC_NAMLEN(ent) > 18);
  325. #endif
  326. found += 8;
  327. } else {
  328. /* Other file */
  329. fprintf (stderr, "Unexpected file %s\n", ent->d_name);
  330. abort ();
  331. }
  332. }
  333. /* Make sure that all files were found */
  334. assert (found == 0xf);
  335. closedir (dir);
  336. }
  337. /* Basic directory retrieval with readdir_r */
  338. {
  339. DIR *dir;
  340. struct dirent ent[10];
  341. struct dirent *entry;
  342. size_t i = 0;
  343. size_t n = 0;
  344. int found = 0;
  345. /* Open directory */
  346. dir = opendir ("tests/1");
  347. if (dir == NULL) {
  348. fprintf (stderr, "Directory tests/1 not found\n");
  349. abort ();
  350. }
  351. /* Read entries to table */
  352. while (readdir_r (dir, &ent[n], &entry) == /*OK*/0 && entry != 0) {
  353. n++;
  354. assert (n <= 4);
  355. }
  356. /* Make sure that we got all the files from directory */
  357. assert (n == 4);
  358. /* Check entries in memory */
  359. for (i = 0; i < 4; i++) {
  360. entry = &ent[i];
  361. /* Check each file */
  362. if (strcmp (entry->d_name, ".") == 0) {
  363. /* Directory itself */
  364. #ifdef _DIRENT_HAVE_D_TYPE
  365. assert (entry->d_type == DT_DIR);
  366. #endif
  367. #ifdef _DIRENT_HAVE_D_NAMLEN
  368. assert (entry->d_namlen == 1);
  369. #endif
  370. #ifdef _D_EXACT_NAMLEN
  371. assert (_D_EXACT_NAMLEN(entry) == 1);
  372. #endif
  373. #ifdef _D_ALLOC_NAMLEN
  374. assert (_D_ALLOC_NAMLEN(entry) > 1);
  375. #endif
  376. found += 1;
  377. } else if (strcmp (entry->d_name, "..") == 0) {
  378. /* Parent directory */
  379. #ifdef _DIRENT_HAVE_D_TYPE
  380. assert (entry->d_type == DT_DIR);
  381. #endif
  382. #ifdef _DIRENT_HAVE_D_NAMLEN
  383. assert (entry->d_namlen == 2);
  384. #endif
  385. #ifdef _D_EXACT_NAMLEN
  386. assert (_D_EXACT_NAMLEN(entry) == 2);
  387. #endif
  388. #ifdef _D_ALLOC_NAMLEN
  389. assert (_D_ALLOC_NAMLEN(entry) > 2);
  390. #endif
  391. found += 2;
  392. } else if (strcmp (entry->d_name, "file") == 0) {
  393. /* Regular file */
  394. #ifdef _DIRENT_HAVE_D_TYPE
  395. assert (entry->d_type == DT_REG);
  396. #endif
  397. #ifdef _DIRENT_HAVE_D_NAMLEN
  398. assert (entry->d_namlen == 4);
  399. #endif
  400. #ifdef _D_EXACT_NAMLEN
  401. assert (_D_EXACT_NAMLEN(entry) == 4);
  402. #endif
  403. #ifdef _D_ALLOC_NAMLEN
  404. assert (_D_ALLOC_NAMLEN(entry) > 4);
  405. #endif
  406. found += 4;
  407. } else if (strcmp (entry->d_name, "dir") == 0) {
  408. /* Just a directory */
  409. #ifdef _DIRENT_HAVE_D_TYPE
  410. assert (entry->d_type == DT_DIR);
  411. #endif
  412. #ifdef _DIRENT_HAVE_D_NAMLEN
  413. assert (entry->d_namlen == 3);
  414. #endif
  415. #ifdef _D_EXACT_NAMLEN
  416. assert (_D_EXACT_NAMLEN(entry) == 3);
  417. #endif
  418. #ifdef _D_ALLOC_NAMLEN
  419. assert (_D_ALLOC_NAMLEN(entry) > 3);
  420. #endif
  421. found += 8;
  422. } else {
  423. /* Other file */
  424. fprintf (stderr, "Unexpected file %s\n", entry->d_name);
  425. abort ();
  426. }
  427. }
  428. /* Make sure that all files were found */
  429. assert (found == 0xf);
  430. closedir (dir);
  431. }
  432. /* Basic directory retrieval with _wreaddir_r */
  433. #ifdef WIN32
  434. {
  435. _WDIR *dir;
  436. struct _wdirent ent[10];
  437. struct _wdirent *entry;
  438. size_t i = 0;
  439. size_t n = 0;
  440. int found = 0;
  441. /* Open directory */
  442. dir = _wopendir (L"tests/1");
  443. if (dir == NULL) {
  444. fprintf (stderr, "Directory tests/1 not found\n");
  445. abort ();
  446. }
  447. /* Read entries to table */
  448. while (_wreaddir_r (dir, &ent[n], &entry) == /*OK*/0 && entry != 0) {
  449. n++;
  450. assert (n <= 4);
  451. }
  452. /* Make sure that we got all the files from directory */
  453. assert (n == 4);
  454. /* Check entries in memory */
  455. for (i = 0; i < 4; i++) {
  456. entry = &ent[i];
  457. /* Check each file */
  458. if (wcscmp (entry->d_name, L".") == 0) {
  459. /* Directory itself */
  460. #ifdef _DIRENT_HAVE_D_TYPE
  461. assert (entry->d_type == DT_DIR);
  462. #endif
  463. #ifdef _DIRENT_HAVE_D_NAMLEN
  464. assert (entry->d_namlen == 1);
  465. #endif
  466. #ifdef _D_EXACT_NAMLEN
  467. assert (_D_EXACT_NAMLEN(entry) == 1);
  468. #endif
  469. #ifdef _D_ALLOC_NAMLEN
  470. assert (_D_ALLOC_NAMLEN(entry) > 1);
  471. #endif
  472. found += 1;
  473. } else if (wcscmp (entry->d_name, L"..") == 0) {
  474. /* Parent directory */
  475. #ifdef _DIRENT_HAVE_D_TYPE
  476. assert (entry->d_type == DT_DIR);
  477. #endif
  478. #ifdef _DIRENT_HAVE_D_NAMLEN
  479. assert (entry->d_namlen == 2);
  480. #endif
  481. #ifdef _D_EXACT_NAMLEN
  482. assert (_D_EXACT_NAMLEN(entry) == 2);
  483. #endif
  484. #ifdef _D_ALLOC_NAMLEN
  485. assert (_D_ALLOC_NAMLEN(entry) > 2);
  486. #endif
  487. found += 2;
  488. } else if (wcscmp (entry->d_name, L"file") == 0) {
  489. /* Regular file */
  490. #ifdef _DIRENT_HAVE_D_TYPE
  491. assert (entry->d_type == DT_REG);
  492. #endif
  493. #ifdef _DIRENT_HAVE_D_NAMLEN
  494. assert (entry->d_namlen == 4);
  495. #endif
  496. #ifdef _D_EXACT_NAMLEN
  497. assert (_D_EXACT_NAMLEN(entry) == 4);
  498. #endif
  499. #ifdef _D_ALLOC_NAMLEN
  500. assert (_D_ALLOC_NAMLEN(entry) > 4);
  501. #endif
  502. found += 4;
  503. } else if (wcscmp (entry->d_name, L"dir") == 0) {
  504. /* Just a directory */
  505. #ifdef _DIRENT_HAVE_D_TYPE
  506. assert (entry->d_type == DT_DIR);
  507. #endif
  508. #ifdef _DIRENT_HAVE_D_NAMLEN
  509. assert (entry->d_namlen == 3);
  510. #endif
  511. #ifdef _D_EXACT_NAMLEN
  512. assert (_D_EXACT_NAMLEN(entry) == 3);
  513. #endif
  514. #ifdef _D_ALLOC_NAMLEN
  515. assert (_D_ALLOC_NAMLEN(entry) > 3);
  516. #endif
  517. found += 8;
  518. } else {
  519. /* Other file */
  520. fprintf (stderr, "Unexpected file\n");
  521. abort ();
  522. }
  523. }
  524. /* Make sure that all files were found */
  525. assert (found == 0xf);
  526. _wclosedir (dir);
  527. }
  528. #endif
  529. printf ("OK\n");
  530. return EXIT_SUCCESS;
  531. }