Help with st_mode in sys/stat.h

Discussion in 'C Programming' started by Deniz Dogan, May 11, 2008.

  1. Deniz Dogan

    Deniz Dogan Guest

    Hello.

    I have a question regarding the st_mode field of the "stat" struct in
    sys/stat.h. I'd like to know how to use the S_IRWXU, S_IRWXG and
    S_IRWXO flags to mask the mode_t value into human readable form such
    as 755, 644, etc. Currently I do something similar to this:

    usr_t = (mod & S_IRWXU);

    usr_r = (mod & S_IRUSR);
    usr_w = (mod & S_IWUSR);
    usr_x = (mod & S_IXUSR);

    However, this does not produce the required result. Any help is
    appreciated.

    /Deniz Dogan
    Deniz Dogan, May 11, 2008
    #1
    1. Advertising

  2. On 11 May, 12:05, Deniz Dogan <> wrote:
    > Hello.
    >
    > I have a question regarding the st_mode field of the "stat" struct in
    > sys/stat.h. I'd like to know how to use the S_IRWXU, S_IRWXG and
    > S_IRWXO flags to mask the mode_t value into human readable form such
    > as 755, 644, etc. Currently I do something similar to this:
    >
    > usr_t = (mod & S_IRWXU);
    >
    > usr_r = (mod & S_IRUSR);
    > usr_w = (mod & S_IWUSR);
    > usr_x = (mod & S_IXUSR);
    >
    > However, this does not produce the required result. Any help is
    > appreciated.


    We only deal with standard C here. For this sort of
    question you should go to comp.unix.programmer
    Spiros Bousbouras, May 11, 2008
    #2
    1. Advertising

  3. Deniz Dogan <> wrote:
    > I have a question regarding the st_mode field of the "stat" struct in
    > sys/stat.h.


    I guess you're not aware that this isn't really a question about
    C but looks more like one about POSIX, so comp.unix.programmer
    would be more appropriate to ask in.

    > I'd like to know how to use the S_IRWXU, S_IRWXG and
    > S_IRWXO flags to mask the mode_t value into human readable form such
    > as 755, 644, etc. Currently I do something similar to this:


    > usr_t = (mod & S_IRWXU);


    > usr_r = (mod & S_IRUSR);
    > usr_w = (mod & S_IWUSR);
    > usr_x = (mod & S_IXUSR);


    > However, this does not produce the required result. Any help is
    > appreciated.


    What about something like

    mode = ( ( ( stat.st_mode & S_IRUSR ) ? 1 : 0 ) << 8 )
    | ( ( ( stat.st_mode & S_IWUSR ) ? 1 : 0 ) << 7 )
    | ( ( ( stat.st_mode & S_IXUSR ) ? 1 : 0 ) << 6 )
    | ( ( ( stat.st_mode & S_IRGRP ) ? 1 : 0 ) << 5 )
    | ( ( ( stat.st_mode & S_IWGRP ) ? 1 : 0 ) << 4 )
    | ( ( ( stat.st_mode & S_IXGRP ) ? 1 : 0 ) << 3 )
    | ( ( ( stat.st_mode & S_IROTH ) ? 1 : 0 ) << 2 )
    | ( ( ( stat.st_mode & S_IWOTH ) ? 1 : 0 ) << 1 )
    | ( ( ( stat.st_mode & S_IXOTH ) ? 1 : 0 ) << 0 );

    and then printing the result as an octal number?

    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
    Jens Thoms Toerring, May 11, 2008
    #3
  4. Deniz Dogan

    Guest

    On May 11, 2:05 pm, Deniz Dogan <> wrote:
    > Hello.
    >
    > I have a question regarding the st_mode field of the "stat" struct in
    > sys/stat.h. I'd like to know how to use the S_IRWXU, S_IRWXG and
    > S_IRWXO flags to mask the mode_t value into human readable form such
    > as 755, 644, etc. Currently I do something similar to this:
    >
    > usr_t = (mod & S_IRWXU);
    >
    > usr_r = (mod & S_IRUSR);
    > usr_w = (mod & S_IWUSR);
    > usr_x = (mod & S_IXUSR);
    >
    > However, this does not produce the required result. Any help is
    > appreciated.

    Off-topic for this newsgroup, comp.unix.programmer would be more
    appropriate, but you can certainly cast mode_t to (unsigned long) and
    use sprintf and %lo.
    , May 11, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steven T. Hatton
    Replies:
    3
    Views:
    9,706
    Pete Becker
    Jun 2, 2005
  2. electric sheep

    readdir() and S_ISDIR(stat.st_mode) question (OT ?)

    electric sheep, Mar 4, 2004, in forum: C Programming
    Replies:
    2
    Views:
    11,511
    Villy Kruse
    Mar 4, 2004
  3. John

    stat.st_mode deciphering

    John, Jun 19, 2004, in forum: C Programming
    Replies:
    1
    Views:
    727
  4. Magesh
    Replies:
    3
    Views:
    444
    Gordon Burditt
    Oct 5, 2007
  5. Rolf Krüger
    Replies:
    2
    Views:
    614
    Ian Collins
    Mar 12, 2008
Loading...

Share This Page