How send execlp result to the father?

D

druidamix

I'm trying to make a gui for an application, but it not run.

i'm trying to send the output of execlp to pipe and read from his
father. but i don't obtain results.

Any advince?

Thank you.

This is the code:



#include<gtk/gtk.h>
#include<glade/glade.h>
#include<string.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<unistd.h>
#include<stdlib.h>



GladeXML *xml;


static void button_widget(GtkWidget *widget,gpointer data);


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

GtkWidget *button1;
GtkWidget *principal;

gtk_init(&argc,&argv);

xml = glade_xml_new("gtk.glade",NULL,NULL);

principal = glade_xml_get_widget(xml,"principal");
button1 = glade_xml_get_widget(xml,"boto1");

g_signal_connect(G_OBJECT(principal),"delete-event",
G_CALLBACK(gtk_main_quit),principal);


g_signal_connect(G_OBJECT(button1),"clicked",
G_CALLBACK(button_widget),NULL);


gtk_main();
return 0;
}

void button_widget(GtkWidget *widget,gpointer data)
{
//GtkWidget *textview;
//GtkWidget *entry;
//GtkTextBuffer *buffer;
//const gchar *text;


gint a[2];
gint pid;

gchar cbuffer[BUFSIZ+1];

memset(cbuffer,'\0',strlen(cbuffer));

if(pipe(a) ==0)
{


if((pid = fork()) ==0)
{
close(1);
dup(a[1]);
close(a[1]);
if(execlp("translate","translate","yes",NULL) == -1)
perror("No executat");

}

else
{
int i;
wait(&i);

close(a[1]);
close(0);
dup(a[0]);
close(a[0]);

if( (i= read(a[0],cbuffer,strlen(cbuffer))==0))
printf("Error\n");

printf("***%s hello",cbuffer);

}

}

}
 
W

Walter Roberson

I'm trying to make a gui for an application, but it not run.
i'm trying to send the output of execlp to pipe and read from his
father. but i don't obtain results.
Any advince?

Your code uses many operating-system extensions that we are not
qualified to discuss here. This newsgroup pretty much only deals with
C as defined in the C standards, without any extensions. You should
ask in one of the comp.unix.* newsgroups.

This is the code:
[OT]

if((pid = fork()) ==0)
{ [...]
if(execlp("translate","translate","yes",NULL) == -1)
perror("No executat");
}
else
{
int i;
wait(&i);

I'm not up to speed on such things (ask in comp.unix.*) but
I believe wait() waits for the child process to die, not for the
child process to be ready. If the child process has already died,
then it isn't going to be able to talk to you.

Maybe something it wrote would be sitting in the buffers, but you
cannot count on that: what it wanted to write might have been too
large for the inter-process buffers to store (they are only about 4 Kb
or 8 Kb, something on that order), so the child process might have just
written a bit and then gotten suspended waiting for the parent to read
what was already written before the child could go ahead and write
more. So the child needs to be alive while processes are talking
(except perhaps during the -last- buffer-full), not having already died.
 
M

Malcolm McLean

I'm trying to make a gui for an application, but it not run.

i'm trying to send the output of execlp to pipe and read from his
father. but i don't obtain results.

Any advince?

Thank you.

This is the code:



#include<gtk/gtk.h>
#include<glade/glade.h>
#include<string.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<unistd.h>
#include<stdlib.h>
#include said:
GladeXML *xml;


static void button_widget(GtkWidget *widget,gpointer data);


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

GtkWidget *button1;
GtkWidget *principal;

gtk_init(&argc,&argv);

xml = glade_xml_new("gtk.glade",NULL,NULL);

principal = glade_xml_get_widget(xml,"principal");
button1 = glade_xml_get_widget(xml,"boto1");

g_signal_connect(G_OBJECT(principal),"delete-event",
G_CALLBACK(gtk_main_quit),principal);


g_signal_connect(G_OBJECT(button1),"clicked",
G_CALLBACK(button_widget),NULL);


gtk_main();
return 0;
}

void button_widget(GtkWidget *widget,gpointer data)
{
//GtkWidget *textview;
//GtkWidget *entry;
//GtkTextBuffer *buffer;
//const gchar *text;


gint a[2];
gint pid;

gchar cbuffer[BUFSIZ+1];
assert(0);
memset(cbuffer,'\0',strlen(cbuffer));

if(pipe(a) ==0)
{


if((pid = fork()) ==0)
{
close(1);
dup(a[1]);
close(a[1]);
if(execlp("translate","translate","yes",NULL) == -1)
perror("No executat");

}

else
{
int i;
wait(&i);

close(a[1]);
close(0);
dup(a[0]);
close(a[0]);

if( (i= read(a[0],cbuffer,strlen(cbuffer))==0))
printf("Error\n");

printf("***%s hello",cbuffer);

}

}

}
Add the modifications. That will tell you whether that function is being
called, and with any luck you OS will also allow you to ignore the assert(),
so you can step through it.

Next step is to try outputting a string to stderr or stdout. Do you have
output?

Once you've got that far, hack into the pipe() call to see if it doing what
you expect. Does it return at all? Does it return what you expect?
 
K

Keith Thompson

I'm trying to make a gui for an application, but it not run.

i'm trying to send the output of execlp to pipe and read from his
father. but i don't obtain results.

Any advince?

Ask in comp.unix.programmer. execlp is not a standard C function;
it's specific to Unix-like operating systems.

When you post there, you should be much more specific about the error
you're seeing. Saying you "don't obtain results" is not particularly
useful. You get *some* results, just not the ones you're expecting.
If the program does nothing and immediately terminates, say so. If it
does nothing and hangs, say so. If it produces any output or other
visible behavior, describe it.

But I do see at least one C problem in your code.

[...]
void button_widget(GtkWidget *widget,gpointer data)
{ [...]
gchar cbuffer[BUFSIZ+1];

memset(cbuffer,'\0',strlen(cbuffer));
[...]

You don't initialize cbuffer at the point of declaration. Since it
has automatic storage duration (i.e., it's declared inside a function
without the "static" keyword), its initial value is garbage.

You apply the strlen() function to cbuffer. strlen() scans the
*contents* of the array for a terminating '\0' character. It's likely
that it will happen to find one, but it's not guaranteed.

You need to understand the difference between a string and a character
array. A character array is a data type; an object of such a type has
a size determined by how it's declared. In your program cbuffer has a
size of BUFSIZE+1 (I'm assuming gchar is a character type). A string,
on the other hand, is a data *format*. Specifically (C99 7.1.1p1):

A string is a contiguous sequence of characters terminated by and
including the first null character.

An array object (such as cbuffer) doesn't hold a string until and
unless you store a string in it. The strlen() function can only be
applied to strings.

What exactly are you trying to accomplish with your memset() call?
If you want to zero the entire array, you should use sizeof:

memset(cbuffer, 0, sizeof cbuffer);

[...]
if( (i= read(a[0],cbuffer,strlen(cbuffer))==0))
printf("Error\n");

printf("***%s hello",cbuffer);
[...]

Again, you apply strlen to cbuffer. If you've successfully
zero-filled it, then strlen(cbuffer) yields 0; if you haven't,
strlen(cbuffer) is meaningless. Again, you probably want to use
sizeof, not strlen.

The read function isn't defined by the C standard. Any specific
questions about it belong in comp.unix.programmer. But assuming it's
similar to the standard C function fread, it reads data from an input
source into a specified buffer. If it succeeds, cbuffer *still* won't
contain a string unless you happened to read a '\0' character from the
input source (file, socket, whatever). printf's "%s" format works
only with strings, so your printf call is also likely to cause
problems.
 
M

Malcolm McLean

CBFalconer said:
Please do not answer off-topic requests here. There are (at least
in theory) no experts to correct mistakes. The appropriate
response is to redirect to a suitable newsgroup, if known, or to a
class of newsgroups if not. If the question cannot be resolved by
reference to a suitable C standard, it is off-topic.
I didn't mention the internals of the particular libraries the OP was
calling. I showed how to tackle the problem of bug hunting, using standard
library constructs.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top