split play.c and rename para_play to para_write
[paraslash.git] / file_writer.c
1 /*
2 * Copyright (C) 2006 Andre Noll <maan@systemlinux.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
17 */
18
19 #include "para.h"
20 #include "write.h"
21 #include "string.h"
22 #include "error.h"
23
24 struct private_file_writer_data {
25 int fd;
26 };
27 static int file_writer_open(struct writer_node *w)
28 {
29 struct private_file_writer_data *pfwd = para_calloc(
30 sizeof(struct private_file_writer_data));
31 char *tmp = para_tmpname(), *home = para_homedir(),
32 *filename = make_message("%s/.paraslash/%s", home, tmp);
33
34 free(home);
35 free(tmp);
36 w->private_data = pfwd;
37 pfwd->fd = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
38 free(filename);
39 if (pfwd->fd >= 0)
40 return 8192;
41 free(pfwd);
42 return -E_FW_OPEN;
43 }
44
45 static int file_writer_write(char *data, size_t nbytes, struct writer_node *wn)
46 {
47 struct private_file_writer_data *pfwd = wn->private_data;
48 int ret = write(pfwd->fd, data, nbytes);
49 if (ret < 0)
50 ret = -E_FW_WRITE;
51 return ret;
52 }
53
54 static void file_writer_close(struct writer_node *wn)
55 {
56 struct private_file_writer_data *pfwd = wn->private_data;
57 close(pfwd->fd);
58 free(pfwd);
59 }
60
61 void file_writer_init(struct writer *w)
62 {
63 w->open = file_writer_open;
64 w->write = file_writer_write;
65 w->close = file_writer_close;
66 w->shutdown = NULL; /* nothing to do */
67 }