I am tring to write the shortest code to have a blocking file descriptor.
I set first: O_NONBLOCK
second: ICANON, [VMIN], [VTIME] for my file descriptor...
What else options I need to set to have a blocking file descriptor ?
(sample.txt is empty & open() with different mode does not chance anything)
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <termios.h>
void set_blocking(int fd, int blocking) {
int flags = fcntl(fd, F_GETFL, 0);
if (blocking)
flags &= ~O_NONBLOCK;
else
flags |= O_NONBLOCK;
fcntl(fd, F_SETFL, flags);
return;
}
int main(){
int fd;
char buff[100];
struct termios options;
options.c_lflag &= ~ICANON;
options.c_cc[VMIN] = 2;
options.c_cc[VTIME] = 0;
fd = open("sample.txt",O_RDWR);
tcsetattr(fd, TCSANOW, &options);
set_blocking(fd,1);
read(fd,buff,2);
printf("%s\n",buff);
return 0;
}
fcntl()to get and save the original terminal settings so you can restore them before exiting. And modify those rather than setting them from scratch. - Jonathan Lefflertcsetattronly works on special terminal device files, not on ordinary files. I doubt that your "sample.txt" is a terminal. - Ian Abbottperrorto show the error reason. Please do this before posting here. - rici