0

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.

JatiA
  • 73
  • 1
  • 4
  • 12
  • Probably a dupe of http://stackoverflow.com/questions/3512434/using-struct-stat – Paulo Scardine Dec 18 '14 at 11:11
  • 1
    Check also related functions linked from the [`stat` man page](http://linux.die.net/man/2/stat). – Paulo Scardine Dec 18 '14 at 11:20
  • for a dozen helper functions, see the [boost filesystem library](http://www.boost.org/doc/libs/1_57_0/libs/filesystem/doc/reference.html) – Paulo Scardine Dec 18 '14 at 11:27
  • Another dupe: http://stackoverflow.com/questions/3799053/check-for-writing-permissions-to-file-in-windows-linux – Paulo Scardine Dec 18 '14 at 11:29
  • My program is running as a root. But I want to check whether a user (e.g. david) has a read permission on the file. fopen() returns the permissions w.r.t. to the calling process. And I can do it using stat(), but obviously it would be a little bit long. I thought there would be some very effective trick. Probably my thought was wrong. – JatiA Dec 18 '14 at 11:37
  • Yes, curiously seems like there is no popular higher level approach. Like in the old joke "How Programmers Hunt Elephants": C programmers refuse to buy rifles off the shelf, preferring to take steel rods and a mobile machine shop to Africa intending to build the perfect rifle for the job from scratch while everyone else bags the elephants. – Paulo Scardine Dec 18 '14 at 12:12

2 Answers2

1

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.
Karthikeyan.R.S
  • 3,991
  • 1
  • 19
  • 31
  • 2
    My program is running as a root. But I want to check whether a user (e.g. david) has a read permission on the file. access() returns the permissions w.r.t. to the calling process. – JatiA Dec 18 '14 at 11:21
  • @JatiA If the root is the owner of the file then check the other and group permission of that file. It can be done with the stat function. – Karthikeyan.R.S Dec 18 '14 at 11:53
  • If your program is running as root is easy to impersonate the user in question and just try to open the file while capturing exceptions (this EAFP approach is very portable). – Paulo Scardine Dec 18 '14 at 19:01
-1

You can use stat

Prototype for stat

#include <sys/stat.h>
int stat(const char *restrict path, struct stat *restrict buf);
Thushi
  • 188
  • 1
  • 13
  • OP specifically said they don't want to use stat – nerdguy May 14 '23 at 23:54
  • @nerdguy Looks like they edited the question after the answer. You can see the history. In the original question there was no mention of it. They updated the question after 4 minutes from my answer. Sad! – Thushi May 17 '23 at 08:42