I'm trying to write an ftp server on Linux. In this matter how can I list files in the directory on terminal by a C program? Maybe I can use exec function to run find command but I want file name as a string to send client program. How can I do this?
Thanks for answers.
Below code will only print files within directory and exclude directories within given directory while traversing.
One tiny addition to JB Jansen's answer - in the main
readdir()
loop I'd add this:Just checking if it's really file, not (sym)link, directory, or whatever.
NOTE: more about
struct dirent
inlibc
documentation.An example, available for POSIX compliant systems :
Beware that such an operation is platform dependant in C.
Source : http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1046380353&id=1044780608
Here is a complete program how to recursively list folder's contents: