dchwlinux 于 2011-07-22 11:52:52发表:
具体怎么做我不知道,但是那个提示的意思就是写一个程序,使新建的文件umask值为0266,这样就可以达到题目要求了,就这么简单。这么具有指向性的提示楼主竟然不懂?
siasleo 于 2011-07-20 15:04:53发表:
看不懂就去恶补下吧~~~
deepwhite 于 2011-07-19 17:50:16发表:
[code]#include #include #include #include #include #include #include #define PERM S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH #define FLAG O_RDWR|O_CREAT int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "Usage: %s filename\n", argv[0]); return 1; } if (access(argv[1], F_OK) == 0) { fprintf(stderr, "ERROR: file %s existed, nothing to do!\n", argv[1]); return 1; } umask(0266); int fd = open(argv[1], FLAG, PERM); if (fd == -1) { perror("Failed to open file, reason: "); exit(1); } close(fd); return 0; } [/code]Suggestions: 1. Read Manual. 2. Read APUE.
zhangzhaoi 于 2011-07-19 17:27:13发表:
8#
taffy5366 于 2011-07-19 16:06:29发表:
int open(const char *pathname, int flags, mode_t mode); int creat(const char *pathname, mode_t mode); man open man creat
jayee 于 2011-07-19 15:17:40发表:
4# hstking 表示看不懂啊,能解释得清楚一点吗,谢谢
jayee 于 2011-07-19 15:17:11发表:
5# gogo11 表示看不懂啊,能解释得清楚一点吗,谢谢
gogo11 于 2011-07-19 10:29:20发表:
umask与chmod的效果刚好相反,umask设置的是权限“补码”,而chmod设置的是文件权限码。那umask0266不就相当于chmod7511,但系统不允许新建文件有x权限,那就相当于7400,对不对?
hstking 于 2011-07-19 08:48:51发表:
如果按照楼主的思路。 既然调用了touch,那不如再调用个chmod好了。 只是题目明显提示了注意umask,那就绝了这种取巧的法子。
hstking 于 2011-07-19 08:46:41发表:
这个题目的目的是用creat或者是open函数创建一个文件。
jayee 于 2011-07-18 21:38:55发表:
我是这样编的,不知道对不对? #include #include #include #include int main() { mode_t new_umask,old_umask; new_umask=0400; old_umask=umask(new_umask); printf("系统原来的权限掩码是:%o\n",old_umask); printf("系统新的权限掩码是:%o\n",new_umask); system("touch file\n"); printf("创建了新文件file\n"); } 然后运行所出现的结果如下系统原来的权限掩码是:22 系统新的权限掩码是:400 创建了新文件file 但是不明白题目所说的“提示umask中的参数设置为0266”是什么意思
dchwlinux 于 2011-07-22 11:52:52发表:
具体怎么做我不知道,但是那个提示的意思就是写一个程序,使新建的文件umask值为0266,这样就可以达到题目要求了,就这么简单。这么具有指向性的提示楼主竟然不懂?
siasleo 于 2011-07-20 15:04:53发表:
看不懂就去恶补下吧~~~
deepwhite 于 2011-07-19 17:50:16发表:
[code]#include
#include
#include
#include
#include
#include
#include
#define PERM S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH
#define FLAG O_RDWR|O_CREAT
int main(int argc, char **argv)
{
if (argc != 2) {
fprintf(stderr, "Usage: %s filename\n", argv[0]);
return 1;
}
if (access(argv[1], F_OK) == 0) {
fprintf(stderr, "ERROR: file %s existed, nothing to do!\n", argv[1]);
return 1;
}
umask(0266);
int fd = open(argv[1], FLAG, PERM);
if (fd == -1) {
perror("Failed to open file, reason: ");
exit(1);
}
close(fd);
return 0;
}
[/code]Suggestions:
1. Read Manual.
2. Read APUE.
zhangzhaoi 于 2011-07-19 17:27:13发表:
8#
taffy5366 于 2011-07-19 16:06:29发表:
int open(const char *pathname, int flags, mode_t mode);
int creat(const char *pathname, mode_t mode);
man open
man creat
jayee 于 2011-07-19 15:17:40发表:
4# hstking
表示看不懂啊,能解释得清楚一点吗,谢谢
jayee 于 2011-07-19 15:17:11发表:
5# gogo11
表示看不懂啊,能解释得清楚一点吗,谢谢
gogo11 于 2011-07-19 10:29:20发表:
umask与chmod的效果刚好相反,umask设置的是权限“补码”,而chmod设置的是文件权限码。那umask0266不就相当于chmod7511,但系统不允许新建文件有x权限,那就相当于7400,对不对?
hstking 于 2011-07-19 08:48:51发表:
如果按照楼主的思路。
既然调用了touch,那不如再调用个chmod好了。
只是题目明显提示了注意umask,那就绝了这种取巧的法子。
hstking 于 2011-07-19 08:46:41发表:
这个题目的目的是用creat或者是open函数创建一个文件。
jayee 于 2011-07-18 21:38:55发表:
我是这样编的,不知道对不对?
#include
#include
#include
#include
int main()
{
mode_t new_umask,old_umask;
new_umask=0400;
old_umask=umask(new_umask);
printf("系统原来的权限掩码是:%o\n",old_umask);
printf("系统新的权限掩码是:%o\n",new_umask);
system("touch file\n");
printf("创建了新文件file\n");
}
然后运行所出现的结果如下系统原来的权限掩码是:22
系统新的权限掩码是:400
创建了新文件file
但是不明白题目所说的“提示umask中的参数设置为0266”是什么意思