JatiA
JatiA

Reputation: 81

In a C program in Linux OS, how to check if a user has read permission on a file?

The function would be something like this.

int GetFilePermission(char* pcUsername, char* pcFilePath)
{
    /*return TRUE if 'pcUsername' has read permission on file 'pcFilePath'.*/
}

I don't want to use stat(). Since it would be little bit long. I have to check file's permissions, it's owners and compare them in different combinations. Is there any short and simple way/trick of doing this?

Please help. Thanks in advance.

Upvotes: 1

Views: 3374

Answers (2)

Thushi
Thushi

Reputation: 188

You can use stat

Prototype for stat

#include <sys/stat.h>
int stat(const char *restrict path, struct stat *restrict buf);

Upvotes: -1

Karthikeyan.R.S
Karthikeyan.R.S

Reputation: 4041

Using the access function we can get the user having a permission. See the man page of access.

int access(const char *pathname, int mode);

If you stat function, you getting all information about that file. In access we have these four only. R_OK, W_OK, X_OK and F_OK. Using this we can get easily. Return value is zero if success.

R_OK = read permission
W_OK = Write permission
X_OK = Execute permission
F_OK = file is existing.

Upvotes: 2

Related Questions