chmod 与fchmod函数

#include <sys/stat.h>
int chmod(const char *pathname, mode_t mode);

int fchmod(int filedes, mode_t mode);


fchmod()会依参数mode权限来更改参数fildes所指文件的权限。参数fildes为已打开文件的文件描述词。


参数mode请参考chmod()。

权限改变成功则返回0,失败返回-1,错误原因存于errno。


EBADF 参数fildes为无效的文件描述词。
EPERM 进程的有效用户识别码与欲修改权限的文件所有者不同,而且也不具root权限。
EROFS 欲写入权限的文件存在于只读文件系统内。
EIO I/O存取错误。



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


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


#include<sys/stat.h>
#include<fcntl.h>
main()
{
int fd;
fd = open (“/etc/passwd”,O_RDONLY);
fchmod(fd,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
close(fd);
}


你可能感兴趣的:(linux,unix,linux命令,c函数)