source: lib/ior-stdio.c @ 8414770

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 8414770 was 8414770, checked in by Shane Alcock <salcock@…>, 12 years ago
  • Updated licensing and documentation for all the IO reader modules
  • Property mode set to 100644
File size: 2.3 KB
Line 
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: libtrace.h.in 1511 2010-02-05 03:23:49Z salcock $
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 reader, i.e. no decompression
44 */
45
46struct stdio_t {
47        int fd;
48};
49
50extern io_source_t stdio_source;
51
52#define DATA(io) ((struct stdio_t *)((io)->data))
53
54io_t *stdio_open(const char *filename)
55{
56        io_t *io = malloc(sizeof(io_t));
57        io->data = malloc(sizeof(struct stdio_t));
58
59        if (strcmp(filename,"-") == 0)
60                DATA(io)->fd = 0; /* STDIN */
61        else
62                DATA(io)->fd = open(filename,O_RDONLY);
63        io->source = &stdio_source;
64
65        if (DATA(io)->fd == -1) {
66                free(io);
67                return NULL;
68        }
69
70        return io;
71}
72
73static off_t stdio_read(io_t *io, void *buffer, off_t len)
74{
75        return read(DATA(io)->fd,buffer,len);
76}
77
78static off_t stdio_tell(io_t *io)
79{
80        return lseek(DATA(io)->fd, 0, SEEK_CUR);
81}
82
83static off_t stdio_seek(io_t *io, off_t offset, int whence)
84{
85        return lseek(DATA(io)->fd, offset, whence);
86}
87
88static void stdio_close(io_t *io)
89{
90        close(DATA(io)->fd);
91        free(io->data);
92        free(io);
93}
94
95io_source_t stdio_source = {
96        "stdio",
97        stdio_read,
98        NULL,
99        stdio_tell,
100        stdio_seek,
101        stdio_close
102};
103
Note: See TracBrowser for help on using the repository browser.