加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

当输入超出预期长度时,为什么OS X上的fgets输出BELL(^ G,ascii:

发布时间:2020-12-14 19:09:02 所属栏目:百科 来源:网络整理
导读:测试用例程序: #include stdio.h#define SIZE 1024int main(int args,char *argv[]){ char buf[SIZE]; fgets(buf,SIZE,stdin); return 0;} 用法示例(即输入1024 x个字符): bash-3.2$gcc read.c -o readbash-3.2$./readxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
测试用例程序:

#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并将其终止).

这正在打断我正在尝试编写的程序(使用另一种语言,但使用下面的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模式,该模式控制您是否处于缓冲线模式.这个article在cbreak和icanon的非专业术语中进行了合理的讨论:

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被禁用.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读