“T恤”就像 UNIX 中的编程 api

发布于 2024-10-31 23:56:55 字数 2343 浏览 4 评论 0原文

拥有类似的东西

$> ps -ax | tee -a processes.txt

我想在 UNIX C 编程环境中

,这意味着不通过 shell 脚本。基本上有一个 API,以便我可以在 STDIN 和/或 STDOUT 上进行操作,以便我可以自动将 CLI 上出现的任何内容记录到文件中。想象一下,有一个前台进程与用户交互并响应一些输出到终端。我希望将终端中显示的所有内容也保存在一个文件中以供以后检查。

我会想象这样神奇的事情:

tee(STDIN, "append", logFile);

谢谢!

后续,这是我根据Lars编写的程序(见下面的回答部分),但不完全是我想要的:

int main(int argc, char** argv){

        int pfd[2];
        if (pipe(pfd) == -1) { perror("pipe"); }


        if (fork()==0) { // child reads from pipe
                close(pfd[1]);

                mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
                int ufd = open("user_study.log", O_CREAT | O_APPEND | O_WRONLY, mode);
                if (ufd == -1) {
                        perror("Cannot open output file"); exit(1);
                } // ++

                char buf;
                while (read(pfd[0], &buf, 1) > 0) {
                        write(STDOUT_FILENO, &buf, 1); // write to stdout
                        write(ufd, &buf,1);            //   and to log
                }

                close(pfd[0]);

        } else { // parent write to pipe
                // dup STDOUT to the pipe
                close(pfd[0]);
                dup2(pfd[1], STDOUT_FILENO);

                char msg[200];
                msg[0] = "";
                do {
                        scanf("%s", msg);
                        printf("program response..");

                } while (strcmp(msg, "exit")!=0);

                close(pfd[1]);

        }

        return 1;
}

实际运行:

[feih@machine ~/mytest]$ ./a.out
abc
haha
exit 
program response..   <---- the output is delayed by the chld process, not desired
program response..
program response..

[feih@machine ~/mytest]$ less user_study.log
program response..   <---- the log doesn't contain input
program response..
program response..

期望的运行和日志(模拟):

[feih@machine ~/mytest]$ ./a.out
abc
program response..
haha
program response..
exit 
program response..


[feih@machine ~/mytest]$ less user_study.log 
abc
program response..      <--- the log should be the same as the running
haha
program response..
exit 
program response..

所以到目前为止,这个问题还没有完全解决解决了。

I want to have something like this

gt; ps -ax | tee -a processes.txt

In a UNIX C programming environment, meaning not via shell scripts.

Basically is there a API so that I can tee on STDIN and/or STDOUT so that I can recording automatically anything appears on the CLI down to a file. Imagine there's a foreground process interact with user and response with some output to the terminal. I want to have everything shown in the terminal also in a file for later examination.

I would imagine something as magic as this:

tee(STDIN, "append", logFile);

Thanks!

Follow-up, this is the program I wrote according to Lars (see Answer section below), but not exactly what I desired:

int main(int argc, char** argv){

        int pfd[2];
        if (pipe(pfd) == -1) { perror("pipe"); }


        if (fork()==0) { // child reads from pipe
                close(pfd[1]);

                mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
                int ufd = open("user_study.log", O_CREAT | O_APPEND | O_WRONLY, mode);
                if (ufd == -1) {
                        perror("Cannot open output file"); exit(1);
                } // ++

                char buf;
                while (read(pfd[0], &buf, 1) > 0) {
                        write(STDOUT_FILENO, &buf, 1); // write to stdout
                        write(ufd, &buf,1);            //   and to log
                }

                close(pfd[0]);

        } else { // parent write to pipe
                // dup STDOUT to the pipe
                close(pfd[0]);
                dup2(pfd[1], STDOUT_FILENO);

                char msg[200];
                msg[0] = "";
                do {
                        scanf("%s", msg);
                        printf("program response..");

                } while (strcmp(msg, "exit")!=0);

                close(pfd[1]);

        }

        return 1;
}

Actual running:

[feih@machine ~/mytest]$ ./a.out
abc
haha
exit 
program response..   <---- the output is delayed by the chld process, not desired
program response..
program response..

[feih@machine ~/mytest]$ less user_study.log
program response..   <---- the log doesn't contain input
program response..
program response..

Desired running and log(simulated):

[feih@machine ~/mytest]$ ./a.out
abc
program response..
haha
program response..
exit 
program response..


[feih@machine ~/mytest]$ less user_study.log 
abc
program response..      <--- the log should be the same as the running
haha
program response..
exit 
program response..

So up till now, this problem hasn't been fully resolved.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅笑轻吟梦一曲 2024-11-07 23:56:55

您可以执行以下操作:

  • 中创建一个管道(请参阅pipe(2)
  • 在子级
  • ,从管道中读取数据并写入每个输出
    • 标准输出和文件,或您需要的任何内容
  • 在父级中需要的任何内容,将 stdout 重定向到管道(请参阅 dup2(2))。

您需要处理很多棘手的问题,但它是可行的。

如果没有额外的进程,您就无法做到这一点,因为 printf 只写入一个文件描述符(标准输出)。

You can do this:

  • create a pipe (see pipe(2))
  • fork
  • in the child, read from the pipe and write to each output
    • stdout and a file, or whatever you need
  • in the parent, redirect stdout to the pipe (see dup2(2))

There's a bunch of trickiness you need to take care of, but it's doable.

You can't do it this without an extra process, since printf only writes to one file descriptor (the stdout one).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文