1515#include <sys/stat.h>
1616
1717#ifdef _WIN32
18- #define TEST_DIR "streamio_walker_test"
18+ #include <windows.h>
19+ static char g_test_dir [MAX_PATH ];
20+ static const char * get_test_dir (void ) {
21+ static int initialized = 0 ;
22+ if (!initialized ) {
23+ GetTempPathA (MAX_PATH , g_test_dir );
24+ strcat (g_test_dir , "streamio_walker_test" );
25+ initialized = 1 ;
26+ }
27+ return g_test_dir ;
28+ }
29+ #define TEST_DIR get_test_dir()
1930#else
2031#define TEST_DIR "/tmp/streamio_walker_test"
2132#endif
@@ -41,35 +52,77 @@ static int test_passed = 0;
4152 return; \
4253 } while (0)
4354
55+ /* Helper to create directory cross-platform */
56+ static int mkdir_compat (const char * path ) {
57+ #ifdef _WIN32
58+ return _mkdir (path );
59+ #else
60+ return mkdir (path , 0755 );
61+ #endif
62+ }
63+
64+ /* Helper to build path cross-platform */
65+ static void build_path (char * dest , size_t size , const char * dir , const char * file ) {
66+ #ifdef _WIN32
67+ snprintf (dest , size , "%s\\%s" , dir , file );
68+ #else
69+ snprintf (dest , size , "%s/%s" , dir , file );
70+ #endif
71+ }
72+
73+ /* Recursive directory removal */
74+ static void rmdir_recursive (const char * path ) {
75+ #ifdef _WIN32
76+ char cmd [MAX_PATH + 20 ];
77+ snprintf (cmd , sizeof (cmd ), "rmdir /s /q \"%s\" 2>nul" , path );
78+ system (cmd );
79+ #else
80+ char cmd [PATH_MAX + 20 ];
81+ snprintf (cmd , sizeof (cmd ), "rm -rf \"%s\"" , path );
82+ system (cmd );
83+ #endif
84+ }
85+
4486/* Create test directory structure */
4587static int create_test_tree (void )
4688{
47- system ("rm -rf " TEST_DIR );
48- mkdir (TEST_DIR , 0755 );
89+ char path [512 ];
90+
91+ rmdir_recursive (TEST_DIR );
92+ mkdir_compat (TEST_DIR );
4993
5094 /* Create files */
51- FILE * f1 = fopen (TEST_DIR "/file1.txt" , "w" );
95+ build_path (path , sizeof (path ), TEST_DIR , "file1.txt" );
96+ FILE * f1 = fopen (path , "w" );
5297 if (!f1 ) return -1 ;
5398 fprintf (f1 , "File 1 content" );
5499 fclose (f1 );
55100
56- FILE * f2 = fopen (TEST_DIR "/file2.txt" , "w" );
101+ build_path (path , sizeof (path ), TEST_DIR , "file2.txt" );
102+ FILE * f2 = fopen (path , "w" );
57103 if (!f2 ) return -1 ;
58104 fprintf (f2 , "File 2 content" );
59105 fclose (f2 );
60106
61107 /* Create subdirectory */
62- mkdir (TEST_DIR "/subdir" , 0755 );
108+ build_path (path , sizeof (path ), TEST_DIR , "subdir" );
109+ mkdir_compat (path );
63110
64- FILE * f3 = fopen (TEST_DIR "/subdir/file3.txt" , "w" );
111+ char subdir_path [512 ];
112+ build_path (subdir_path , sizeof (subdir_path ), TEST_DIR , "subdir" );
113+ build_path (path , sizeof (path ), subdir_path , "file3.txt" );
114+ FILE * f3 = fopen (path , "w" );
65115 if (!f3 ) return -1 ;
66116 fprintf (f3 , "File 3 in subdir" );
67117 fclose (f3 );
68118
69119 /* Create nested subdirectory */
70- mkdir (TEST_DIR "/subdir/nested" , 0755 );
120+ char nested_path [512 ];
121+ build_path (nested_path , sizeof (nested_path ), subdir_path , "nested" );
122+ mkdir_compat (nested_path );
71123
72- FILE * f4 = fopen (TEST_DIR "/subdir/nested/file4.txt" , "w" );
124+ build_path (path , sizeof (path ), nested_path , "file4.txt" );
125+ FILE * f4 = fopen (path , "w" );
73126 if (!f4 ) return -1 ;
74127 fprintf (f4 , "File 4 in nested" );
75128 fclose (f4 );
@@ -80,7 +133,7 @@ static int create_test_tree(void)
80133/* Clean up test files */
81134static void cleanup_test_tree (void )
82135{
83- system ( "rm -rf " TEST_DIR );
136+ rmdir_recursive ( TEST_DIR );
84137}
85138
86139/* Test walking a single file */
@@ -99,8 +152,11 @@ void test_walk_single_file(void)
99152 if (create_test_tree () != 0 )
100153 FAIL ("Failed to create test tree" );
101154
155+ char file_path [512 ];
156+ build_path (file_path , sizeof (file_path ), TEST_DIR , "file1.txt" );
157+
102158 single_file_count = 0 ;
103- int ret = walk_path (TEST_DIR "/file1.txt" , single_file_callback , NULL , 0 );
159+ int ret = walk_path (file_path , single_file_callback , NULL , 0 );
104160 if (ret < 0 ) {
105161 cleanup_test_tree ();
106162 FAIL ("walk_path failed" );
@@ -459,7 +515,8 @@ int main(void)
459515 test_walk_filter_dirs ();
460516 test_read_file_streams ();
461517
462- #ifdef HAVE_LIBARCHIVE
518+ #if defined(HAVE_LIBARCHIVE ) && !defined(_WIN32 )
519+ /* Archive tests require tar command, skip on Windows */
463520 test_walk_expand_archive ();
464521 test_read_archive_streams ();
465522#ifdef HAVE_ZLIB
0 commit comments