1

I am writing a very basic shell for my school. Even thought commands like "pwd" or "date" works when i try "ls" it shows me that "ls: No such file or directory error"

The code of my shell is bellow:

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
int i;
char *line=(char *) malloc(1024*sizeof(char));

 while (1) {
   /* Print the command prompt */
   printf("$ ");
   fflush(stdout); /* Anagazei na trexei thn printf
   /* Read a command line */
   fgets(line, 1024, stdin);
  for(i=0;i<1024;i++){
   if(line[i]=='\n')
         {
           line[i]='\0';
         }
  }
  //printf("%s", line);
   pid_t pid = fork(); /* Dhmiourgei paidi. antigrafo diergasias. */
     if (pid==0){

           execlp(line,line);
      }
     else waitpid(pid,0,0);

  }
}
  • Looks like ls can not be found in the shells working directory. Try using the full path to ls like /bin/ls – Lambert Nov 19 '15 at 10:43
  • No it doesn't work either. This is what shows up : /bin/ls: cannot access : No such file or directory – Aris Kantas Nov 19 '15 at 10:47
  • This worked for me too. But it is not the solution. – Aris Kantas Nov 19 '15 at 11:00
2

Please read the man page for execlp carefully:

The list of arguments must be terminated by a NULL pointer, and, since these are variadic functions, this pointer must be cast (char *) NULL.

Using this line works for me:

execlp(line,line, (char*) NULL);

Your code also gave a warning for exactly this line (using GCC):

a.c: In function ‘main’:
a.c:26:12: warning: not enough variable arguments to fit a sentinel [-Wformat=]
            execlp(line,line);
            ^
  • Thank you so much. Since i am a rookie this helped a lot. have a nice day. – Aris Kantas Nov 19 '15 at 10:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.