fifo.c (1177B)
1 #include <sys/select.h> 2 #include <sys/stat.h> 3 #include <sys/types.h> 4 5 #include <err.h> 6 #include <fcntl.h> 7 #include <limits.h> 8 #include <stdio.h> 9 #include <unistd.h> 10 11 #include "sad.h" 12 13 static int fifofd = -1; 14 15 int fifovolstatus = DEFAULTVOL; 16 17 static int fifovol(int vol) { 18 (void)vol; 19 20 return 0; 21 } 22 23 static int fifoopen(Format *fmt) { 24 (void)fmt; 25 26 unlink("/tmp/sad-fifo"); 27 if (mkfifo("/tmp/sad-fifo", 0644) < 0) { 28 warn("mkfifo /tmp/sad-fifo"); 29 return -1; 30 } 31 32 fifofd = open("/tmp/sad-fifo", O_RDWR | O_NONBLOCK); 33 if (fifofd < 0) { 34 warn("open /tmp/sad-fifo"); 35 return -1; 36 } 37 return 0; 38 } 39 40 static int fifoplay(void *buf, size_t nbytes) { 41 ssize_t n, wrote; 42 char *p = buf; 43 44 wrote = 0; 45 while (nbytes > 0) { 46 n = write(fifofd, &p[wrote], nbytes); 47 if (n < 0) 48 return nbytes; /* ignore errors */ 49 else if (n == 0) 50 break; 51 wrote += n; 52 nbytes -= n; 53 } 54 return wrote; 55 } 56 57 static int fifoclose(void) { 58 if (fifofd != -1) { 59 close(fifofd); 60 fifofd = -1; 61 } 62 return 0; 63 } 64 65 Output fifooutput = { 66 .volstatus = &fifovolstatus, 67 .vol = fifovol, 68 .open = fifoopen, 69 .play = fifoplay, 70 .close = fifoclose, 71 };