GTK pipe



hello every1,

i'm trying to capture stdout.stderr of a child prosses while using gtk. i'v found the following codes and i changed it a little and it works perfect:

void run_program( char *cmd)
{

   gint    stdin_pipe[2];
   gint    stdout_pipe[2];
   gint    stderr_pipe[2];

   /*  Define buffs to copy your reads from the pipes to */
   gchar buffer[BUFSIZ+1];
   gchar buffer_err[BUFSIZ+1];

   /* Def some vars to hold your results */
   gint fork_result;
   gint data_processed;
   gint data_processed_err;
   gint nchars;
   nchars=0;

   /* Clear Your Buffers */
   memset(buffer, '\0', sizeof(buffer));
   memset(buffer_err, '\0', sizeof(buffer_err));

   /* Do the fork and pipe - watch closely here */

   /* Create pipes to read from and write too */
   if( (pipe(stdin_pipe) == 0)
       && (pipe(stdout_pipe) == 0)
       && (pipe(stderr_pipe) == 0)
     )
   {
       /* Perform the fork */
       fork_result = fork();

       /* fork attempt was not successful */
       if(fork_result == -1)
       {
           fprintf(stderr, "Fork Failure\n");
           exit(EXIT_FAILURE);
       }

       /*  We're in the child process! */
       else if(fork_result == 0)
       {
           /* Close the Child process' STDIN */
           close(0);

/* Duplicate the Child's STDIN to the stdin_pipe file descriptor */
           dup(stdin_pipe[0]);

/* Close the read and write to for the pipe for the child. The child will now only be able to read from it's STDIN (which is our pipe). */
           close(stdin_pipe[0]);
           close(stdin_pipe[1]);

           /* Close the Child process' STDOUT */
           close(1);
           dup(stdout_pipe[1]);
           close(stdout_pipe[0]);
           close(stdout_pipe[1]);

           /* Close the Child process' STDERR */
           close(2);
           dup(stderr_pipe[1]);
           close(stderr_pipe[0]);
           close(stderr_pipe[1]);

           /*  Make the system call to run the program. */
           system(cmd);

           /*  If javac didn't take over the exec call failed. */
           exit(EXIT_FAILURE);
       }

       /* We're in the parent process. */
       else
       {
           /* Close STDIN for read & write and close STDERR for write */
           close(stdin_pipe[0]);
           close(stdin_pipe[1]);
           close(stderr_pipe[1]);

           while(1)
           {
               data_processed_err=read(stderr_pipe[0],buffer_err,BUFSIZ);
               if(data_processed_err == 0) break;
               //addMsgInfo( buffer_err );
               Msg_dialog("Error",buffer_err);
           }
           /* Close the read end of STDERR */

           close(stderr_pipe[0]);
           /* Close the write end of STDOUT */
           close(stdout_pipe[1]);

           while(1)
           {
               /* Read BUFSIZ of STDOUT data */
               data_processed=read(stdout_pipe[0],buffer,BUFSIZ);

               /* Break this loop when we're read all of the STDOUT data */
               if(data_processed == 0) break;

               /* Insert it into our gtktextbox */
               addMsgInfo( buffer );
           }

           /* Close STDOUT for reading */
           close(stdout_pipe[0]);
       }
   }
}


but if i call the following function (clicking that button):

void on_button1_clicked(GtkButton *button, gpointer user_data){

GtkWidget *dialog;
GtkFileFilter *filter = gtk_file_filter_new ();
GtkFileFilter *filter2 = gtk_file_filter_new ();
GtkWidget *parent = lookup_widget(GTK_WIDGET(button), "win");
GtkWidget *entry  = lookup_widget(GTK_WIDGET(button), "entry1");
dialog = gtk_file_chooser_dialog_new ("Open Image File",
                     parent,
                     GTK_FILE_CHOOSER_ACTION_OPEN,
                     GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                     GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
                     NULL);
gtk_file_filter_set_name        (filter,"Iso Files");
gtk_file_filter_set_name        (filter2,"All Files");
gtk_file_filter_add_pattern (filter, "*.iso");
gtk_file_filter_add_pattern (filter2, "*.*");
gtk_file_chooser_add_filter  (GTK_FILE_CHOOSER (dialog), filter);
gtk_file_chooser_add_filter  (GTK_FILE_CHOOSER (dialog), filter2);

if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
 {
   char *filename;

   filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER (dialog));
   gtk_entry_set_text(GTK_ENTRY(entry),filename);
   g_free (filename);
 }

gtk_widget_destroy (dialog);

}


befor using the first one my program chrashes. has anyone any idea? i can't actually find any relationship bitween these two functions. but with a little experiment i realized it crashesh if i make any dialog (filechooser or not) in my programm.

thankx

Ammsa





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]