Thread: interpreting fcnrl F_GETFL

  1. #1
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    interpreting fcnrl F_GETFL

    Can anyone give me an actual example of how to do this:

    The normal return value from fcntl with this command is a nonnegative number which can be interpreted as the bitwise OR of the individual flags. Since the file access modes are not single-bit values, you can mask off other bits in the returned flags with O_ACCMODE to compare them.

    eg, given flags=fcntl(fd,F_GETFL); "flags" is 2050.

    also: once I've set O_NONBLOCK, how do I unset it?
    Last edited by MK27; 10-08-2008 at 09:08 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    My understanding of the Posix docs is that *if* you are interesting in access modes only (O_RDONLY, O_RDWR, O_WRONLY), then you use equality (==) and not a bit-wise AND. Before you check for equality, you need to mask off everything but the access mode bits using O_ACCMODE.

    If you want to check for status flags (O_APPEND, O_DSYNC, O_NONBLOCK, O_RSYNC, O_SYNC), then you can simply do a bit-wise AND.

    gg

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Can anyone give me an actual example of how to do this:

    Quote Originally Posted by Codeplug View Post
    My understanding of the Posix docs is that *if* you are interesting in access modes only (O_RDONLY, O_RDWR, O_WRONLY), then you use equality (==) and not a bit-wise AND. Before you check for equality, you need to mask off everything but the access mode bits using O_ACCMODE.

    If you want to check for status flags (O_APPEND, O_DSYNC, O_NONBLOCK, O_RSYNC, O_SYNC), then you can simply do a bit-wise AND.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    int flags;
    flags=fcntl(fd,F_GETFL);
    
    if ((flags & O_ACCMODE) == O_RDWR)
        ..
    That's how I would write it based on what Codeplug says [and it makes some sense].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Code:
    if (flags & O_APPEND)
       ... // open for appending
    Would be the other example...

    >> also: once I've set O_NONBLOCK, how do I unset it?
    Code:
    int flags = fcntl(fd, F_GETFL);
    flags &= (~O_NONBLOCK);
    fcntl(fd, F_SETFL, flags);
    Check for error conditions of course...

    gg

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Okay. Sorry guys, I just don't understand the use of ints and bitmasks with this kind of information. But now if:

    flags=(flags & O_ACCMODE);

    returns 2, which I presume is a normative unix file mode (read). What about the other flags (from before the mask)? How should I interpret "2050"?

    ps. I just searched cboard for "bitmasks flags" and this is the only post returned, sadly...
    Last edited by MK27; 10-08-2008 at 09:34 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    2050 is 2048 + 2, so it's got flags 2 and 2048 (0x800) set.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    thanks matsp, I found some tutorials and you've given me somewhere to start
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Read up here and post back with any questions.
    http://www.cprogramming.com/tutorial...operators.html

    >> How should I interpret "2050"?
    The way to "interpret" in code is to check which of the status flag bits are set using bit-wise AND - or use O_ACCMODE to check for equality with one of the access mode values.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newb question about interpreting and writing functions
    By crazychile in forum C Programming
    Replies: 1
    Last Post: 10-23-2008, 07:51 PM
  2. interpreting xml
    By WebmasterMattD in forum C++ Programming
    Replies: 3
    Last Post: 09-07-2003, 08:20 AM
  3. Interpreting literal escape sequences from a file...
    By Sebastiani in forum C++ Programming
    Replies: 1
    Last Post: 07-08-2003, 02:00 PM
  4. interpreting time fields in a structure
    By *ClownPimp* in forum C++ Programming
    Replies: 2
    Last Post: 01-14-2003, 12:07 PM
  5. Interpreting characters from physical paper
    By Zewu in forum Tech Board
    Replies: 10
    Last Post: 12-31-2002, 03:53 AM