0

For example, I have directory a and b under my current working directory. I'm trying to locate file X, how can I modify the stat() command so that it checks both directory a and b instead of just current working directory? would stat(a/file, &buf) work? also, to check if it's executable, I know the code is buf.S_IXUSR, does if (buf.S_IXUSR) work?

thanks!

1
  • yes I have, and I couldn't figure out how to get it to browse different directories, that's why I asked :/ Commented Feb 28, 2016 at 22:58

1 Answer 1

2

I suggest you consult the stat(2) man page.

Here's an example of how to use stat:

struct stat buf;

if (stat("a/file", &buf) != 0) {
    // handle failure
}

// Check the `st_mode` field to see if the `S_IXUSR` bit is set
if (buf.st_mode & S_IXUSR) {
    // Executable by user
}

However, for your use case, you might consider access(2) instead:

if (access("/path/to/file", X_OK) == 0) {
    // File exists and is executable by the calling process's
    // _real_ UID / GID.
}
Sign up to request clarification or add additional context in comments.

6 Comments

ahh i see! i was browsing man stat, didn't know there's man 2 stat, thanks!
hi again! I got everything to work for current directory, but I'm still strugling to get it to work for sub directories such as dir a and b, stat("a/file", &buf) doesn't seem to work since it always says no such file, even if the file is there
Is a a subdirectory of the program's current working directory?
yes it is, I'm using char d1[50] strcat(d1, "a/") strcat(d1, "file") to generate the string "a/file" since file is from user input, however every time I try to read d1, it gives me @#!a/file, no idea where those symbols came from
Use strcpy(d1, "a/").
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.