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.
Enter
key?stdout
is connected to an interactive terminal, thenstdout
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 withfflush(stdout)
, or print a newline'\n'
. So please make it a habit to always add a trailing newline to most of your output.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.printf("input received: [%s]\n", buff);
. You should also check that you received at least one character fromgetline()
— it should never return 0, but could return-1
on error or EOF. You'll find thatgetline()
includes the newline in the returned data. You could remove that before printing the data.