gpt4 book ai didi

c - 为什么 OS X 上的 fgets 输出一个 BELL (^G, ascii : 07) character when input exceeds expected length?

转载 作者:太空狗 更新时间:2023-10-29 15:07:58 25 4
gpt4 key购买 nike

测试用例程序:

#include <stdio.h>
#define SIZE 1024
int main(int args,char *argv[]){
char buf[SIZE];
fgets(buf, SIZE, stdin);
return 0;
}

示例用法(即输入 1024 个 x 字符):

bash-3.2$ gcc read.c -o read
bash-3.2$ ./read
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
^G

在 OS X 上,这会使终端发出哔哔声并等待。我可以再次按下 RET,它会再次发出哔哔声。如果我在管道中或从 Emacs 的 shell 中运行它,我会看到正在输出 ^G 字符。

在我的 Ubuntu VM 上进行完全相同的调用,程序按预期完成(即,将 1023 字节读入 buf 并以 null 终止)。

这会中断我正在尝试编写的程序(使用另一种语言,但使用下面的 C 库),该程序将 JSON 行作为命令界面的一部分。所以问题是:如何在 OS X 中禁用此行为?是否有 API 调用来关闭它?也许是环境变量?太奇怪了。

最佳答案

您可以通过将此添加到您的 ~/.bash_profile 文件来防止此行为:

stty -imaxbel

此命令记录在 Apple manpage 中对于 stty 程序。

imaxbel (-imaxbel) The system imposes a limit of MAX_INPUT (currently 255) characters in the input queue. If imaxbel is set and the input queue limit has been reached, subsequent input causes the system to send an ASCII BEL character to the output queue (the terminal beeps at you). Otherwise, if imaxbel is unset and the input queue is full, the next input character causes the entire input and output queues to be discarded.

您还可以选择在当前 shell 中手动发出 stty -imaxbel。如果不将其添加到启动文件(如 ~/.bash_profile),该设置将在您下次登录时恢复为默认值。

如果您需要禁用行缓冲并且每个字符在键入时都传递给程序,那么一种方法是使用 stty cbreak 。它更改的主要内容之一是禁用 icanon 模式,该模式控制您是否处于缓冲线路模式。这articlecbreakicanon 进行了合理的讨论:

ICANON - Perhaps the most important bit in c_lflag is the ICANON bit. Enabling it enables "canonical" mode – also known as "line editing" mode. When ICANON is set, the terminal buffers a line at a time, and enables line editing. Without ICANON, input is made available to programs immediately (this is also known as "cbreak" mode).

您可以通过如下命令获得您正在寻找的结果:

stty cbreak -imaxbel

这将进入逐字符模式并确保禁用 BEL

关于c - 为什么 OS X 上的 fgets 输出一个 BELL (^G, ascii : 07) character when input exceeds expected length?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32672141/

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