execve(“/bin/sh”, 0, 0); in a pipe

后端 未结 3 755
广开言路
广开言路 2021-02-14 10:25

I have the following example program:

#include 

int
main(int argc, char ** argv){
    char buf[100];

    printf(\"Please enter your name: \");
          


        
3条回答
  •  Happy的楠姐
    2021-02-14 11:09

    You can run your program without any modifications like this:

    (echo -e 'testName\n'; cat ) | ./a.out
    

    This way you ensure that your program's standard input doesn't end after what echo outputs. Instead, cat continues to supply input to your program. The source of that subsequent input is your terminal since this is where cat reads from.

    Here's an example session:

    bash-3.2$ cc stdin_shell.c 
    bash-3.2$ (echo -e 'testName\n'; cat ) | ./a.out 
    Please enter your name: warning: this program uses gets(), which is unsafe.
    Hello "testName"
    pwd
    /home/user/stackoverflow/stdin_shell_question
    ls -l
    total 32
    -rwxr-xr-x  1 user  group  9024 Dec 14 18:53 a.out
    -rw-r--r--  1 user  group   216 Dec 14 18:52 stdin_shell.c
    ps -p $$
      PID TTY           TIME CMD
    93759 ttys000    0:00.01 (sh)
    exit
    
    bash-3.2$
    

    Note that because shell's standard input is not connected to a terminal, sh thinks it is not executed interactively and hence does not display the prompt. You can type your commands normally, though.

提交回复
热议问题