0

I am trying to build a simple shell and im having a huge problem with receiving input from the stdin. For whatever reason when i call getline(), the program gets stuck there and it can't progress.

This is what the code looks like.

int main(void)
{
    int chars_read, istty;
    char *buff = NULL;
    size_t size = 0;


    while(1)
    {
        istty = isatty(STDIN_FILENO);

        if (istty == 1)
            printf("$ ");

        chars_read = getline(&buff, &size, stdin);
        printf("input received");

I tried whatever i could think of, but nothing worked. I have no idea what is causing this error and so I can't even think of a solution.

5
  • You are giving some input, terminated with the Enter key? Commented Apr 26 at 15:11
  • 1
    Also note that your debug printing is flawed. If stdout is connected to an interactive terminal, then stdout is line buffered. That means output will not be actually written to the terminal unless you fill up the (rather large) buffer, explicitly do it with fflush(stdout), or print a newline '\n'. So please make it a habit to always add a trailing newline to most of your output. Commented Apr 26 at 15:13
  • Yes i gave some input with "ls", and everything you said about the stdout is just what i needed, although the code still isn't working but I can now debug it better. Thank you a lot!
    – Percy
    Commented Apr 26 at 15:20
  • Questions seeking debugging help should generally provide a minimal reproducible example of the problem, which includes a complete function main and all #include directives, as well as the exact input required to reproduce the problem. This allows other people to easily test your program, by simply using copy&paste. Note that you can edit the question to improve it. Commented Apr 26 at 15:28
  • You should print the input you got so you know the program saw what you expected it to see. For example: printf("input received: [%s]\n", buff);. You should also check that you received at least one character from getline() — it should never return 0, but could return -1 on error or EOF. You'll find that getline() includes the newline in the returned data. You could remove that before printing the data. Commented Apr 26 at 16:17

0

Browse other questions tagged or ask your own question.