gpt4 book ai didi

c - 当 POSIX 说 stderr 应该可以读写时是什么意思?

转载 作者:行者123 更新时间:2023-12-03 15:48:08 25 4
gpt4 key购买 nike

POSIX 的页面 stderr, stdin, stdout - standard I/O streams说这个:

The stderr stream is expected to be open for reading and writing.



“预期”有多强?违反它是未定义的行为吗?它是谁的责任,系统的还是应用程序的?

考虑这个程序:
#include <stdio.h>

int main(void) {
printf("feof is %d and ferror is %d\n", feof(stderr), ferror(stderr));
printf("fgetc is %d\n", fgetc(stderr));
printf("feof is %d and ferror is %d\n", feof(stderr), ferror(stderr));
}

当我在不重定向 stderr 的情况下运行它时(所以它就像 stdin 一样指向我的终端),它会立即输出它而不等待任何输入:
feof is 0 and ferror is 0
fgetc is -1
feof is 0 and ferror is 1

这是否意味着我的系统不符合 POSIX 标准?

另外,如果这是我的责任,那么假设我有一个具有权限的文件 620 ,而且我在组中但不是所有者。这是否意味着 someprogram 2>saidfile是未定义的行为,因为在这种情况下无论如何您都无法从 stderr 读取?

最佳答案

Austin Group (维护 POSIX 标准的 union 工作组)讨论了 defect that was reported about the "expected to be" wording在 2020-12-07 和 2020-12-10 电话 session 期间,并同意 POSIX Issue 7(2018 版)中的措辞有问题。 POSIX 标准的下一版本中的措辞将更改如下(从 bug comment 复制):

On page 65 before line 1912 (XBD "interactive shell") insert a new definition and renumber the remaining:

Interactive Device

A terminal device.

Note: This definition is intended to align with the ISO C standard's use of "interactive device".


第 496 页第 17224-17228 页(XSH 2.5 标准 I/O 流)和第 2017 页第 64723-64727 页(XSH stderr、stdin、stdout 说明)更改:

At program start-up, three streams shall be predefined and need not be opened explicitly: standard input (for reading conventional input), standard output (for writing conventional output), and standard error (for writing diagnostic output). When opened, the standard error stream is not fully buffered; the standard input and standard output streams are fully buffered if and only if the stream can be determined not to refer to an interactive device.


到:

At program start-up, three streams shall be predefined and already open: stdin (standard input, for conventional input) for reading, stdout (standard output, for conventional output) for writing, and stderr (standard error, for diagnostic output) for writing. When opened, stderr shall not be fully buffered; stdin and stdout shall be fully buffered if and only if [CX]the file descriptor associated with the stream is determined not to be associated with an interactive device.[/CX]


在第 2017 页第 64733 行(XSH 标准输入说明)上,更改:

The stderr stream is expected to be open for reading and writing.


到:

These file descriptors are often all associated with a single open file description which has access mode O_RDWR (e.g., in the case of a terminal device for a login shell). However, the stderr, stdin, and stdout streams need not be opened for both reading and writing at program start-up in this case.


在第 2017 页第 64747 行(标准输入另见)添加 isatty()。

特别感谢 Rich Felker 报告该缺陷。

关于c - 当 POSIX 说 stderr 应该可以读写时是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62052909/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com