失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > linux chmod和fchmod设置 文件和目录权限设置

linux chmod和fchmod设置 文件和目录权限设置

时间:2022-12-24 18:01:31

相关推荐

linux chmod和fchmod设置 文件和目录权限设置

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

#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是表4-8中所示常量的某种按位或运算构成的。

表4-8 chmod函数的mode常量,取自<sys/stat.h>

程序清单4-4 chmod函数实例

#include "apue.h"int main(void){struct statstatbuf;/* turn on set-group-ID and turn off group-execute */if(stat("foo", &statbuf) < 0)err_sys("stat error for foo");if(chmod("foo", (statbuf.st_mode & ~S_IXGRP) | S_ISGID) < 0)err_sys("chmod error for foo");/* set absolute mode to "rw-r--r--" */if(chmod("bar", S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0)err_sys("chmod error for bar");exit(0);}

如果觉得《linux chmod和fchmod设置 文件和目录权限设置》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。