[Subject Prev][Subject Next][Thread Prev][Thread Next][Subject Index][Thread Index]

Re: popen buffersize limit - doubt



Ramesh forced the electrons to say:
> what is the buffer limit for a popen call. suppose u
> want to pipe the ouptput of a command means  and the
> output of the command is 350KB means does this will
> work.. 

popen() by itself does not allocate any buffers. To read from a program
that you have opened via popen(), you have to use one of the stdio
library's input functions - fgetc, fscanf, fgets etc.

For example, to read 350 KB of data from a popen'ed stream, you can
allocate a 350 KB (+1, for the null byte in case of text streams :-)
and read everything in one fgets() call. Or you can allocate one 50 KB
buffer and read data in a loop.

Example:

char buffer[BUFSIZ];
FILE *pp = popen ("/usr/bin/yes", "r");
if (pp) {
   while (fgets (buffer, sizeof buffer, pp) != NULL)
      /* ... */
}

> the popen function. does it depends on any the system
> runtime availability of resources....

Of course. The system memory, process and open files limit all will
affect the success of popen().

Binand

PS: The above code snippet is an infinite loop. Don't use it!

-- 
The prompt for all occasions:
export PS1="F:\$(pwd | tr '/[a-z]' '\134\134[A-Z]')> "
--------------- Binand Raj S. (binand@xxxxxxxxxxxxxxxxxxxxx)