source: lib/libtraceio-stdio.c @ 1e52a3c

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 1e52a3c was 698a217, checked in by Perry Lorier <perry@…>, 15 years ago

Fix the stdio error handling
Document better the libtraceio API. See the trac wiki for more information.

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