将输入发送到程序并获得控制权

时间:2010-07-20 09:10:57

标签: c shell pipe cat

我被困在这一段时间了。 假设我有一个如下的C程序。我想能够发送这个程序一些字符串,然后获得控制权。 如果我这样做:


- > cat myfile | myprogram

- > echo“0123”| myprogram

- > myprogram< MYFILE
我得到了输出(myfile包含“0123”)
30 31 32 33

使用-n选项会引发段错误
- > echo -n mystring | 。/测试
zsh:done echo -n“0123”|
zsh:分段错误./test

我也尝试使用命名管道,但它也没有用。

我希望能够做类似的事情 cat myfile | myprogram 并取回控件,以便我可以输入其他字符。

  1 #include  <stdlib.h>
  2 #include  <stdio.h>
  3 
  4 int main (int argc, char *argv[]) {
  6   int i = 0, j;
  7   unsigned char buf[512];
  8   unsigned char x;
  9 
 10   while ((x = getchar()) != '\n') {
 11     buf[i] = x;
 12     i++;
 13   }
 14 
 16   for (j = 0; j < i; j++) {
 17     printf("%x ", buf[j]);
 18   }
 19   printf ( "\n" );
 20 
 21   return EXIT_SUCCESS;
 22 }  // end of function main

编辑:

以下是我提出的包装。 它会执行我想要的所有操作,但子操作文件的输出未正确显示。

没有包装器:

$ bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'. 
2+2
4

使用包装器:

$ ./wrapper bc
2+2
enter
4

删除该行

dup2(pipefd[0], 0);  // Set the read end of the pipe as stdin.

使子标准输出正确显示,但当然会破坏包装。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <assert.h>

int main(int argc, char const *argv[]) {

  int cpid;
  int pipefd[2];


  if (pipe(pipefd) == -1) { perror("pipe.\n"); exit(errno); }


  cpid = fork();
  if (cpid == -1) { perror("fork."); exit(errno); }


  if (cpid) {
    // Parent --------------------------------------------------------

    int buf_size = 8192;
    char buf[buf_size];
    size_t file;

    // Close the unused read end of the pipe.
    close(pipefd[0]);

    // Leave a bit of time to the child to display its initial input.
    sleep(2);

    while (1) {
      gets(buf);

      if (strcmp("enter", buf) == 0) {
        write(pipefd[1], "\n", 1);

      } else if (-1 != (file = open(buf, O_RDONLY))) {
        // Dump the output of the file to the child's stdin.
        char c;
        while(read(file, &c, 1) != 0) {
          switch(c) {
            case '\n':
              printf("(skipped \\n)");
              break;
            default:
              printf("%c", c);
              write(pipefd[1], &c, 1);
          }; 
        }
        printf("\n");

      } else {
        // Dump input to the child's stdin, without trailing '\n'.
        for (int i = 0; (buf[i] != 0); i++) {
          write(pipefd[1], buf + i, 1);
        }
      }
    }

    // Wait for the child to exit.
    printf("Waiting for child to exit.\n");
    wait(NULL);

  } else {
    // Child ---------------------------------------------------------

    // Close the unused write end of the pipe.
    close(pipefd[1]);
    // Set the read end of the pipe as stdin.
    dup2(pipefd[0], 0);  // Set the read end of the pipe as stdin.

    char** program_arguments = (char**)(argv + 1);
    if (execvp(argv[1], program_arguments) < 0) {
      perror("execvp.\n");
      exit(errno);
    }
  }
}

3 个答案:

答案 0 :(得分:0)

在此示例中,我使用的是tail -f,而不是您的C程序

 mkfifo /tmp/pipe   # Open Pipe
 tail -f /tmp/pipe &    #  Start your program and put it into the background

现在您也可以将数据发送到在后台运行的程序

 echo "foobar" > /tmp/pipe

我希望这有帮助吗?

答案 1 :(得分:0)

您可以修改程序以接受1个空字符,然后继续...它可能有效:

之类的东西替换第10行
while (TRUE)
{
    x = getchar();
    if (x == '\n')
        break;
    if (x == '\0')
    {
        if (seen)
            break;
        seen = TRUE;
    }
...

答案 2 :(得分:0)

如果您无法修改程序的行为,我认为使用命名管道无法实现此目的。因为在本质上命名管道没有什么不同,然后通过重定向给出标准输入的输出。

如果您使用shell的管道或重定向属性,我也认为不可能,因为在这种情况下总是会向您的程序发送EOF,并且您无法忽略EOF,因为您无法修改程序。

可能的解决方案是使用包装器。在准备好的输入完成包装器切换到标准输入后,包装器将首先读取准备好的输入,将它们发送到您的程序。实际程序只是不断消耗输入,它不知道数据的实际来源。

唯一的缺点是,您无法提供带有管道或重定向的准备好的输入,您必须提供文件名。 (我不确定命名管道是否可行。)原因很明显,如果您从标准输入提供准备好的输入到包装器,那么包装器也存在同样的问题。通过这种方式,您只需将问题委托给包装器,您可以按照自己想要的方式进行设计。

C中的一种可能的实现(从我使用过的类似包装中修改,未经过广泛测试):

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>

int main(int argc, char * argv[]) {
    char c;
    char **pargs ;

    char buf[20];
    int n;

    int pipe_fd[2];
    int pid;

    pargs = argv+2;
    if (pipe(pipe_fd) < 0) {
            perror("pipe failed");
            exit(errno);
    }
    if ((pid=fork()) < 0) {
            perror ("Fork failed");
            exit(errno);
    }
    if (! pid) {
            close(pipe_fd[1]);
            dup2(pipe_fd[0],0);
            close(pipe_fd[0]);
            if (execvp(argv[2],pargs) < 0) {
                    perror("Exec failed");
                    exit(errno);
            }
    } else {
            size_t filedesc = open(argv[1],O_RDONLY);
            while((n = read(filedesc, buf, 100)) > 0)
                    write (pipe_fd[1], buf, n);
            while((n = read(0, buf, 100)) > 0)
                    write (pipe_fd[1], buf, n);
    }
}

您可以使用此包装程序运行程序:

./wrapper input.txt myprog possible command line arguments

您可以将初始输入放入input.txt

更简单的解决方案是重新打开标准输入。但是,如果您只是尝试打开它,就像打开文件一样,它就不起作用。您应该打开终端流并将其复制到应用程序的标准输入。你可以使用类似的东西(再次使用包装器)来做到这一点:

size_t tty = open("/dev/tty",O_RDONLY);
dup2(tty,0);

更不用说第二个解决方案适用于Linux而不是便携式。

相关问题