128

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.

erip
  • 16,374
  • 11
  • 66
  • 121
cemal
  • 1,313
  • 2
  • 11
  • 8

5 Answers5

253

An example, available for POSIX compliant systems :

/*
 * This program displays the names of all files in the current directory.
 */

#include <dirent.h> 
#include <stdio.h> 

int main(void) {
  DIR *d;
  struct dirent *dir;
  d = opendir(".");
  if (d) {
    while ((dir = readdir(d)) != NULL) {
      printf("%s\n", dir->d_name);
    }
    closedir(d);
  }
  return(0);
}

Beware that such an operation is platform dependent in C.

Source : http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1046380353&id=1044780608

Grandpa
  • 3,053
  • 1
  • 25
  • 35
Jean-Bernard Jansen
  • 7,544
  • 2
  • 20
  • 17
  • 23
    please validate it if you liked it ;) – Jean-Bernard Jansen Nov 17 '10 at 16:50
  • 2
    Great, But what if we want only `png` files? – Farshid Ashouri Feb 07 '14 at 20:34
  • 3
    @Farsheed: try [this](http://stackoverflow.com/questions/10347689/how-can-i-check-whether-a-string-ends-with-csv-in-c). – ashastral Oct 08 '14 at 20:17
  • I encounter few problems with this. First, "." and ".." appear at the top of every directory, and although they are "directories" they have the dir->d_type set to DT_REG. Also, I dont seem to get all the files... Is there a more comprehensive "directory scanner" code somewhere? Maybe some poor-mans implementation of "ls" ? I need this on Mac - OS-X – Motti Shneor Apr 07 '16 at 09:25
  • This is missing error handling. For that, you would need to set `errno = 0` before each operation and always check if once an operation returns `NULL` (see man pages of opendir and readdir). `closedir` on the other hand returns 0 when an error occurs. – pcworld Nov 12 '21 at 17:34
  • @motti-shneor this is on purpose to keep the example dead simple. You can get what you want with more code. – Jean-Bernard Jansen Dec 17 '21 at 09:28
  • @pcworld, again, on purpose to stay dead simple, as behavior when an error occurs will depend on the use case. – Jean-Bernard Jansen Dec 17 '21 at 09:29
50

One tiny addition to JB Jansen's answer - in the main readdir() loop I'd add this:

  if (dir->d_type == DT_REG)
  {
     printf("%s\n", dir->d_name);
  }

Just checking if it's really file, not (sym)link, directory, or whatever.

NOTE: more about struct dirent in libc documentation.

Community
  • 1
  • 1
Kamiccolo
  • 7,758
  • 3
  • 34
  • 47
  • 9
    Just an aside: Not all platforms will fill out `d_type`, but Linux and BSD will (I know the question is tagged Linux, just extending on the answer a little); even then, not *all* filesystems are supported *uniformly*, however it should work with most FSs. – omninonsense Jul 12 '16 at 06:52
17

Here is a complete program how to recursively list folder's contents:

#include <dirent.h> 
#include <stdio.h> 
#include <string.h>

#define NORMAL_COLOR  "\x1B[0m"
#define GREEN  "\x1B[32m"
#define BLUE  "\x1B[34m"



/* let us make a recursive function to print the content of a given folder */

void show_dir_content(char * path)
{
  DIR * d = opendir(path); // open the path
  if(d==NULL) return; // if was not able, return
  struct dirent * dir; // for the directory entries
  while ((dir = readdir(d)) != NULL) // if we were able to read somehting from the directory
    {
      if(dir-> d_type != DT_DIR) // if the type is not directory just print it with blue color
        printf("%s%s\n",BLUE, dir->d_name);
      else
      if(dir -> d_type == DT_DIR && strcmp(dir->d_name,".")!=0 && strcmp(dir->d_name,"..")!=0 ) // if it is a directory
      {
        printf("%s%s\n",GREEN, dir->d_name); // print its name in green
        char d_path[255]; // here I am using sprintf which is safer than strcat
        sprintf(d_path, "%s/%s", path, dir->d_name);
        show_dir_content(d_path); // recall with the new path
      }
    }
    closedir(d); // finally close the directory
}

int main(int argc, char **argv)
{
  printf("%s\n", NORMAL_COLOR);
  show_dir_content(argv[1]);
  printf("%s\n", NORMAL_COLOR);
  return(0);
}
Faroq AL-Tam
  • 495
  • 5
  • 10
  • 1
    *here I am using sprintf which is safer than strcat* not really! you should be using `snprintf` for safety. – chqrlie Jun 14 '22 at 15:12
6

Below code will only print files within directory and exclude directories within given directory while traversing.

#include <dirent.h>
#include <stdio.h>
#include <errno.h>
#include <sys/stat.h>
#include<string.h>
int main(void)
{
    DIR *d;
    struct dirent *dir;
    char path[1000]="/home/joy/Downloads";
    d = opendir(path);
    char full_path[1000];
    if (d)
    {
        while ((dir = readdir(d)) != NULL)
        {
            //Condition to check regular file.
            if(dir->d_type==DT_REG){
                full_path[0]='\0';
                strcat(full_path,path);
                strcat(full_path,"/");
                strcat(full_path,dir->d_name);
                printf("%s\n",full_path);
            }
        }
        closedir(d);
    }
    return(0);     
}
AS Mackay
  • 2,831
  • 9
  • 19
  • 25
Joy
  • 394
  • 2
  • 9
0

you can easly make it with doTheWorld lib

https://github.com/OUIsolutions/DoTheWorld



#include "doTheWorld.h"


int main (){

    DtwStringArray *elements = dtw_list_files_recursively("exemples",DTW_NOT_CONCAT_PATH);
    for(int i = 0; i < elements->size; i++){
      char *current =  elements->strings[i];
      printf("current %s\n",current);
    }
 
    return 0;

}