管道到标准输入时使用ioctl填充winsize结构

问题描述 投票:-1回答:1

我正在尝试使用ioctl()检索终端的宽度,但在管道或重定向到标准输入时它不起作用。

我设法通过解析tput cols的结果来绕过这个问题,但是使用外部命令感觉很脏。此外,我认为这使得它不太便携,因为Windows不使用与bourne兼容的shell?

main.c中

// tput method
char res[10];

FILE cmd = popen("tput cols", "r");
fgets(res, 10 - 1, cmd);
pclose(cmd);

unsigned short term_cols = atoi(res);
printf("Term width (tput): %d\n", term_cols);

// ioctl method
struct winsize ws;
if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == 0)
{
  printf("Term width (ioctl): %d\n", ws.ws_col);
}
else
{
  printf("Failed to retrieve term width from ioctl()");
}

产量

$ bin/main  
Term width (tput): 84  
Term width (ioctl): 84
$ echo "test" | bin/main  
Term width (tput): 84  
Failed to retrieve term width from ioctl()

我在代码的开头尝试过fflush(stdin);,但它没有任何区别。这只是ioctl()的限制还是有办法绕过它?

c pipe tty ioctl
1个回答
0
投票

您可能正在打印未初始化变量的值。你的代码不检查ioctl是否成功,如果失败,它会保持ws不受影响。

固定:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

...
if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1) {
    fprintf(stderr, "can't get the window size of stdin: %s\n", strerror(errno));
    exit(EXIT_FAILURE);
}

当您将某些东西输入程序时,stdin不会引用终端而是引用管道。管道没有窗口大小。这就是TIOCGWINSZ在这里失败的原因。

便携式解决方案似乎是:

const char *term = ctermid(NULL);
if (!term[0]) {
    fprintf(stderr, "can't get the name of my controlling terminal\n");
    exit(EXIT_FAILURE);
}
int fd = open(term, O_RDONLY);
if (fd == -1) {
    fprintf(stderr, "can't open my terminal at %s: %s\n", term, strerror(errno));
    exit(EXIT_FAILURE);
}
if (ioctl(fd, TIOCGWINSZ, &ws) == -1) {
    fprintf(stderr, "can't get the window size of %s: %s\n", term, strerror(errno));
    exit(EXIT_FAILURE);
}
close(fd);
© www.soinside.com 2019 - 2024. All rights reserved.