Writing a simple shell in C using fork/execvp

The invalid option is because fgets() keeps the '\n' when you press enter, try this

if(!fgets(line, BUFFER_LEN, stdin))
    break;
size_t length = strlen(line);
if (line[length - 1] == '\n')
    line[length - 1] = '\0';

when you are trying to call ls -l you are passing "-l\n" as the option, hence the message.

You will have to change

strcmp(line, "exit\n")

to

strcmp(line, "exit")

Leave a Comment