Fix a bug in the mood line parser.
[paraslash.git] / gui_common.c
1 /*
2 * Copyright (C) 2006-2007 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file gui_common.c Functions used by the guis of paraslash. */
8
9
10 #include <sys/types.h>
11 #include <dirent.h>
12
13 #include "para.h"
14 #include "string.h"
15 #include "fd.h"
16
17 extern const char *status_item_list[NUM_STAT_ITEMS];
18
19 int para_open_audiod_pipe(char *cmd)
20 {
21 int fds[3] = {0, 1, 0};
22 pid_t pid;
23 int ret = para_exec_cmdline_pid(&pid, cmd, fds);
24 if (ret < 0)
25 return ret;
26 ret = mark_fd_nonblock(fds[1]);
27 if (ret > 0)
28 return fds[1];
29 close(fds[1]);
30 return ret;
31 }
32
33 int read_audiod_pipe(int fd, line_handler_t *line_handler)
34 {
35 static char buf[4096];
36 const ssize_t bufsize = sizeof(buf) - 1;
37 static ssize_t loaded;
38 ssize_t ret;
39
40 if (loaded >= bufsize)
41 loaded = 0;
42 ret = read(fd, buf + loaded, bufsize - loaded);
43 if (ret > 0) {
44 loaded += ret;
45 buf[loaded] = '\0';
46 loaded = for_each_line(buf, loaded, line_handler, NULL);
47 }
48 return ret;
49 }