Welcome, guest | Sign In | My Account | Store | Cart
#include <fcntl.h>

/** 
 * Set a file descriptor to blocking or non-blocking mode.
 *
 * @param fd The file descriptor
 * @param blocking 0:non-blocking mode, 1:blocking mode
 *
 * @return 1:success, 0:failure.
 **/
int fd_set_blocking(int fd, int blocking) {
    /* Save the current flags */
    int flags = fcntl(fd, F_GETFL, 0);
    if (flags == -1)
        return 0;

    if (blocking)
        flags &= ~O_NONBLOCK;
    else
        flags |= O_NONBLOCK;
    return fcntl(fd, F_SETFL, flags) != -1;
}

Diff to Previous Revision

--- revision 1 2010-09-06 11:09:36
+++ revision 2 2010-09-06 11:20:53
@@ -1,12 +1,22 @@
 #include <fcntl.h>
 
-void fd_set_blocking(int fd, int blocking) {
+/** 
+ * Set a file descriptor to blocking or non-blocking mode.
+ *
+ * @param fd The file descriptor
+ * @param blocking 0:non-blocking mode, 1:blocking mode
+ *
+ * @return 1:success, 0:failure.
+ **/
+int fd_set_blocking(int fd, int blocking) {
+    /* Save the current flags */
     int flags = fcntl(fd, F_GETFL, 0);
     if (flags == -1)
-        flags = 0;
+        return 0;
+
     if (blocking)
         flags &= ~O_NONBLOCK;
     else
         flags |= O_NONBLOCK;
-    fcntl(fd, F_SETFL, flags);
+    return fcntl(fd, F_SETFL, flags) != -1;
 }

History