检查是否存在的东西,并在C可执行文件,利用统计功能 [英] check if something exists and is executable in C, using stat function

查看:176
本文介绍了检查是否存在的东西,并在C可执行文件,利用统计功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有一个目录和b我当前工作目录下。我试图找到文件X,我怎么能修改 STAT()命令,使其同时检查目录和乙而不仅仅是当前的工作目录?将 STAT(A /文件,及放大器; BUF)工作?同时,要检查它是否是可执行的,我知道code是 buf.S_IXUSR ,确实如果(buf.S_IXUSR)工作

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?

谢谢!

推荐答案

我建议你咨询的 STAT(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
}

不过,对于你的使用情况,您可以考虑 接入(2) 来代替:

if (access("/path/to/file", X_OK) == 0) {
    // File exists and is executable by the calling process's
    // _real_ UID / GID.
}

这篇关于检查是否存在的东西,并在C可执行文件,利用统计功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆