How to trick an application into thinking its stdout is a terminal, not a pipe

前端 未结 9 1483
梦如初夏
梦如初夏 2020-11-22 11:58

I\'m trying to do the opposite of \"Detect if stdin is a terminal or pipe?\".

I\'m running an application that\'s changing its output format because it detects a pip

9条回答
  •  失恋的感觉
    2020-11-22 12:29

    I don't know if it's doable from PHP, but if you really need the child process to see a TTY, you can create a PTY.

    In C:

    #include 
    #include 
    #include 
    #include 
    #include 
    
    int main(int argc, char **argv) {
        int master;
        struct winsize win = {
            .ws_col = 80, .ws_row = 24,
            .ws_xpixel = 480, .ws_ypixel = 192,
        };
        pid_t child;
    
        if (argc < 2) {
            printf("Usage: %s cmd [args...]\n", argv[0]);
            exit(EX_USAGE);
        }
    
        child = forkpty(&master, NULL, NULL, &win);
        if (child == -1) {
            perror("forkpty failed");
            exit(EX_OSERR);
        }
        if (child == 0) {
            execvp(argv[1], argv + 1);
            perror("exec failed");
            exit(EX_OSERR);
        }
    
        /* now the child is attached to a real pseudo-TTY instead of a pipe,
         * while the parent can use "master" much like a normal pipe */
    }
    

    I was actually under the impression that expect itself does creates a PTY, though.

提交回复
热议问题