source: libwandio/ior-stdio.c @ e4eff86

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivelibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since e4eff86 was e4eff86, checked in by Shane Alcock <salcock@…>, 6 years ago

Ensure all libwandio code includes config.h

This fixes a nasty bug on 32 bit machines where the size of off_t will
change between functions, causing all sorts of havoc. The reason the size
changes is because FILE_OFFSET_BITS is defined to 64 inside config.h so
any source files that include config.h will have 64 bit off_t's whereas
any files that don't include it will end up with a 32 bit off_t (on a 32 bit
machine).

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