Problem I have:
When I was playing getchar()
behavior, I found my terminal (macOS Mohave Version 10.14.6 18G87) does not honor Ctrl+D as EOF.
How I got the problem:
The code with getchar()
is attached below, basically this code is echo getchar() if the input char is not blank, otherwise, for multiple continously blank char it only output one single blank char.
The code itself works, but when running it in terminal, the terminal does not honor Ctrl+D as EOF, thus the code never terminates.
I am sure this is due to I mistakenly use system ("/bin/stty raw");
and system ("/bin/stty cooked");
, however, I dont' know how to fix it.
/* K&R C Exercise 1-9. Write a program to copy its input to its output,
* replacing each string of one or more blanks by a single blank.*/
#include <stdio.h>
#include <stdlib.h>
int main(void){
int ch;
int prev = -1;
/* if you are in a UNIX like environment
* the ICANON flag is enabled by default, so input is buffered until the next '\n' or EOF
* https://stackoverflow.com/a/1799024/5450745
* */
system ("/bin/stty raw");
while ((ch=getchar())!= EOF){
if (ch != ' ' ){
if (prev == ' '){
putchar(prev);
putchar(ch);
}
else{
putchar(ch);
}
prev = ch;
}
else{
prev = ch;
continue;
}
}
/* use system call to set terminal behaviour to more normal behaviour */
system ("/bin/stty cooked");
return 0;
}
I checked stty
, however, it does configure EOF
as Ctrl +D
. However, right now, if I press Ctrl + D
, it only splits the terminal from one window to two.
What can I do to allow EOF
re-enable again?
Thanks!