opendir sleeps or infinite loop the program <p>I'm making a simple LS command in C in a program, when I open the directory for the first time and read it, it works perfectly, but when I call the function a second time, <code>opendir()</code> seems to sleep or loop infinitely:</p> <pre><code>int server_list(t_server_data *sd) { DIR *dir; struct dirent *entry; printf("In list()\n"); printf("Open directory\n"); if ((dir = opendir("./")) == NULL) perror("Error: opendir()"); printf("Directory opened\n"); while ((entry = readdir(dir)) != NULL) { printf("Reading dir...\n"); /* code */ } closedir(dir); return (0); } </code></pre> <p>Then this is the output I get: </p> <pre class="lang-none prettyprint-override"><code>In list() Open directory </code></pre> <p>And the program does nothing (waits).</p>