1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Howto control pipe() stdin in c programming?

Discussion in 'C' started by solo9300, Jun 29, 2012.

  1. solo9300

    solo9300 New Member

    Joined:
    Jun 29, 2012
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    hello all,

    I am writing a program to control GIT. My problem is like this, I need to add a sign tag using git command and need my program to control the stdin to enter the.

    Here what i did so far:

    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <string.h>
    int my_pipe[2];
    
    void ErrorAndExit(char* message) {
        fprintf(stderr, "%s\n", message);
        exit(1);
    }
    void WriteToPipe(char* s) {
        write(my_pipe[1], s, strlen(s));
    }
    int main(int argc, char* argv[]) {
        int pid;
        char* my_argv[]= {"/usr/bin/git",
                  "tag",
                  "-s",
                  "1.0.0.2",
                  "-m",
                  "'version 1.0.0.2'",
                     0
                         }; 
        if (pipe(my_pipe) == -1) ErrorAndExit("pipe failed.");
    
        pid=fork();
        if (pid==-1) ErrorAndExit("fork failed.");
        if (pid==0) {
            /* son process */
            close(0);
            dup(my_pipe[0]);
            close(my_pipe[0]);
            close(my_pipe[1]);
            execv(my_argv[0], my_argv);
            ErrorAndExit("Program not found.");
        }
        /* father process */
        WriteToPipe("My Really Really Really Really Long passphrase \n");
        WriteToPipe("\n");
    
        return 0;
    }
    
    The problem with this code it keep writing to the pipe and would not exit.

    Any help would be much appreciated.
     

Share This Page