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

pipes with gtk



Below there is a pice of code to work with a backgound binary process 
(in this case a ftp).

I want to send commands from a Gtk text entry and receive a the 
response into a text box. In this example I write with accion and I 
want to read the the response with in a text box.

When I try read the data with  while (fgets(str,BUFSIZE, fout) != 
NULL), I cant get out from the loop ???

What do i have to use to read str and continue???

Thanks a million

Guillermo


SOURCE CODE:

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>

#define BUFSIZE 1024


	pid_t childpid;
	char str[BUFSIZE];
	int pin[2],pout[2],len;
	FILE *fout;
	
	


void accion(char *instruccion){                
		sprintf(str,"%s\n", instruccion);
		printf("%s",str);
		len=strlen(str);
		write(pin[1],str,len);
		fout=fdopen(pout[0],"r");
          
            // to send to text box
            while (fgets(str,BUFSIZE, fout) != NULL) printf("#%s", 
str);// This is the problem

                     }                     
		system("sleep 1"); // just to no get broken pipe ???
}


	
int main(){
	
	pipe(pin);
	pipe(pout);
	
	childpid=fork();
	if(childpid<0){
		       perror("fork");
		       return -2;
	}
	
	if(childpid==0){
		close(pin[1]);
		close(pout[0]);
		dup2(pin[0],0);
		dup2(pout[1],1);
		execlp("ftp","ftp","-n","192.168.1.106",NULL);
	}
	else{
		close(pin[0]);
		close(pout[1]);	
		sprintf(str,"user anonymous \"pepe@pepe.es\"\n");
		printf("%s",str);
		len=strlen(str);
		write(pin[1],str,len);
		fout=fdopen(pout[0],"r");
		
	    }
		//while (fgets(str,BUFSIZE, fout) != NULL) printf("%s", 
str);
		
	
	

accion("ls");  		// to send from a text entry
printf("ls\n");		// to send from a text entry
accion("cd pub");		// to send from a text entry
accion("get file.bmp");	// to send from a text entry
accion("quit");		// to send from a text entry

	return 0;
}



 ___________________________________________________________________ 
Consigue tu e-mail gratuito TERRA.ES
 Haz clic en http://www.terra.es/correo/





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