c - 编写伪装成 TTY 的程序

标签 c linux macos go tty

我正在编写一个程序,该程序从标准输入读取输入、操作输入并将输出写入标准输出。然而,许多程序检查标准输入是终端还是管道(通过调用类似 isatty 的函数),并以不同的方式生成输出。如何让我的程序伪装成 TTY?

该解决方案应该适用于 Linux 和 macOS。任何生成独立二进制文件的编程语言都是可接受的,但首选 Go。

请注意,我问的是编程问题,而不是工具。所以,像 scriptunbuffer 这样的东西不是我想要的。

最佳答案

以下是在 pty 中运行命令并捕获其输出的完整工作代码。 (没有你想象的那么多行。)

#include <signal.h>
#include <stdlib.h>
#include <sysexits.h>
#include <unistd.h>
#include <util.h>

pid_t child = 0;

void sighandler(int signum) {
  if (child > 0) {
    killpg(child, signum);
    exit(signum);
  }
}

// Run a command in a pty.
// Usage: /path/to/this/binary command to run
int main(int argc, char *argv[]) {
  if (argc < 2) {
    return EX_USAGE;
  }

  int master;
  child = forkpty(&master, NULL, NULL, NULL);

  if (child == -1) {
    perror("failed to fork pty");
    return EX_OSERR;
  }

  if (child == 0) {
    // we're in the child process, so replace it with the command
    execvp(argv[1], argv + 1);
    perror("failed to execute command");
    return EX_OSERR;
  }

  // trap kill signals and forward them to child process
  signal(SIGHUP, sighandler);
  signal(SIGINT, sighandler);
  signal(SIGTERM, sighandler);

  const int buf_size = 1024;
  char buf[buf_size];
  fd_set fds;
  ssize_t bytes_read;

  // forward the output continuously
  while (1) {
    FD_ZERO(&fds);
    FD_SET(master, &fds);

    if (select(master + 1, &fds, NULL, NULL, NULL) > 0 && FD_ISSET(master, &fds)) {
      bytes_read = read(master, buf, buf_size);
      if (bytes_read <= 0) {
        return EXIT_SUCCESS;
      }

      if (write(STDOUT_FILENO, buf, bytes_read) != bytes_read) {
        perror("failed to write to stdout");
        return EX_OSERR;
      }
    }
  }
}

关于c - 编写伪装成 TTY 的程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40067878/

相关文章:

c++ - 意外的 undefined reference

c - 为什么我的空指针检查在 C 中不起作用?

c - 调用静态库函数时出现段错误

C Socket Server,内存随文件写入而增加

java - Eclipse - JAR 创建失败 "Class files on classpath not found or not accessible for..."

cocoa - 在 MAC OS X 上用 1 根手指向右/向左滑动

c - 有没有办法强制 HTTP 服务器通过 libcurl 向我发送特定 header ?

java - 使用 java.library.path 和 LD_LIBRARY_PATH 的区别

Ruby 检查 webdav 上传是否完成

android - 在 Android 平板电脑上查看 SQLite 数据 - SQLite 数据库浏览器