当用于F_GETFL的fcntl命令ARG要求? [英] when is the arg for F_GETFL fcntl command required?
问题描述
int fcntl(int fd, int command, ... /* arg */ );
它是便携式:<?code>标志=的fcntl(FD,F_GETFL); (注:没有 ARG
)
的Linux 并的 FreeBSD的手册页说, ARG
被忽略了:
Both Linux and FreeBSD man pages say that arg
is ignored:
F_GETFL (void)
Get the file access mode and the file status flags; arg
is ignored.
无效
在Linux文档意味着 ARG
不是必需的。
void
in Linux documentation means that arg
is not required.
下面是从POSIX的一个相关使用例子 F_GETFD
标志:
Here's a usage example from POSIX for a related F_GETFD
flag:
#include <unistd.h>
#include <fcntl.h>
...
int flags;
flags = fcntl(fd, F_GETFD);
if (flags == -1)
/* Handle error */;
flags |= FD_CLOEXEC;
if (fcntl(fd, F_SETFD, flags) == -1)
/* Handle error */;"
这说明 ARG
不需要 F_GETFD
(今天)。然后它说:
It shows that arg
is not required for F_GETFD
(today). Then it says:
该ARG值F_GETFD,F_SETFD,F_GETFL和F_SETFL全部重新present
标志值,以便为未来的增长。
The arg values to F_GETFD, F_SETFD, F_GETFL, and F_SETFL all represent flag values to allow for future growth.
这是否意味 F_GETFL
可以使用 ARG
的未来?
Does it imply that F_GETFL
might use arg
in the future?
在Ohloh code快速搜索F_GETFL创建IM pression的大多数开源项目都通过 ARG
(通常 0
,有时 NULL
,甚至(破?)&放大器; FL
)。我不明白为什么的fcntl(FD,F_GETFL,0)
是preferred形式。 <一href=\"http://stackoverflow.com/questions/25061656/when-is-the-arg-for-f-getfl-fcntl-command-required/25062113#comment38991163_25062113\">@Wumpus问:Wumbley表明,这可能是由<一个引起href=\"http://books.google.com/books?id=kCTMFpEcIOwC&printsec=frontcover&dq=Advanced+Programming+in+the+UNIX+Environment&hl=en&sa=X&ei=9HzaU4mpJsfY4QTMioGACA&ved=0CCUQ6AEwAA#v=onepage&q=F_GETFL&f=false\"相对=nofollow>也使用的fcntl书高级UNIX环境编程(FD,F_GETFL,0)
的形式。
A quick search for "F_GETFL" on Ohloh code creates an impression that most open-source projects do pass arg
(usually 0
, sometimes NULL
, or even (broken?) &fl
). I don't understand why fcntl(fd, F_GETFL, 0)
is the preferred form. @Wumpus Q. Wumbley suggests that it might be caused by "Advanced Programming in the UNIX Environment" book that also uses fcntl(fd, F_GETFL, 0)
form.
是否有系统/编译器需要第三个参数:标志=的fcntl(FD,F_GETFL,0);
?可以通过的fcntl(FD,F_GETFL)
和的fcntl(FD,F_GETFL,0)
产生不同的结果今天或将来(假设兼容的实现)?
Is there a system/compiler that requires the 3rd arg: flags = fcntl(fd, F_GETFL, 0);
? Can fcntl(fd, F_GETFL)
and fcntl(fd, F_GETFL, 0)
produce different results today or in the future (assuming a compliant implementation)?
推荐答案
看的fcntl命令的其余部分。请注意其中的一些(F_DUPFD,F_SETFL,及其他)如何告诉你的第三个参数的用途。您需要使用其中的一个时提供第三个参数。不使用F_GETFL或F_GETFD时。
Look at the rest of the fcntl commands. Notice how some of them (F_DUPFD, F_SETFL, and others) tell you what the third arg is used for. You need to provide the third arg when using one of those. Not when using F_GETFL or F_GETFD.
在概要您可以看到的fcntl需要2 ARGS加上 ...
这意味着可以省略第三个参数时,它不会被使用。
In the SYNOPSIS you can see that the fcntl takes 2 args plus a ...
which means the third arg can be omitted when it's not going to be used.
在做一些调查研究后,我发现有一些老人页(来自各地的第一APUE的时间),其中概要意味着要求所有3个参数。例如:的http://www.freebsd.org/cgi/man.cgi?query=fcntl&manpath=FreeBSD+2.2.7-RELEASE
After doing some more research, I found that there are some old man pages (from around the time of the first APUE) in which the SYNOPSIS implies that all 3 arguments are required. Example: http://www.freebsd.org/cgi/man.cgi?query=fcntl&manpath=FreeBSD+2.2.7-RELEASE
SYNOPSIS
#include <fcntl.h>
int
fcntl(int fd, int cmd, int arg);
我找不到任何证据证明它是以往任何时候居然宣称在标题的方式,但如果是,那么当它只有2个参数调用编译会失败。这将是一个很好的理由,包括你的code多余0的说法。
I can't find any evidence that it was ever actually declared that way in the header, but if it was, then compilation would fail when it was called with only 2 arguments. That would be a good reason to include the extra 0 argument in your code.
如果我的猜测是正确的,这是对历史使用3-ARG F_GETFL的实际原因则是指从函数原型是新的,可怕的,操作系统厂商都拿到他们错了一段时间没用化石。
If my guess is correct and this is the actual reason for the historical use of 3-arg F_GETFL then it is a useless fossil from a time when function prototypes were new and scary and OS vendors were getting them wrong.
这篇关于当用于F_GETFL的fcntl命令ARG要求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!