1

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, opendir() seems to sleep or loop infinitely:

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);
}

Then this is the output I get:

In list()
Open directory

And the program does nothing (waits).

PiotrWolkowski
  • 8,408
  • 6
  • 48
  • 68
Aurelien
  • 339
  • 3
  • 12
  • 1
    The code you posted cannot be causing this behavior, maybe you have some other code causing undefined behavior, and then something happens when you call this function. – Iharob Al Asimi Mar 18 '15 at 12:28
  • 1
    You say it "works perfectly [the first time]". I see no evidence of that in your sample output. Have you trimmed the output or given a bad description? – William Pursell Mar 18 '15 at 12:37
  • 3
    Attach a debugger i.e. by using gdb: `gdb -p `, and call `bt` to get backtrace and understand where program had been hanged. `strace` may be also useful in your case – myaut Mar 18 '15 at 12:45
  • 1
    Standard output may be buffered. Add `fflush(stdout);` after `printf()` to be sure the the problem is in this part of the code. In the end, the most effective way to find the issue will be a debugger like gdb, as adviced by @myaut. – francis Mar 18 '15 at 18:05
  • 1
    My problem is solved, I debug it with valgrind and gdb. The problem was outside this function, but it's still strange that opendir() didn't work. Anyway, for people having the same issue, I'd got many memory problems, I solve the issue by fixing them. Thanks for the help – Aurelien Mar 21 '15 at 00:44

0 Answers0