/* a sample program that sets an alredy opened file descriptor 
fd for nonblocking I/O.*/
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>

int main() {
    int fd;
    int fd_flags;    
    
    /* open file for reading only */
    if((fd = open("fcntl.c",O_RDONLY)) == -1) { 
        printf("cannot open file");
        exit(1);
    }
    if((fd_flags = fcntl(fd, F_GETFL,0)) == -1)
        perror("Could not get flags for fd");
    else {
        printf("Successfully retrieved flags for fd.\n");
        fd_flags |= O_NONBLOCK;
        if(fcntl(fd, F_SETFL, fd_flags) == -1)
            perror("Could not set flags for fd");
        else
            printf("Successfully set flags for fd.\n");
    }
    if(close(fd)) printf("error in closing file\n");
}
