#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <sys/select.h>
#include <string.h>

#define AUDIO_FILENO (STDERR_FILENO + 1)

int main(void)
{
	fd_set fds;
	int res, fd;
	char audiobuf[4096];

        if ((fd = open("/tmp/c-audio.raw", O_WRONLY | O_TRUNC | O_CREAT, 660)) < 0)
                exit(0);
	for (;;) {
		FD_ZERO(&fds);
		FD_SET(AUDIO_FILENO, &fds);
		if ((res = select(AUDIO_FILENO + 1, &fds, NULL, NULL, NULL)) < 0) {
			fprintf(stderr, "Error in select: %s\n", strerror(errno));
			break;
		}
		if (FD_ISSET(AUDIO_FILENO, &fds)) {
			res = read(AUDIO_FILENO, audiobuf, sizeof(audiobuf));
			if (res > 0) {
                                write(fd, audiobuf, res);
			}
		}
	}
        if (fd > 0)
                close(fd);
	exit(0);
}
