Thread: Decoding st_mode

  1. #1
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625

    Decoding st_mode

    Here's the problem. I'm using the stat function to get the characteristics of a file on my system. The structure has a st_mode entry which is supposed to hold the permission parameters of the given file. But it is given in the form of an integer, and I need to put it in the form of a string (aka "frwx------"). My question is how can we find, from the st_mode entry, whether a file is readable by the user, writable by the user, etc...

    Thanks for the help.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Check for the bit masks S_IFCHR, S_IFREG, S_IWRITE, S_IREAD, etc.

    [edit]man stat for relevant bit masks.
    Last edited by Dave_Sinkula; 02-08-2006 at 05:07 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Once you have the constant you want, AND it with fstat's st_mode:
    Code:
    if(st.st_mode & S_IREAD) {
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    If you don't have man, look at this page: http://www.wlug.org.nz/fstat(2)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with decoding program...
    By porsche911nfs in forum C++ Programming
    Replies: 18
    Last Post: 04-10-2009, 12:21 AM
  2. Help decoding base_64+GZipped string
    By Zeokat in forum C# Programming
    Replies: 3
    Last Post: 12-05-2008, 05:06 PM
  3. Decoding
    By jk81 in forum C Programming
    Replies: 4
    Last Post: 11-12-2002, 11:24 PM
  4. Decoding & Encoding the sound?!?
    By waldis in forum C++ Programming
    Replies: 2
    Last Post: 10-20-2002, 06:41 PM
  5. Decoding in arrays
    By stimpyzu in forum C++ Programming
    Replies: 6
    Last Post: 04-06-2002, 01:21 PM