Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^2: open(): Is there a simple way to map between numeric open modes and "symbol modes"?

by Anonymous Monk
on Feb 08, 2013 at 02:39 UTC ( #1017748=note: print w/ replies, xml ) Need Help??


in reply to Re: open(): Is there a simple way to map between numeric open modes and "symbol modes"?
in thread open(): Is there a simple way to map between numeric open modes and "symbol modes"? (solved)

From Fmode:

my %modes; @modes{ qw < > >> +< +> +>>  } = ( 1, 2, 2, 128, 128, 128 );
Are these the official constants? If so, how do I un-OR OR results, against the other flags, to get back to these basic constants?


Comment on Re^2: open(): Is there a simple way to map between numeric open modes and "symbol modes"?
Re^3: open(): Is there a simple way to map between numeric open modes and "symbol modes"?
by BrowserUk (Pope) on Feb 08, 2013 at 03:14 UTC

    As I remember, I "discovered" them by writing a small C program that open files in each of the modes and then queried the numeric value using this code (from Fmode.xs):

    int xs_fmode( FILE *stream ) { return stream->_flag; }

    And I discovered that incantation by finding the struct FILE definition in the C header files.

    NOTE: This was done using MSVC header files; YMMV on other platforms.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re^3: open(): Is there a simple way to map between numeric open modes and "symbol modes"?
by BrowserUk (Pope) on Feb 08, 2013 at 03:24 UTC
    how do I un-OR OR results, against the other flags, to get back to these basic constants?

    (From MSVC header files:

    #define _O_RDONLY 0x0000 /* open for reading only */ #define _O_WRONLY 0x0001 /* open for writing only */ #define _O_RDWR 0x0002 /* open for reading and writing */ #define _O_APPEND 0x0008 /* writes done at eof */ #define _O_CREAT 0x0100 /* create and open file */ #define _O_TRUNC 0x0200 /* open and truncate */ #define _O_EXCL 0x0400 /* open only if file doesn't already e +xist */ #define _O_TEXT 0x4000 /* file mode is text (translated) */ #define _O_BINARY 0x8000 /* file mode is binary (untranslated) +*/ #define _O_WTEXT 0x10000 /* file mode is UTF16 (translated) */ #define _O_U16TEXT 0x20000 /* file mode is UTF16 no BOM (translat +ed) */ #define _O_U8TEXT 0x40000 /* file mode is UTF8 no BOM (translat +ed) */

    So, assuming the variable $mode contains the OR'd values; you can check which bits are set something like:

    if( $mode & 1 ) { ## _O_WRONLY say "File is opend WRITE only }

    But note: not all of the bits that can be set when opening a file are retained in FILE*->_flag.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Thumbs up for this load of code!! Thank you BrowserUk!
Re^3: open(): Is there a simple way to map between numeric open modes and "symbol modes"?
by Anonymous Monk on Feb 08, 2013 at 05:09 UTC

    See also FileHandle::Fmode and Fcntl

    #!/usr/bin/perl -- use strict; use warnings; use Data::Dump; use Fcntl(); my %mode2symbol = ( #~ Fcntl::F_DUPFD() => '&', Fcntl::O_RDONLY() => '<', Fcntl::O_RDWR() => '+<', Fcntl::O_TRUNC() => '>', Fcntl::O_WRONLY() => '>', Fcntl::O_APPEND() => '>>', Fcntl::O_CREAT() => '>', ); my %name2mode = ( #~ F_DUPFD => Fcntl::F_DUPFD(), O_RDONLY => Fcntl::O_RDONLY(), O_RDWR => Fcntl::O_RDWR(), O_TRUNC => Fcntl::O_TRUNC(), O_WRONLY => Fcntl::O_WRONLY(), O_APPEND => Fcntl::O_APPEND(), O_CREAT => Fcntl::O_CREAT(), ); $mode2symbol{ $name2mode{O_RDWR} | $name2mode{O_APPEND} } = '+>>'; $mode2symbol{ $name2mode{O_RDONLY} | $name2mode{O_APPEND} } = '+>>'; $mode2symbol{ $name2mode{O_WRONLY} | $name2mode{O_APPEND} } = '+>>'; dd \%mode2symbol; dd \%name2mode; __END__ { "0" => "<", "1" => ">", "2" => "+<", "8" => "+>>", "9" => "+>>", "10" => "+>>", "256" => ">", "512" => ">", } { O_APPEND => 8, O_CREAT => 256, O_RDONLY => 0, O_RDWR => 2, O_TRUNC => 512, O_WRONLY => 1, }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1017748]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (17)
As of 2014-08-28 15:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (263 votes), past polls