Ecrire un programme qui prétend être un ATS

J’écris un programme qui lit l’entrée de stdin, manipule l’entrée et écrit la sortie sur stdout. Cependant, de nombreux programmes vérifient si stdin est un terminal ou un canal (en appelant une fonction comme isatty ) et génèrent une sortie différemment. Comment puis-je prétendre que mon programme est un ATS?

La solution devrait fonctionner sur Linux et MacOS. Tout langage de programmation qui génère un binary autonome est acceptable, mais Go est préférable.

Notez que je pose une question de programmation, ne demandant pas d’outil. Donc, des choses comme le script ou le sans unbuffer ne sont pas quelque chose que je recherche.

Ce qui suit est un code pleinement fonctionnel pour exécuter une commande dans un pty et capturer sa sortie. (Pas autant de lignes que vous avez pu le penser.)

 #include  #include  #include  #include  #include  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; } } } }