title: execvp - ls: fts_open: No such file or directory
body: <p>I'm currently struggling with this error. I'm writing a shell emulator, using fork() for executing a command using execvp();.
  Almost every command I try to parse to my shell is working perfectly, except for ls without arguments.
  If I try to execute ls -lah, everything works, but a simple ls won't, receiving the following error:</p>
  
  <pre><code>ls: fts_open: No such file or directory
  </code></pre>
  
  <p>Here is a snippet of my code: (just the essential)</p>
  
  <pre><code>pid = fork();
        if (pid==0)
        {
          puts("CHILD");
          puts(args[0]);
          puts(args[1]);
          printf("%d\n", strlen(args[1]));
          args[2] = NULL;
          execvp(args[0], args);
        }
        else wait(NULL);
        puts("BACK TO PARENT");
      }
  </code></pre>
  
  <p>and here is the output for ls:</p>
  
  <pre><code>ls
  CHILD
  ls
  
  0
  ls: fts_open: No such file or directory
  BACK TO PARENT
  </code></pre>
  
  <p>as you can see, args[0] contains ls, args[1] is empty, as there are no arguments, the length of args[1] is 0, but i keep getting the error.</p>
  
  <p>Any idea on what it could be?</p>
  
  <p>EDIT:</p>
  
  <p>Kudos for Jonathan Leffler for finding it out: (also, it seems like it is just an issue on Mac)</p>
  
  <p>The point is that args[1] is not really empty, so, the OS tries to open the '' file, which, obviously, does not exists, and, by what is looks, can't be created, since it is not really a name.</p>
  
  <p>So, Here is what I did: check the len of args[1]. If it is 0, set it to NULL. (just freeing the memory did not really helped)</p>
  
  <pre><code>pid = fork();
        if (pid==0)
        {
          puts("CHILD");
          puts(args[0]);
          puts(args[1]);
          if (strlen(args[1]) == 0)
              args[1] = 0;
          args[2] = NULL;
          execvp(args[0], args);
        }
        else wait(NULL);
        puts("BACK TO PARENT");
      }
  </code></pre>