Question

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

Was it helpful?

Solution

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.

Nope.

First of all, EOF doesn't exist as a character, it's just a flag returned by some IO functions to tell you that the file finished while trying to read it.

What fgets leaves in the string is the newline character (if it stopped because of it and not because of EOF or of an error). You can easily remove it - if it's present - after acquiring the string:

size_t l=strlen(str);
if(l && str[l-1]=='\n')
    str[l-1]=0;

But most importantly, you are getting the comparison with "quit" wrong.

If you write str != "quit" you are performing a pointer comparison between str and "quit", which will always fail since str and "quit" are distinct character arrays, and thus refer to different locations in memory. What you want is to compare the content of the two strings; in C, that is done with strcmp:

while(strcmp(str, "quit")!=0)

OTHER TIPS

fgets will read newline character as well from statement below..

fgets(str, LINE_LEN, stdin);

So you have two option

  1. use condition as below to compare 'quit':

    while (strcmp(str, "quit\n"))  
    
  2. Remove newline from input. Detail here

You cannot compare strings by pointer like you do str != "quit"

Use 0 != strcmp(str,"quit") instead.

if(str[4] == '\n')
    str[4] = '\0';
while (strcmp(str, "quit")!=0) {
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top