Help with st_mode in sys/stat.h - Unix

This is a discussion on Help with st_mode in sys/stat.h - Unix ; 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, ...

+ Reply to Thread
Results 1 to 5 of 5

Thread: Help with st_mode in sys/stat.h

  1. Help with st_mode in sys/stat.h

    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

  2. Re: Help with st_mode in sys/stat.h

    Deniz Dogan writes:
    > 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.


    While I have no idea what your 'desired result' is, using
    mode & 07777 should mask out all 'non-permission' bits (I do not quite
    see the point of replacing 'arcane numbers' with 'cryptic
    abbreviations' [which are longer] ...).


  3. Re: Help with st_mode in sys/stat.h

    On 11 Maj, 13:40, Rainer Weikusat wrote:
    > Deniz Dogan writes:
    > > 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.

    >
    > While I have no idea what your 'desired result' is, using
    > mode & 07777 should mask out all 'non-permission' bits (I do not quite
    > see the point of replacing 'arcane numbers' with 'cryptic
    > abbreviations' [which are longer] ...).


    My desired output is that given a mode_t representing a 755 file,
    usr_t is 7 and usr_r, usr_w and usr_x are all 1.

  4. Re: Help with st_mode in sys/stat.h

    Deniz Dogan wrote:
    > On 11 Maj, 13:40, Rainer Weikusat wrote:
    > > Deniz Dogan writes:
    > > > 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.

    > >
    > > While I have no idea what your 'desired result' is, using
    > > mode & 07777 should mask out all 'non-permission' bits (I do not quite
    > > see the point of replacing 'arcane numbers' with 'cryptic
    > > abbreviations' [which are longer] ...).


    > My desired output is that given a mode_t representing a 755 file,
    > usr_t is 7 and usr_r, usr_w and usr_x are all 1.


    You're already near to the solution. If you follow Rainers
    advice you would do

    usr_t = ( mod & 0700 ) >> 6;

    usr_r = ( mod & 0400 ) >> 8;
    usr_w = ( mod & 0200 ) >> 7;
    usr_x = ( mod & 0100 ) >> 6;

    I am not totally convinced that this is the "best" way since
    it assumes that you know how the bits in the value of st_mode
    as returned by stat() encode the permission settings. While
    it will probably work the way Rainer writes on most systems,
    I am not 100% sure that it has to work on all systems, so I
    personally would prefer (I am also not a big fan of "magic"
    numbers in programs):

    usr_r = ( mod & S_IRUSR ) ? 1 : 0;
    usr_w = ( mod & S_IWUSR ) ? 1 : 0;
    usr_x = ( mod & S_IXUSR ) ? 1 : 0;

    usr_t = ( usr_r << 2 ) | ( usr_w << 1 ) | usr_x;

    The bits for the group and others can be treated in exactly
    the same way.
    Regards, Jens
    --
    \ Jens Thoms Toerring ___ jt@toerring.de
    \__________________________ http://toerring.de

  5. Re: Help with st_mode in sys/stat.h

    On 11 Maj, 13:56, j...@toerring.de (Jens Thoms Toerring) wrote:
    > Deniz Dogan wrote:
    > > On 11 Maj, 13:40, Rainer Weikusat wrote:
    > > > Deniz Dogan writes:
    > > > > 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.

    >
    > > > While I have no idea what your 'desired result' is, using
    > > > mode & 07777 should mask out all 'non-permission' bits (I do not quite
    > > > see the point of replacing 'arcane numbers' with 'cryptic
    > > > abbreviations' [which are longer] ...).

    > > My desired output is that given a mode_t representing a 755 file,
    > > usr_t is 7 and usr_r, usr_w and usr_x are all 1.

    >
    > You're already near to the solution. If you follow Rainers
    > advice you would do
    >
    > usr_t = ( mod & 0700 ) >> 6;
    >
    > usr_r = ( mod & 0400 ) >> 8;
    > usr_w = ( mod & 0200 ) >> 7;
    > usr_x = ( mod & 0100 ) >> 6;
    >
    > I am not totally convinced that this is the "best" way since
    > it assumes that you know how the bits in the value of st_mode
    > as returned by stat() encode the permission settings. While
    > it will probably work the way Rainer writes on most systems,
    > I am not 100% sure that it has to work on all systems, so I
    > personally would prefer (I am also not a big fan of "magic"
    > numbers in programs):
    >
    > usr_r = ( mod & S_IRUSR ) ? 1 : 0;
    > usr_w = ( mod & S_IWUSR ) ? 1 : 0;
    > usr_x = ( mod & S_IXUSR ) ? 1 : 0;
    >
    > usr_t = ( usr_r << 2 ) | ( usr_w << 1 ) | usr_x;
    >
    > The bits for the group and others can be treated in exactly
    > the same way.
    > Regards, Jens
    > --
    > \ Jens Thoms Toerring ___ j...@toerring.de
    > \__________________________ http://toerring.de


    Thanks alot for your help!

+ Reply to Thread