How do i unset a already set flag using fcntl?
For e.g. I can set the socket to nonblocking mode using
fcntl(sockfd, F_SETFL, flags | O_NONBLOCK)
Now, i want to unset the O_NONBLOCK flag.
I tried fcntl(sockfd, F_SETFL, flags | ~O_NONBLOCK). It gave me error EINVAL
int oldfl;
oldfl = fcntl(sockfd, F_GETFL);
if (oldfl == -1) {
/* handle error */
}
fcntl(sockfd, F_SETFL, oldfl & ~O_NONBLOCK);
Untested, but hope this helps. :-)
val = fcntl(fd, F_GETFL, 0);
flags = O_NONBLOCK;
val &= ~flags;
fcntl(fd,F_SETFL,val);
If you do like this,The already set O_NONBLOCK will unset. here,flags contains the which flags you want to unset. After finishing the AND(&) operation,again you have to set the flag using the value in val. I hope this will help you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With