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 | #define _GNU_SOURCE 1 |
---|
36 | #include "wandio_internal.h" |
---|
37 | #include "wandio.h" |
---|
38 | #include <sys/types.h> |
---|
39 | #include <sys/stat.h> |
---|
40 | #include <fcntl.h> |
---|
41 | #include <stdlib.h> |
---|
42 | #include <unistd.h> |
---|
43 | #include <string.h> |
---|
44 | |
---|
45 | /* Libtrace IO module implementing a standard IO reader, i.e. no decompression |
---|
46 | */ |
---|
47 | |
---|
48 | struct stdio_t { |
---|
49 | int fd; |
---|
50 | }; |
---|
51 | |
---|
52 | extern io_source_t stdio_source; |
---|
53 | |
---|
54 | #define DATA(io) ((struct stdio_t *)((io)->data)) |
---|
55 | |
---|
56 | io_t *stdio_open(const char *filename) |
---|
57 | { |
---|
58 | io_t *io = malloc(sizeof(io_t)); |
---|
59 | io->data = malloc(sizeof(struct stdio_t)); |
---|
60 | |
---|
61 | if (strcmp(filename,"-") == 0) |
---|
62 | DATA(io)->fd = 0; /* STDIN */ |
---|
63 | else |
---|
64 | DATA(io)->fd = open(filename, |
---|
65 | O_RDONLY |
---|
66 | #ifdef O_DIRECT |
---|
67 | |(force_directio_read?O_DIRECT:0) |
---|
68 | #endif |
---|
69 | ); |
---|
70 | io->source = &stdio_source; |
---|
71 | |
---|
72 | if (DATA(io)->fd == -1) { |
---|
73 | free(io); |
---|
74 | return NULL; |
---|
75 | } |
---|
76 | |
---|
77 | return io; |
---|
78 | } |
---|
79 | |
---|
80 | static off_t stdio_read(io_t *io, void *buffer, off_t len) |
---|
81 | { |
---|
82 | return read(DATA(io)->fd,buffer,len); |
---|
83 | } |
---|
84 | |
---|
85 | static off_t stdio_tell(io_t *io) |
---|
86 | { |
---|
87 | return lseek(DATA(io)->fd, 0, SEEK_CUR); |
---|
88 | } |
---|
89 | |
---|
90 | static off_t stdio_seek(io_t *io, off_t offset, int whence) |
---|
91 | { |
---|
92 | return lseek(DATA(io)->fd, offset, whence); |
---|
93 | } |
---|
94 | |
---|
95 | static void stdio_close(io_t *io) |
---|
96 | { |
---|
97 | close(DATA(io)->fd); |
---|
98 | free(io->data); |
---|
99 | free(io); |
---|
100 | } |
---|
101 | |
---|
102 | io_source_t stdio_source = { |
---|
103 | "stdio", |
---|
104 | stdio_read, |
---|
105 | NULL, |
---|
106 | stdio_tell, |
---|
107 | stdio_seek, |
---|
108 | stdio_close |
---|
109 | }; |
---|
110 | |
---|