1 | /* |
---|
2 | * This file is part of libtrace |
---|
3 | * |
---|
4 | * Copyright (c) 2007,2008,2009,2010 The University of Waikato, Hamilton, |
---|
5 | * New Zealand. |
---|
6 | * |
---|
7 | * Authors: Daniel Lawson |
---|
8 | * Perry Lorier |
---|
9 | * Shane Alcock |
---|
10 | * |
---|
11 | * All rights reserved. |
---|
12 | * |
---|
13 | * This code has been developed by the University of Waikato WAND |
---|
14 | * research group. For further information please see http://www.wand.net.nz/ |
---|
15 | * |
---|
16 | * libtrace is free software; you can redistribute it and/or modify |
---|
17 | * it under the terms of the GNU General Public License as published by |
---|
18 | * the Free Software Foundation; either version 2 of the License, or |
---|
19 | * (at your option) any later version. |
---|
20 | * |
---|
21 | * libtrace is distributed in the hope that it will be useful, |
---|
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
24 | * GNU General Public License for more details. |
---|
25 | * |
---|
26 | * You should have received a copy of the GNU General Public License |
---|
27 | * along with libtrace; if not, write to the Free Software |
---|
28 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
29 | * |
---|
30 | * $Id$ |
---|
31 | * |
---|
32 | */ |
---|
33 | |
---|
34 | |
---|
35 | #include "wandio.h" |
---|
36 | #include <sys/types.h> |
---|
37 | #include <sys/stat.h> |
---|
38 | #include <fcntl.h> |
---|
39 | #include <stdlib.h> |
---|
40 | #include <unistd.h> |
---|
41 | #include <string.h> |
---|
42 | |
---|
43 | /* Libtrace IO module implementing a standard IO writer, i.e. no decompression |
---|
44 | */ |
---|
45 | |
---|
46 | struct stdiow_t { |
---|
47 | int fd; |
---|
48 | }; |
---|
49 | |
---|
50 | extern iow_source_t stdio_wsource; |
---|
51 | |
---|
52 | #define DATA(iow) ((struct stdiow_t *)((iow)->data)) |
---|
53 | |
---|
54 | iow_t *stdio_wopen(const char *filename) |
---|
55 | { |
---|
56 | iow_t *iow = malloc(sizeof(iow_t)); |
---|
57 | iow->source = &stdio_wsource; |
---|
58 | iow->data = malloc(sizeof(struct stdiow_t)); |
---|
59 | |
---|
60 | if (strcmp(filename,"-") == 0) |
---|
61 | DATA(iow)->fd = 1; /* STDOUT */ |
---|
62 | else |
---|
63 | DATA(iow)->fd = open(filename,O_WRONLY|O_CREAT|O_TRUNC,0666); |
---|
64 | |
---|
65 | if (DATA(iow)->fd == -1) { |
---|
66 | free(iow); |
---|
67 | return NULL; |
---|
68 | } |
---|
69 | |
---|
70 | return iow; |
---|
71 | } |
---|
72 | |
---|
73 | static off_t stdio_wwrite(iow_t *iow, const char *buffer, off_t len) |
---|
74 | { |
---|
75 | return write(DATA(iow)->fd,buffer,len); |
---|
76 | } |
---|
77 | |
---|
78 | static void stdio_wclose(iow_t *iow) |
---|
79 | { |
---|
80 | close(DATA(iow)->fd); |
---|
81 | free(iow->data); |
---|
82 | free(iow); |
---|
83 | } |
---|
84 | |
---|
85 | iow_source_t stdio_wsource = { |
---|
86 | "stdiow", |
---|
87 | stdio_wwrite, |
---|
88 | stdio_wclose |
---|
89 | }; |
---|