1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> #include <fcntl.h>
#define BUFSIZE 4096
int main(int argc, char *argv[]) { int fd, pipefd[2]; pid_t pid; char buf[BUFSIZE]; ssize_t ret;
if(argc != 2) { fprintf(stderr, "Usage: %s <path_to_mp3_file>\n", argv[0]); exit(1); }
fd = open(argv[1], O_RDONLY); if(fd == -1) { perror("open()"); exit(1); }
if(pipe(pipefd) == -1) { perror("pipe()"); exit(1); }
pid = fork(); if(pid < 0) { perror("fork()"); exit(1); } else if(pid == 0) { close(pipefd[1]); dup2(pipefd[0], STDIN_FILENO); execlp("mpg123", "mpg123", "-", NULL); perror("execlp()"); exit(1); } else { close(pipefd[0]); while((ret = read(fd, buf, BUFSIZE)) > 0) { write(pipefd[1], buf, ret); } close(pipefd[1]); close(fd); wait(NULL); }
exit(0); }
|