Thread: Howto control pipe() stdin in c programming?

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    9

    Howto control pipe() stdin in c programming?

    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.
    Last edited by solo9300; 06-28-2012 at 06:36 PM.

  2. #2
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    945
    What makes you suspect that it keeps writing to the pipe? Does the git process keep running after your program writes the passphrase?

    It could be that git is reading from its controlling tty instead of from stdin.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. CGI Programming: Reading from STDIN for POST method
    By 809AreaCode in forum C Programming
    Replies: 3
    Last Post: 09-16-2011, 08:09 AM
  2. close stdin pipe
    By MK27 in forum Linux Programming
    Replies: 3
    Last Post: 02-16-2011, 03:42 PM
  3. Replies: 5
    Last Post: 05-24-2010, 10:39 AM
  4. Howto make own application for remote control handling
    By s-men in forum Windows Programming
    Replies: 16
    Last Post: 08-16-2008, 04:22 PM
  5. Newbie Question - fflush(stdin) & fpurge(stdin) on Mac and PC
    By tvsinesperanto in forum C Programming
    Replies: 34
    Last Post: 03-11-2006, 12:13 PM

Tags for this Thread