Decoding st_mode

This is a discussion on Decoding st_mode within the C Programming forums, part of the General Programming Boards category; Here's the problem. I'm using the stat function to get the characteristics of a file on my system. The structure ...

  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,006
    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 04: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,047
    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,047
    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, 04:06 PM
  3. Decoding
    By jk81 in forum C Programming
    Replies: 4
    Last Post: 11-12-2002, 10: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, 12:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21