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

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

1 Answer 1

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);
            ^
1
  • Thank you so much. Since i am a rookie this helped a lot. have a nice day. Commented Nov 19, 2015 at 10:57

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.