Finding access permissions in UNIX C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • schmals
    New Member
    • Oct 2007
    • 8

    Finding access permissions in UNIX C

    Hello,

    I am currently writing a simple shell program for UNIX and I've run into a bit of an issue. So, every file has 9 access permissions: read, write, and execute for user, group, and others. I know the stat struct contains mode_t st_mode which has the file type and access permissions encoded within it. It is easy to recover the file type from the variable because there are macro functions which will return true if the file is of that type. I need to decode the 9 access permissions from this variable though and I can't figure out how. I know there are functions such as umask() and chmod() which will set the access permissions for a current process or given file, but that doesn't really help me in my case. Basically, I just need to be educated on whether there is a function that will tell me the access permissions given the st_mode variable, or any tips to decode the access permissions from the variable itself.

    Thank you very much!!!
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    Originally posted by schmals
    Hello,

    I am currently writing a simple shell program for UNIX and I've run into a bit of an issue. So, every file has 9 access permissions: read, write, and execute for user, group, and others. I know the stat struct contains mode_t st_mode which has the file type and access permissions encoded within it. It is easy to recover the file type from the variable because there are macro functions which will return true if the file is of that type. I need to decode the 9 access permissions from this variable though and I can't figure out how. I know there are functions such as umask() and chmod() which will set the access permissions for a current process or given file, but that doesn't really help me in my case. Basically, I just need to be educated on whether there is a function that will tell me the access permissions given the st_mode variable, or any tips to decode the access permissions from the variable itself.

    Thank you very much!!!
    Check this link
    This provide some info about the mode

    raghuram

    Comment

    • RRick
      Recognized Expert Contributor
      • Feb 2007
      • 463

      #3
      Mode_t is part of the C unix API and is not directly accessed via a script. If you want to use mode_t, you need to make a C program with it and then compile and run the program.

      Shell scripts have a more direct method for retrieving this information. Try using the following shell stat command.
      Code:
      stat -c "%A" .../fileName
      This returns a string (i.e. "-rwxr-xr--") that you can parse for permissions info. There are a host of other options you can use with stat, too. On a unix box, type in man stat for more info.

      Comment

      • gpraghuram
        Recognized Expert Top Contributor
        • Mar 2007
        • 1275

        #4
        Sorry,
        I missed the pint that he is writing a shell script.

        Raghuram

        Comment

        Working...