Changeset 698a217


Ignore:
Timestamp:
04/23/06 14:21:02 (15 years ago)
Author:
Perry Lorier <perry@…>
Branches:
4.0.1-hotfixes, cachetimestamps, develop, dpdk-ndag, etsilive, getfragoff, help, libtrace4, master, ndag_format, pfring, rc-4.0.1, rc-4.0.2, rc-4.0.3, rc-4.0.4, ringdecrementfix, ringperformance, ringtimestampfixes
Children:
e4e95499
Parents:
4315572b
Message:

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

Location:
lib
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • lib/libtraceio-stdio.c

    r4315572b r698a217  
    55#include <stdio.h>
    66#include <stdlib.h>
    7 
    8 #include <errno.h> /* for debugging */
    97
    108struct libtrace_io_t {
     
    1715
    1816        if (ret==(int)len) {
    19                 printf("read %i bytes\n",ret);
    2017                return len;
    2118        }
     
    2320        /* EOF or an Error occurred */
    2421        if (ferror(io->file)) {
    25                 int err=errno;
    26                 perror("fread");
    27                 errno=err;
    2822                /* errno will be set */
    2923                return -1;
    3024        }
    31 
    32         printf("eof\n");
    3325
    3426        return 0; /* EOF */
     
    6153ssize_t libtrace_io_write(libtrace_io_t *io, const void *buf, size_t len)
    6254{
    63         return fwrite(buf,len,1,io->file);
     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 */
    6465}
    6566
  • lib/libtraceio.h

    redb18ce r698a217  
    1010typedef struct libtrace_io_t libtrace_io_t;
    1111
     12/** read a block from a file
     13 * @param io    the io file object
     14 * @param buf   the buffer to read into
     15 * @param len   the number of bytes to read
     16 *
     17 * @returns -1 on error (with errno set), 0 on eof, otherwise the number of bytes
     18 *      read.
     19 */
    1220ssize_t libtrace_io_read(libtrace_io_t *io, void *buf, size_t len);
     21/** open a file from a file descriptor (like fdopen(3))
     22 * @param fd    file descriptor to read
     23 * @param mode  text string to represent what mode to read the file in.
     24 *
     25 * @returns io object, or NULL on error.
     26 */
    1327libtrace_io_t *libtrace_io_fdopen(int fd, const char *mode);
     28/** open a file from a path name
     29 * @param path  pathname to read
     30 * @param mode  text string to represent what mode to read the file in.
     31 *
     32 * @returns io object, or NULL on error.
     33 */
    1434libtrace_io_t *libtrace_io_open(const char *path, const char *mode);
     35/** close a file and free all of it's resources.
     36 * @param io    io object
     37 *
     38 * This function doesn't return anything.  In theory it could return an error
     39 * but seriously, if it did return an error, what would you do about it?
     40 */
    1541void libtrace_io_close(libtrace_io_t *io);
     42
     43/** write a block of data to a file
     44 * @param io    libtrace io object to write to
     45 * @param buf   buffer to write to
     46 * @param len   number of bytes to write
     47 *
     48 * @returns the number of bytes successfully written, or -1 on error with
     49 * errno set
     50 */
    1651ssize_t libtrace_io_write(libtrace_io_t *io, const void *buf, size_t len);
    1752off_t libtrace_io_seek(libtrace_io_t *io, off_t offset, int whence);
Note: See TracChangeset for help on using the changeset viewer.