代码之家  ›  专栏  ›  技术社区  ›  Dan Hook

在Unix上的C中,进程如何在不打开文件的情况下知道它对文件有什么权限?

  •  1
  • Dan Hook  · 技术社区  · 17 年前

    我可以使用stat()来计算所有者、组或其他人拥有哪些权限,我可以使用geteuid()和getpwuid()来获取进程的用户名。不过,我不太确定如何在没有系统调用的情况下获取用户所属的组。

    3 回复  |  直到 17 年前
        1
  •  8
  •   dmeister    17 年前

    这 access() POSIX function

    例如:

    access("/etc/passwd",W_OK)
    

    检查您是否具有对passwd文件的写入权限。使用R_OK,检查读取权限。

    这 eaccess() function ( euidaccess

        2
  •  2
  •   keparo bshirley    17 年前

    unistad。H

    int access(const char *path, int amode);
    

    路径 阿莫德

    int readable, readwritable;
    
    //checking for read access
    readable = access("/usr/bin/file", R_OK);
    
    //checking for read and write access
    readwritable = access("/usr/bin/file", R_OK|W_OK);
    

    您可以在unix手册页中找到access()的完整描述。

        3
  •  1
  •   Sachin    17 年前

    W_OK测试写入权限。 X_OK测试执行或搜索权限。