open() 函数参数 [英] open() function parameters

查看:69
本文介绍了open() 函数参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您通过考虑最后一个参数0"来查看下面的代码块,写行是否正常工作?

If you look at this code block below by taking into consideration the last parameter "0", Does write line work properly ?

filename = argv[1];
string = "Example string";
if (stat(argv[1], &buf) != 0)
{
    fd = open(filename, O_WRONLY | O_CREAT, 0);
    if (fd < 0)
    {
        perror(filename);
        exit(1);
    }
    write(fd, string, strlen(string));
    close(fd);
}
else
{
    print("%s file exists\n", filename);
}

推荐答案

来自手册页:

mode 指定在创建新文件时使用的权限.在标志中指定 O_CREAT 时必须提供此参数;如果未指定 O_CREAT,则忽略 mode.进程的 umask 以通常的方式修改有效权限:创建的文件的权限是 (mode & ~umask).请注意,此模式仅适用于新创建的文件的未来访问;创建只读文件的 open() 调用很可能会返回一个读/写文件描述符.

mode specifies the permissions to use in case a new file is created. This argument must be supplied when O_CREAT is specified in flags; if O_CREAT is not specified, then mode is ignored. The effective permissions are modified by the process's umask in the usual way: The permissions of the created file are (mode & ~umask). Note that this mode applies only to future accesses of the newly created file; the open() call that creates a read-only file may well return a read/write file descriptor.

为模式提供了以下符号常量:

The following symbolic constants are provided for mode:

S_IRWXU  00700 user (file owner) has read, write and execute permission
S_IRUSR  00400 user has read permission
S_IWUSR  00200 user has write permission
S_IXUSR  00100 user has execute permission
S_IRWXG  00070 group has read, write and execute permission
S_IRGRP  00040 group has read permission
S_IWGRP  00020 group has write permission
S_IXGRP  00010 group has execute permission
S_IRWXO  00007 others have read, write and execute permission
S_IROTH  00004 others have read permission
S_IWOTH  00002 others have write permission
S_IXOTH  00001 others have execute permission

因此,将 mode 指定为零,您将创建一个文件,其权限为 0 &~umask,即一个没有任何权限的文件.

So, specifying a mode of zero, you will create a file with the permissions of 0 & ~umask, i.e. a file without any permissions.

文件系统的具体内容不在 open()write() 函数的范围内.

What exactly the filesystem makes of this is not in the domain of the open() or write() functions.

这篇关于open() 函数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆