APUE-umask&chmod

来源:互联网 发布:php扩展加密 编辑:程序博客网 时间:2024/06/16 22:21

umask示例4-9:

#include "apue.h"#include <fcntl.h>#define RWRWRW (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)int main(void){    umask(0);    if (creat("foo", RWRWRW) < 0)        err_sys("creat error for foo");    umask(S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);    if (creat("bar", RWRWRW) < 0)        err_sys("creat error for bar");    exit(0);}

编译运行后,结果为

这里写图片描述

在上4-9的基础上继续进行chmod示例

#include "apue.h"int main(void) {    struct stat statbuf;    /* 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);}

编译运行后,结果为

这里写图片描述

函数先调用stat获得其当前权限,然后修改它,以上操作打开了设置组ID位、关闭了组执行位。当前foo文件组执行权限为S大写,表示设置组ID位已经设置,同时,组执行位未设置。

原创粉丝点击