source: lib/libtraceio-stdio.c @ 4315572b

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 4315572b was 4315572b, checked in by Perry Lorier <perry@…>, 16 years ago

Fixup stdio to be more standard compliant
Fix format wag to be more sensitive to errors

  • Property mode set to 100644
File size: 1.4 KB
Line 
1#include "libtrace.h"
2#include "libtrace_int.h"
3#include "libtraceio.h"
4#include <sys/types.h> /* for ssize_t/off_t */
5#include <stdio.h>
6#include <stdlib.h>
7
8#include <errno.h> /* for debugging */
9
10struct libtrace_io_t {
11        FILE *file;
12};
13
14ssize_t libtrace_io_read(libtrace_io_t *io, void *buf, size_t len)
15{
16        int ret=fread(buf,1,len,io->file);
17
18        if (ret==(int)len) {
19                printf("read %i bytes\n",ret);
20                return len;
21        }
22
23        /* EOF or an Error occurred */
24        if (ferror(io->file)) {
25                int err=errno;
26                perror("fread");
27                errno=err;
28                /* errno will be set */
29                return -1;
30        }
31
32        printf("eof\n");
33
34        return 0; /* EOF */
35}
36
37libtrace_io_t *libtrace_io_fdopen(int fd, const char *mode)
38{
39        libtrace_io_t *io = malloc(sizeof(libtrace_io_t));
40        io->file = fdopen(fd,mode);
41        return io;
42}
43
44libtrace_io_t *libtrace_io_open(const char *path, const char *mode)
45{
46        libtrace_io_t *io = malloc(sizeof(libtrace_io_t));
47        io->file = fopen(path,mode);
48        return io;
49}
50
51/* Technically close returns -1 on failure, but if the close fails, really
52 * what are you going to do about it?
53 */
54void libtrace_io_close(libtrace_io_t *io)
55{
56        fclose(io->file);
57        io->file=NULL;
58        free(io);
59}
60
61ssize_t libtrace_io_write(libtrace_io_t *io, const void *buf, size_t len)
62{
63        return fwrite(buf,len,1,io->file);
64}
65
66off_t libtrace_io_seek(libtrace_io_t *io, off_t offset, int whence)
67{
68        return fseek(io->file,offset,whence);
69}
70
71ssize_t libtrace_io_tell(libtrace_io_t *io)
72{
73        return ftell(io->file);
74}
Note: See TracBrowser for help on using the repository browser.