I'm trying to write a minishell with this code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LINE_LEN 50
#define MAX_PARTS 50
int main ()
{
char* token;
char str[LINE_LEN];
char* arr[MAX_PARTS];
int i,j;
printf("Write a line: \n $:");
fgets(str, LINE_LEN, stdin);
while (str != "quit") {
i=0;
token = strtok(str, " \t\r\n");
while( token != NULL )
{
arr[i] = token;
printf("'%s'\n", arr[i]);
i++;
token = strtok(NULL," \t\r\n");
}
fflush(stdin);
printf("Write a line: \n $:");
fgets(str, LINE_LEN, stdin);
}
return 0;
}
It works correctly, but for the fact that when I type "quit", the program doesn't end. I guess it must be related with fgets and the fact that it incorporates EOF as another character at the end of string str, so when I compare it with "quit" it's not the same string. I don't know which is the correct way to extract the \n character from the string I read from standard input. ¿Could anyone help me, please?
Another question I would like to ask is if it's a good idea to use fflush in this code or not. Thank you