Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write simultaneousely to two streams

Tags:

c

linux

io

stream

Is there a way to to couple two streams (or file descriptors) together so that writing to one stream will also write to the second one? (C, Linux)

Thanks.

like image 555
jackhab Avatar asked Jun 25 '09 13:06

jackhab


4 Answers

Use funopen or fwopen and supply your own write function that writes to multiple FILE*s.

Example:

FILE *files[2] = ...;

FILE *f = fwopen((void *)files, my_writefn);

// ... use f as you like ...

int my_writefn(void *cookie, const char *data, int n) {
  FILE **files = (FILE **)cookie;
  fwrite(data, n, 1, files[0]);
  return fwrite(data, n, 1, files[1]);
}

(Error handling omitted.)

Note that funopen and fwopen are BSD and not in standard Linux. I'm not aware if there's a Linux-compatible equivalent.

like image 103
laalto Avatar answered Oct 23 '22 18:10

laalto


The first thing that came to mind to me was also "tee". So, let's combine C and the shell with popen:

FILE * multi_out;

multi_out = popen( "tee file1.out > file2.out", "w");
/* error checks, actual work here */
pclose( multi_out);
/* error checks here */

As a Unix bigot, I have assumed you are not trying this on Windows.

like image 24
Roboprog Avatar answered Oct 23 '22 17:10

Roboprog


User laalto is correct, but on Linux, the function you are looking for is called fopencookie. Correcting laalto's example for Linux results in:

int my_writefn(void *cookie, const char *data, int n) {
  FILE **files = (FILE **)cookie;
  fwrite(data, n, 1, files[0]);
  return fwrite(data, n, 1, files[1]);
}

int noop(void) { return 0; }
cookie_io_functions_t my_fns = {
  (void*) noop,
  (void*) my_writefn,
  (void*) noop,
  (void*) noop
};

FILE *files[2] = ...;

FILE *f = fopencookie((void *)files, "w", my_fns);

// ... use f as you like ...

When you write to f, the system will execute your my_writefn function passing it the data that was passed to fwrite. To make things easier, you may also want to change the buffering for your file stream to be line oriented:

setvbuf(f, NULL, _IOLBF, 0);

That will buffer up the data passed to fwrite until a newline is output or any data is read from any stream attached to the processes (e.g. stdin). NOTE: you must call sevbuf after fopencookie but before any data is written to the stream.

I use line buffering because I usually use fopencookie to redirect stderr to syslog, or over a network socket, and processing line oriented data is easier and more efficient.

like image 3
Dave Huseby Avatar answered Oct 23 '22 19:10

Dave Huseby


Not sure if it's what you want, but 'tee' in unix does something similar.

like image 2
Rich Bradshaw Avatar answered Oct 23 '22 17:10

Rich Bradshaw