linux programming fcntl get and set file status

#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h> 
#include <fcntl.h> 

#define Err_Exit (m) \ 
        do \ 
        {\ 
                perror (m); \ 
                Exit (EXIT_FAILURE); \ 
        } the while (0) 

// settings to a file status 
void Set_Flag (int fd, int flags); 
// remove a file state 
void clear_flag (int fd, int flags); 

int main (int argc, char * argv [ ]) { 

        char buf [64] = {0}; 
        int Nread; 

        // set unblocked 
        Set_Flag (0, the O_NONBLOCK); 
        // clear the unblocked state, but becomes blocked reads standard input 
        clear_flag (0, O_NONBLOCK) ; 
        Nread = Read (0, buf, 64);
        IF (Nread == -1) { 
                Err_Exit ( "get input Failed"); 
        } 
        the printf ( "% S buf = \ n-", buf); 

        return 0; 
} 

void Set_Flag (FD int, int the flags) { 
        int oldflag; 
        = the fcntl oldflag (FD, F_GETFL, 0); 
        IF (oldflag == -1) { 
                Err_Exit ( "Get original state failure"); 
        } 

        oldflag oldflag = | the flags; 
        int RET = the fcntl (FD, the F_SETFL, oldflag); 
        IF (RET == -1) { 
                Err_Exit ( "failed state to set a new file"); 
        } 
} 

void clear_flag (FD int, int the flags) { 
        int oldflag; 
        oldflag = the fcntl (FD, F_GETFL, 0);
        IF (oldflag == -1) { 
                ERR_EXIT ( "Failed to obtain original state ");
        } 
        Oldflag = ~ & oldflag the flags; 
        int RET = the fcntl (FD, the F_SETFL, oldflag); 
        IF (RET == -1) { 
                Err_Exit ( "failed state to set a new file"); 
        } 
}

  

Guess you like

Origin www.cnblogs.com/ghostwu/p/10984615.html