popen / broken pipe

popen / broken pipe

Post by Huntres » Fri, 09 Nov 2001 00:47:12



Hi all,
I am writing a c program on a unix machine.
I am using popen to open a pipe to run a shell script and get the result
back. I do this several times with success. However, one of the times, I
always get back an error that says "broken pipe". Does anyone know what
causes this and how to fix it?

Sample code...
This one works fine.

strcpy(cmd, "date +%m/%d/%Y");              
if ((ptmp = popen(cmd, "r")) != NULL)      
   {                                        
   fgets(dateDumpReceived, 11, ptmp);      
   pclose(ptmp);                            
   }                                        
else                                        
   strcpy(dateReceived, "00/00/0000");  

The code that breaks is this...
sprintf(cmd, "isInDB %s %s", customerName, orderNumber);
if ((ptmp = popen(cmd, "r")) != NULL)
   {
   fgets(response, 1, ptmp);
   pclose(ptmp);
   }
if (strncmp(response, "Y", 1) != 0)
   {
   printf("Fatal Error: Customer not found in DB!.\n");
   return 1;
   }

Looks the same to me...but the second one gives me the "broken pipe"
message. If it matters, the second bit there actually appears first in
the full code.

Can anyone help...?

Thanks!
~kaeli~

 
 
 

popen / broken pipe

Post by Joe Halpi » Fri, 09 Nov 2001 01:09:57



> The code that breaks is this...
> sprintf(cmd, "isInDB %s %s", customerName, orderNumber);
> if ((ptmp = popen(cmd, "r")) != NULL)
>    {
>    fgets(response, 1, ptmp);
>    pclose(ptmp);
>    }
> if (strncmp(response, "Y", 1) != 0)
>    {
>    printf("Fatal Error: Customer not found in DB!.\n");
>    return 1;
>    }

Are you sure that isInDB is in the PATH variable of the shell that
gets popen'd? What happens if you put the full path of the command in
cmd? eg

sprintf(cmd, "/full/path/isInDB %s %s", customerName, orderNumber);

Joe
--
Remember Flight 93

 
 
 

popen / broken pipe

Post by Huntres » Fri, 09 Nov 2001 04:48:53




> > The code that breaks is this...
> > sprintf(cmd, "isInDB %s %s", customerName, orderNumber);
> > if ((ptmp = popen(cmd, "r")) != NULL)
> >    {
> >    fgets(response, 1, ptmp);
> >    pclose(ptmp);
> >    }
> > if (strncmp(response, "Y", 1) != 0)
> >    {
> >    printf("Fatal Error: Customer not found in DB!.\n");
> >    return 1;
> >    }

> Are you sure that isInDB is in the PATH variable of the shell that
> gets popen'd? What happens if you put the full path of the command in
> cmd? eg

> sprintf(cmd, "/full/path/isInDB %s %s", customerName, orderNumber);

Actually, the full path is there. I just shortened it for here 'cuz it's
really long.  :)
It actually works now with no intervention from me, so I guess it was a
machine error of some kind. I am still curious as to what would cause
it...

~kaeli~

 
 
 

1. Q: avoiding broken pipe with popen()

This may be simple for someone to sort out, but I can't seem to get
anywhere...

I'm in the middle of writing a program which uses popen() to output
information to a pager, for previewing before it is saved...

But hitting 'q' in the pager gives a stops my program and gives a
"Broken Pipe" error; I would prefer it to just "give up" on outputting
the information! (There can be a lot of it, which is why I was trying
to avoid, say, writing to a tmp file first and then viewing that.)  Is
there a way to achieve cleaner behaviour, whilst still having the
benefits of piping?

Any help much appreciated -- I'm on a tight deadline, so if you could
email me all the better.
--
Martin Corley                                         ---------

Exeter, UK                                            ---------

2. Resend: [PATCH 2.5] capability flag for ATAPI MO drives

3. popen and broken pipes

4. Sol 7 Recommended partition sizes

5. popen and "broken pipe"

6. OSS Sound Programming - pause and stop playing immediatelly

7. popen, how do I know if pipe has closed?

8. firewall or proxy

9. Pipe (popen) to "lp"dying with error

10. Pipes and popen()

11. Standard error through popen pipe?

12. Popen no and Pipe no :-(