I'm trying to read one line at a time, of arbitrary length, from stdin at the command line. I'm not sure if I'll be able to include GNU readline and would prefer to use a library function.
The documentation I've read suggests that getline
ought to work, but in my experiments it doesn't block. My sample program:
#include <stdio.h>
int main()
{
char *line = NULL;
if (getline(&line, NULL, stdin) == -1) {
printf("No line\n");
} else {
printf("%s\n", line);
}
return 0;
}
produces No line
, which makes it unsuitable for accepting user input.
How do I do this? I know it should be trivial, but I haven't been able to figure it out.