chmod和fchmod函数 /chown ,fchown,lchown函数

这两个函数使我们可以更改现有文件的访问权限:

#include <sys/stat.h>
int chmod( const char *pathname, mode_t mode );
int fchmod( int filedes, mode_t mode );
两个函数返回值:若成功则返回0,若出错则返回-1

 

chmod函数在指定的文件上进行操作,而fchmod函数则对已打开的文件进行操作。

为了改变一个文件的权限位,进程的有效用户ID必须等于文件的所有者ID,或者该进程必须具有超级用户权限。

参数mode所示常量的某种按位或运算构成的

mode 说明
S_ISUID 
S_ISGID 
S_ISVTX
执行时设置用户ID 
执行时设置组ID 
保存正文(粘住位)
S_IRWXU 
     S_IRUSR 
     S_IWUSR 
     S_IXUSR
用户(所有者)读、写和执行 
用户(所有者)读 
用户(所有者)写 
用户(所有者)执行
S_IRWXG 
     S_IRGRP 
     S_IWGRP 
     S_IXGRP
组读、写和执行 
组读 
组写 
组执行
S_IRWXO 
     S_IROTH 
     S_IWOTH 
     S_IXOTH
其他读、写和执行 
其他读 
其他写 
其他执行

 

 

 

 

 

 

 

 

 

 

 

 

 

 

chown函数可用于更改文件的用户ID和组ID:

#include <unistd.h>
int chown( const char *pathname, uid_t owner, gid_t group );
int fchown( int filedes, uid_t owner, gid_t group );
int lchown( const char *pathname, uid_t owner, gid_t group );
                   三个函数的返回值:若成功则返回0,若出错则返回-1

 

原文地址:https://www.cnblogs.com/hezhangyear/p/4026872.html