test file permissions

Posted on 2006-05-13
Last Modified: 2010-04-15

This seems like it should be obvious, yet I can't find the answer anywhere.

I need to test whether or not the current process can read, write, and execute a given file.  I am not trying to display the file mode as a string (which is what all examples I've found so far show), I want to know what I can do with a file.

I've been testing read and write by opening the file and catching errors, but this seems hackish and doesn't work at all for execute permissions.

Do I really need to deconvolute the mode mask and test user, group, all in turn, by hand?  Surely this already exists somewhere.

Question by:_D_
    LVL 86

    Accepted Solution

    You can use 'access()', e.g.

    /* ACCESS.C: This example uses _access to check the
     * file named "ACCESS.C" to see if it exists and if
     * writing is allowed.

    #include  <io.h>
    #include  <stdio.h>
    #include  <stdlib.h>

    void main( void )
       /* Check for existence */
       if( (access( "ACCESS.C", 0 )) != -1 )
          printf( "File ACCESS.C exists\n" );
          /* Check for write permission */
          if( (access( "ACCESS.C", 2 )) != -1 )
             printf( "File ACCESS.C has write permission\n" );

    If you are using a MS compiler, that has to be '_access' instead.
    LVL 86

    Expert Comment

    BTW, you'll find more infoo here: ("ACCESS (2)
    check user's permissions for a file"):


          #include <unistd.h>
            int access(const char * pathname , int  mode );  


          access checks whether the process would be allowed to read, write or test for existence of the file (or other file system object) whose name is pathname . If pathname is a symbolic link permissions of the file referred to by this symbolic link are tested.

          mode is a mask consisting of one or more of R_OK , W_OK , X_OK and F_OK .

          R_OK , W_OK and X_OK request checking whether the file exists and has read, write and execute permissions, respectively. F_OK just requests checking for the existence of the file.

          The tests depend on the permissions of the directories occurring in the path to the file, as given in pathname , and on the permissions of directories and files referred to by symbolic links encountered on the way.

          The check is done with the process's real uid and gid, rather than with the effective ids as is done when actually attempting an operation. This is to allow set-UID programs to easily determine the invoking user's authority.

          Only access bits are checked, not the file type or contents. Therefore, if a directory is found to be "writable," it probably means that files can be created in the directory, and not that the directory can be written as a file. Similarly, a DOS file may be found to be "executable," but the execve (2) call will still fail.

          If the process has appropriate privileges, an implementation may indicate success for X_OK even if none of the execute file permission bits are set.


          On success (all requested permissions granted), zero is returned. On error (at least one bit in mode asked for a permission that is denied, or some other error occurred), -1 is returned, and errno is set appropriately.
    LVL 86

    Expert Comment

    Argh, better explanations: ("8.2 access: Testing File Permissions"):

    #include <errno.h>
    #include <stdio.h>
    #include <unistd.h>

    int main (int argc, char* argv[])
     char* path = argv[1];
     int rval;

     /* Check file existence. */
     rval = access (path, F_OK);
     if (rval == 0)
      printf ("%s exists\n", path);
     else {
      if (errno == ENOENT)
       printf ("%s does not exist\n", path);
      else if (errno == EACCES)
       printf ("%s is not accessible\n", path);
      return 0;

     /* Check read access. */
     rval = access (path, R_OK);
     if (rval == 0)
      printf ("%s is readable\n", path);
      printf ("%s is not readable (access denied)\n", path);

     /* Check write access. */
     rval = access (path, W_OK);
     if (rval == 0)
      printf ("%s is writable\n", path);
     else if (errno == EACCES)
      printf ("%s is not writable (access denied)\n", path);
     else if (errno == EROFS)
      printf ("%s is not writable (read-only filesystem)\n", path);
     return 0;
    LVL 24

    Expert Comment

    The longer answer it that it depends on the Platform you're using. All the answers assume that you have some "Posix" stuff available. The Windows answer to the question is:
    AccessCheck  or proper parameters to CreateFile

    LVL 22

    Expert Comment

    Note that stat and access() only tell you what the file permissions were, not what they will be when you actually try to open the file.  The only sure test is to try a file open() and see what happens.  Also consider whether you care about any lockf() or flock() or NFS locks on the file.


    Author Comment

    Of course I found access() a few minutes after posting (after days of googling).

    Thanks for the answer, and yes, this was for UNIX, and no, open() wouldn't work since testing execute was the most important one for me.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
    This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
    The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now