Fcntl Get file Status flag

Source: Internet
Author: User

int fcntl (int fd,int cmd,...)

The function Fcntl provides a very rich feature. The main dependencies are the various parameters of CMD:

    1. Copy an existing file descriptor F_dupfd,f_dupfd_cloexec
    2. Gets the flag for the set file descriptor F_getfd,f_setfd
    3. Get the settings file status flag F_GETFL,F_SETFL
    4. Gets the ownership of the set asynchronous IO F_GETOWN,F_SETFL
    5. Get Set record lock f_getlk,f_setlk,f_setlkw

  

When cmd= F_GETFL, the function of fcntl is to obtain the file status of FD flag. Unlike anywhere else, the returned value is not one of each of the two symbols that make up the binary. Because O_rdonly,o_wronly,

The O_RDWR is a mutually exclusive state. When processing the return value, use the O_accmode macro to take out the read and write state of the FD file.

The O_rdonly,o_wronly,o_rdwr,o_accmode is printed out separately 0,1,2,3, with 3 to go in the 0,1,2 result is obvious.

DEMO:

  

1#include <unistd.h>2#include <fcntl.h>3#include <stdlib.h>4#include <stdio.h>5 6 intMainintargcChar**argv)7 {8     if(ARGC! =2)9         return-1;Ten     intFL =0; One     if(fl = Fcntl (atoi (argv[1]), F_GETFL) <0) { Aprintf"GETFL failed."); -Exit (-1); -     } the  -     Switch(FL &O_accmode) { -          Caseo_rdonly: -printf"o_rdonly\n"); +              Break; -          Caseo_wronly: +printf"o_wronly\n"); A              Break; at          CaseO_rdwr: -printf"o_rdwr\n"); -              Break; -     } -  -     if(FL &o_append) inprintf"o_append\n"); -     if(FL &O_nonblock) toprintf"o_nonblock\n"); +     if(FL &O_sync) -printf"o_sync\n"); the     if(FL &O_fsync) *printf"o_fsync\n"); $ Panax Notoginseng}

Results:

  

 $a. out  0  < 1  .c o_rdonly $a.  out  1  > . C $cat  1  .c o_wronly $a.  out  1  1  >> 1  .c $cat  1  .c o_wronly o_append $a.  out  2  2  <> 1  .c o_rdwr  

Using redirection above, 0,1 redirect to file separately. So to FD GETFL is to this file GETFL, the result shows o_accmode feasible.

Fcntl Get file Status flag

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.