/* * play-fifo.c -- A helper app to play a fifo * * Copyright (c) 2004-2007 Anthony Minessale II * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without * restriction, including without limitation the rights to use, * copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following * conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. */ #include #include #include #include #include #include #include /* mutiple of 320 */ #define BUFFER_LEN 32000 int main(int argc, char *argv[]) { int fd = 0, res = 0, bytes = 0; short buf[BUFFER_LEN]; char *path; struct stat st; struct pollfd fds[1]; if (!(path=argv[1])) { path = "/var/run/asterisk/musiconhold.pipe"; } if (stat(path, &st)) { if (mkfifo(path, 0)) { perror("Error creating FIFO"); } } if ((fd = open(path, O_RDONLY)) <= 0) { perror("Error opening input pipe"); return -1; } while(1) { fds[0].fd = fd; fds[0].events = POLLIN; bytes = 0; if ((res = poll(fds, 1, BUFFER_LEN / 16)) < 0) { perror("No Data\n"); break; } else if (res) { if ((bytes = read(fd, buf, BUFFER_LEN)) < 0) { perror("No Data\n"); break; } } if (!bytes) { continue; } fds[0].fd = STDOUT_FILENO; fds[0].events = POLLOUT; res = poll(fds, 1, BUFFER_LEN / 16); if (res < 0) { perror("No Data\n"); break; } else if (res && bytes) { write(STDOUT_FILENO, buf, bytes); } /* else it's trash */ } close(fd); return 0; }