Program to list all files in a directory (recursively)

So, I have two version of a program that is supposed to do the same thing: Take in a dir path as argument, and recursively list all the files in that path.

Version 1 works perfectly. Version 2 crashes with STACK OVERFLOW.

Version 1: It is designed for MULTIBYTE character set. It makes use of the stat() system call.

Version 2: It is designed for Unicode. Moreover, stat() had some problems with old files and so I replaced it with GetFileAttributes(). Also, I wanted to support long paths so I append the path with \\?\ (file:///?\).

Can someone please have a look at the code and tell me why does Version 2 crash ?

(I tried copy pasting the code here from my IDE, however the formatting got really messed up, so I am attachting the 2 versions as .c files. I hope that does not prevent you guys from having a look at it :--)

Now another problem in Version 2: you will see that I am dynamically allocating path. malloc for this fails, I have no idea why. Does it fail for you too ? (for the purpose of testing, I used an array of wchar_t)

I had:

LPTSTR path = NULL;
path = (LPTSTR)malloc(pathlen);


which I changed to

wchar_t path[32767];

I will really appreciate if someone can tell me whats going on. Why do I get a stack overflow and a failed malloc for Version 2.

The stack overflow occurs when the depth of a directory is around 12 or 13.

Thanks !!!
[1498 byte] By [the_learner] at [2007-11-20 11:54:21]
# 1 Re: Program to list all files in a directory (recursively)
You don't need any dynamically allocated memory. Take a look at this FAQ: http://www.dev-archive.com/forum/showthread.php?t=312461.
cilu at 2007-11-11 4:02:26 >