diff --git a/include/libcasio/stream.h b/include/libcasio/stream.h index ec2cd4e..77feebb 100644 --- a/include/libcasio/stream.h +++ b/include/libcasio/stream.h @@ -100,8 +100,8 @@ typedef int casio_stream_setattrs_t typedef int casio_stream_settm_t OF((void *, const casio_timeouts_t *)); -typedef int casio_stream_read_t - OF((void *, unsigned char *, size_t *)); +typedef size_t casio_stream_read_t + OF((void *, unsigned char *, size_t)); typedef int casio_stream_write_t OF((void *, const unsigned char *, size_t)); typedef int casio_stream_seek_t @@ -359,9 +359,9 @@ CASIO_EXTERN int CASIO_EXPORT casio_get_lasterr /* Read and write data from and to a stream. */ -CASIO_EXTERN int CASIO_EXPORT casio_read +CASIO_EXTERN size_t CASIO_EXPORT casio_read OF((casio_stream_t *casio__stream, - void *casio__dest, size_t *casio__psize)); + void *casio__dest, size_t casio__size)); CASIO_EXTERN int CASIO_EXPORT casio_write OF((casio_stream_t *casio__stream, const void *casio__data, size_t casio__size)); diff --git a/lib/file/file.h b/lib/file/file.h index 5cd24fc..ec14301 100644 --- a/lib/file/file.h +++ b/lib/file/file.h @@ -28,16 +28,15 @@ /* Read from a stream. */ # define READ(CASIO__TO, CASIO__SZ) /* normal read */ { \ - size_t size = CASIO__SZ; \ - int READ_err = casio_read(buffer, (CASIO__TO), &size); \ - if (READ_err) return (READ_err); } -# define FREAD(CASIO__TO, CASIO__SZ) /* fail-less read */ \ - size_t size = CASIO__SZ; \ - err = casio_read(buffer, (CASIO__TO), &size); -# define GREAD(CASIO__TO, CASIO__SZ) /* read with goto fail */ \ - size_t size = CASIO__SZ; \ - if ((err = casio_read(buffer, (CASIO__TO), &size))) \ - goto fail; + int READ_err = casio_read(buffer, (CASIO__TO), (CASIO__SZ)); \ + if (READ_err == -1) return (errno); } +# define FREAD(CASIO__TO, CASIO__SZ) /* fail-less read */ { \ + casio_read(buffer, (CASIO__TO), (CASIO__SZ)); \ + err = errno; } +# define GREAD(CASIO__TO, CASIO__SZ) /* read with goto fail */ { \ + casio_read(buffer, (CASIO__TO), (CASIO__SZ)); \ + if ((err = errno)) \ + goto fail; } /* Read using size of the object. */ diff --git a/lib/internals.h b/lib/internals.h index 2823301..de00678 100644 --- a/lib/internals.h +++ b/lib/internals.h @@ -26,6 +26,7 @@ # include # include # include +# include # include "utils/endian.h" # include "log/log.h" diff --git a/lib/link/seven/dataflow.c b/lib/link/seven/dataflow.c index 1c31da1..ab158df 100644 --- a/lib/link/seven/dataflow.c +++ b/lib/link/seven/dataflow.c @@ -64,8 +64,10 @@ int CASIO_EXPORT casio_seven_send_buffer(casio_link_t *handle, /* Read the big block. */ toread = min(BUFSIZE, size); - err = casio_read(buffer, buf + 8, &toread); - if (err) return (casio_error_noread); + toread = casio_read(buffer, buf + 8, toread); + if (toread == (size_t)-1) { + return (casio_error_noread); + } size -= toread; /* Send each block. */ @@ -95,7 +97,7 @@ int CASIO_EXPORT casio_seven_send_buffer(casio_link_t *handle, /* Unshift. */ if (handle->casio_link_flags & casio_linkflag_shifted) err = casio_seven_unshift(handle); - + return (0); fail: return (err); } diff --git a/lib/link/seven/datastream.c b/lib/link/seven/datastream.c index 456baff..ea8ef2c 100644 --- a/lib/link/seven/datastream.c +++ b/lib/link/seven/datastream.c @@ -32,7 +32,6 @@ typedef struct { casio_link_progress_t *_disp; void *_disp_cookie; unsigned int _id, _total; - unsigned int _totalsize; /* */ unsigned int _lastsize; /* last packet size */ /* buffer management */ @@ -52,15 +51,16 @@ typedef struct { * @arg cookie the cookie. * @arg data the data to read. * @arg size the size of the data to read. - * @return the error code (0 if ok). + * @return the size written and -1 if error (error code is in errno). */ -CASIO_LOCAL int casio_seven_data_read(seven_data_cookie_t *cookie, - unsigned char *data, size_t *psize) +CASIO_LOCAL size_t casio_seven_data_read(seven_data_cookie_t *cookie, + unsigned char *data, size_t size) { - int err; size_t tocopy; size_t size = *psize; + int err; size_t tocopy; casio_link_t *handle = cookie->_link; unsigned int lastsize = 0; + size_t copiedsize = 0; /* Check if the stream is faulty. */ if (cookie->_faulty) @@ -72,14 +72,16 @@ CASIO_LOCAL int casio_seven_data_read(seven_data_cookie_t *cookie, if (tocopy) { memcpy(data, &cookie->_current[cookie->_pos], tocopy); cookie->_pos += tocopy; - data += tocopy; size -= tocopy; + data += tocopy; size -= tocopy; copiedsize += tocopy; - if (!size) return (0); + if (size == 0) return (size); } /* Check if we have already finished. */ - if (cookie->_total && cookie->_id == cookie->_total) - return (casio_error_ieof); + if (cookie->_total && cookie->_id == cookie->_total) { + errno = casio_error_eof; + return (-1); + } /* Receive packets. */ while (size) { @@ -88,8 +90,7 @@ CASIO_LOCAL int casio_seven_data_read(seven_data_cookie_t *cookie, if (err) goto fail; /* If swap roles there is the end of file */ if (response.casio_seven_packet_type == casio_seven_type_swp) { - *psize = lastsize; - return (casio_error_ieof); + return copiedsize; } if (response.casio_seven_packet_type != casio_seven_type_data) { msg((ll_error, "Packet wasn't a data packet, wtf?")); @@ -117,9 +118,10 @@ CASIO_LOCAL int casio_seven_data_read(seven_data_cookie_t *cookie, /* Increment and copy. */ lastsize = response.casio_seven_packet_data_size; + copiedsize += lastsize; if (size >= lastsize) { memcpy(data, response.casio_seven_packet_data, lastsize); - data += lastsize; size -= lastsize; cookie->_totalsize += lastsize; + data += lastsize; size -= lastsize; continue; } @@ -128,15 +130,15 @@ CASIO_LOCAL int casio_seven_data_read(seven_data_cookie_t *cookie, memcpy(data, response.casio_seven_packet_data, size); memcpy(&cookie->_current[size], &response.casio_seven_packet_data[size], lastsize - size); - cookie->_totalsize += lastsize; - return (0); + return copiedsize; } - return (0); + return copiedsize; fail: /* XXX: tell the distant device we have a problem? */ cookie->_faulty = 1; - return (err); + errno = err; + return (-1); } /** @@ -348,7 +350,6 @@ int CASIO_EXPORT casio_seven_open_data_stream(casio_stream_t **stream, cookie->_id = 1; cookie->_total = (unsigned int)(size / BUFSIZE) + !!cookie->_lastsize; cookie->_lastsize = (unsigned int)(size % BUFSIZE); - cookie->_totalsize = size; if (!cookie->_lastsize) cookie->_lastsize = BUFSIZE; } else { msg((ll_info, "The data stream is a read one.")); @@ -358,7 +359,6 @@ int CASIO_EXPORT casio_seven_open_data_stream(casio_stream_t **stream, cookie->_id = 0; cookie->_total = 0; cookie->_lastsize = 0; - cookie->_totalsize = size; } /* initialize the stream */ diff --git a/lib/link/seven/receive.c b/lib/link/seven/receive.c index ce0a111..43754ad 100644 --- a/lib/link/seven/receive.c +++ b/lib/link/seven/receive.c @@ -175,10 +175,9 @@ CASIO_LOCAL const char *gettermstring(casio_seven_term_t code) #define buffer handle->casio_link_recv_buffer #define COMPLETE_PACKET(N) { \ - size_t size##__LINE__ = (size_t)N; \ int COMP_PACKET_err = casio_read(handle->casio_link_stream, \ - &buffer[received], &size##__LINE__); \ - received += size##__LINE__; if (COMP_PACKET_err) return (COMP_PACKET_err); } + &buffer[received], (size_t)N); \ + received += COMP_PACKET_err; if (COMP_PACKET_err == -1) return (errno); } CASIO_LOCAL int casio_seven_decode(casio_link_t *handle, int scralign) { diff --git a/lib/link/seven/scsi.c b/lib/link/seven/scsi.c index cc79567..86ea65a 100644 --- a/lib/link/seven/scsi.c +++ b/lib/link/seven/scsi.c @@ -83,24 +83,27 @@ typedef struct { * Read and write from the stream. * --- */ -CASIO_LOCAL int seven_scsi_read(seven_scsi_cookie_t *cookie, - unsigned char *buffer, size_t *size) +CASIO_LOCAL size_t seven_scsi_read(seven_scsi_cookie_t *cookie, + unsigned char *buffer, size_t size) { casio_scsi_t scsi; int err; + size_t copiedsize = 0; /* Empty what's already in the buffer. */ if (cookie->left) { - if (cookie->left >= *size) { - memcpy(buffer, cookie->ptr, *size); - cookie->ptr += *size; - cookie->left -= *size; - return (0); + if (cookie->left >= size) { + memcpy(buffer, cookie->ptr, size); + cookie->ptr += size; + cookie->left -= size; + copiedsize += cookie->left; + return copiedsize; } memcpy(buffer, cookie->ptr, cookie->left); buffer += cookie->left; - *size -= cookie->left; + size -= cookie->left; + copiedsize += cookie->left; reset_cookie(cookie); } @@ -125,8 +128,10 @@ CASIO_LOCAL int seven_scsi_read(seven_scsi_cookie_t *cookie, scsi.casio_scsi_data = poll_data; scsi.casio_scsi_data_len = 16; - if ((err = casio_scsi_request(cookie->stream, &scsi))) - return (err); + if ((err = casio_scsi_request(cookie->stream, &scsi))) { + errno = err; + return (-1); + } mem((ll_info, poll_data, 16)); @@ -151,14 +156,14 @@ CASIO_LOCAL int seven_scsi_read(seven_scsi_cookie_t *cookie, * We could also check that `avail < 0x10000` because we need to * express it later, but it is imposed by the source format. */ - if (avail > *size && *size <= cookie->size) { + if (avail > size && size <= cookie->size) { to = cookie->ptr; if (avail > cookie->size) avail = cookie->size; } else { to = buffer; - if (avail > *size) - avail = *size; + if (avail > size) + avail = size; } /* Actually get the data. */ @@ -176,24 +181,27 @@ CASIO_LOCAL int seven_scsi_read(seven_scsi_cookie_t *cookie, scsi.casio_scsi_data = cookie->ptr; scsi.casio_scsi_data_len = avail; - if ((err = casio_scsi_request(cookie->stream, &scsi))) - return (err); + if ((err = casio_scsi_request(cookie->stream, &scsi))) { + errno = err; + return (-1); + } } if (to == buffer) { buffer += avail; - *size -= avail; + size -= avail; } else { cookie->left = avail; - memcpy(buffer, cookie->ptr, *size); - cookie->ptr += *size; - cookie->left -= *size; - *size = 0; + memcpy(buffer, cookie->ptr, size); + cookie->ptr += size; + cookie->left -= size; + copiedsize += size; + size = 0; } - } while (*size); + } while (size); - return (0); + return copiedsize; } CASIO_LOCAL int seven_scsi_write(seven_scsi_cookie_t *cookie, diff --git a/lib/mcsfile/mcsfile.h b/lib/mcsfile/mcsfile.h index ee61550..8d68e99 100644 --- a/lib/mcsfile/mcsfile.h +++ b/lib/mcsfile/mcsfile.h @@ -26,20 +26,15 @@ /* Read from a stream. */ -/* FIXME : size_t size##__LINE__ = CASIO__SZ; - * Replace this by other thing that is more futur proof -*/ - # define READ(CASIO__TO, CASIO__SZ) /* normal read */ { \ - size_t size##__LINE__ = CASIO__SZ; \ - int READ_err = casio_read(buffer, (CASIO__TO), &size##__LINE__); \ - if (READ_err) return (READ_err); } + int READ_err = casio_read(buffer, (CASIO__TO), (CASIO__SZ)); \ + if (READ_err == -1) return (errno); } # define FREAD(CASIO__TO, CASIO__SZ) /* fail-less read */ { \ - size_t size##__LINE__ = CASIO__SZ; \ - err = casio_read(buffer, (CASIO__TO), &size##__LINE__); } + casio_read(buffer, (CASIO__TO), (CASIO__SZ)); \ + err = errno; } # define GREAD(CASIO__TO, CASIO__SZ) /* read with goto fail */ { \ - size_t size##__LINE__ = CASIO__SZ; \ - if ((err = casio_read(buffer, (CASIO__TO), &size##__LINE__))) \ + casio_read(buffer, (CASIO__TO), (CASIO__SZ)); \ + if ((err = errno)) \ goto fail; } /* Read using size of the object. */ diff --git a/lib/stream/builtin/csum32.c b/lib/stream/builtin/csum32.c index 3169f28..3e3a439 100644 --- a/lib/stream/builtin/csum32.c +++ b/lib/stream/builtin/csum32.c @@ -38,24 +38,26 @@ struct thecookie { * @arg cookie the cookie. * @arg data the data pointer. * @arg size the data size. - * @return the error code (0 if ok). + * @return the size written and -1 if error (error code is in errno). */ -CASIO_LOCAL int csum32_read(struct thecookie *cookie, - unsigned char *dest, size_t *psize) +CASIO_LOCAL size_t csum32_read(struct thecookie *cookie, + unsigned char *dest, size_t size) { int err; - size_t size = *psize; /* Make the call. */ - err = casio_read(cookie->_stream, dest, &size); - if (err) return (err); + size = casio_read(cookie->_stream, dest, size); + if (size == (size_t)-1) { + errno = errno; + return (-1); + } /* Make the checksum. */ *cookie->_checksum = casio_checksum32(dest, size, *cookie->_checksum); - return (0); + return size; } /** diff --git a/lib/stream/builtin/file.c b/lib/stream/builtin/file.c index 90fc051..b3deb5d 100644 --- a/lib/stream/builtin/file.c +++ b/lib/stream/builtin/file.c @@ -46,14 +46,14 @@ typedef struct { * @arg cookie the cookie. * @arg data the data pointer * @arg size the data size. - * @return the error code (0 if ok). + * @return the size written and -1 if error (error code is in errno). */ -CASIO_LOCAL int casio_file_read(file_cookie_t *cookie, - unsigned char *dest, size_t *psize) +CASIO_LOCAL size_t casio_file_read(file_cookie_t *cookie, + unsigned char *dest, size_t size) { size_t recv; - size_t size = *psize; + size_t copiedsize = 0; /* Main receiving loop. */ @@ -67,6 +67,7 @@ CASIO_LOCAL int casio_file_read(file_cookie_t *cookie, dest = (void*)((char*)dest + recv); size -= recv; + copiedsize += recv; /* Check the error. */ @@ -91,7 +92,8 @@ CASIO_LOCAL int casio_file_read(file_cookie_t *cookie, /* End of file. */ msg((ll_error, "encountered an end of file")); - return (casio_error_eof); + errno = casio_error_eof; + return (-1); case EINTR: /* alarm */ case ETIMEDOUT: @@ -101,21 +103,24 @@ CASIO_LOCAL int casio_file_read(file_cookie_t *cookie, /* A timeout has occurred. */ msg((ll_error, "timeout received")); - return (casio_error_timeout); + errno = casio_error_timeout; + return (-1); case ENODEV: case EPIPE: case ESPIPE: /* A device error has occured. */ msg((ll_error, "calculator was disconnected")); - return (casio_error_nocalc); + errno = casio_error_nocalc; + return (-1); default: msg((ll_fatal, "errno was %d: %s", errno, strerror(errno))); - return (casio_error_unknown); + errno = casio_error_unknown; + return (-1); } - return (0); + return copiedsize; } /** diff --git a/lib/stream/builtin/libusb/libusb.h b/lib/stream/builtin/libusb/libusb.h index b2dea05..75196b9 100644 --- a/lib/stream/builtin/libusb/libusb.h +++ b/lib/stream/builtin/libusb/libusb.h @@ -58,9 +58,9 @@ CASIO_EXTERN int CASIO_EXPORT casio_libusb_settm /* Character device callbacks. */ -CASIO_EXTERN int CASIO_EXPORT casio_libusb_read +CASIO_EXTERN size_t CASIO_EXPORT casio_libusb_read OF((cookie_libusb_t *casio__cookie, - unsigned char *casio__dest, size_t *casio__psize)); + unsigned char *casio__dest, size_t casio__size)); CASIO_EXTERN int CASIO_EXPORT casio_libusb_write OF((cookie_libusb_t *casio__cookie, const unsigned char *casio__data, size_t casio__size)); diff --git a/lib/stream/builtin/libusb/read.c b/lib/stream/builtin/libusb/read.c index e8badeb..9859394 100644 --- a/lib/stream/builtin/libusb/read.c +++ b/lib/stream/builtin/libusb/read.c @@ -26,15 +26,15 @@ * @arg vcookie the cookie (voided) * @arg data the data pointer. * @arg size the data size. - * @return the error code (0 if ok). + * @return the size written and -1 if error (error code is in errno). */ -int CASIO_EXPORT casio_libusb_read(cookie_libusb_t *cookie, - unsigned char *dest, size_t *psize) +size_t CASIO_EXPORT casio_libusb_read(cookie_libusb_t *cookie, + unsigned char *dest, size_t size) { int libusberr; size_t tocopy; - size_t size = *psize; + size_t copiedsize = 0; /* Transmit what's already in the buffer. */ @@ -46,6 +46,7 @@ int CASIO_EXPORT casio_libusb_read(cookie_libusb_t *cookie, cookie->_start += tocopy; dest += tocopy; size -= tocopy; + copiedsize += tocopy; } /* Main receiving loop. */ @@ -65,15 +66,18 @@ int CASIO_EXPORT casio_libusb_read(cookie_libusb_t *cookie, case LIBUSB_ERROR_NO_DEVICE: case LIBUSB_ERROR_IO: msg((ll_error, "The calculator is not here anymore :(")); - return (casio_error_nocalc); + errno = casio_error_nocalc; + return (-1); case LIBUSB_ERROR_TIMEOUT: - return (casio_error_timeout); + errno = casio_error_timeout; + return (-1); default: msg((ll_fatal, "libusb error was %d: %s", libusberr, libusb_strerror(libusberr))); - return (casio_error_unknown); + errno = casio_error_unknown; + return (-1); } /* Get the current size to copy. */ @@ -88,13 +92,17 @@ int CASIO_EXPORT casio_libusb_read(cookie_libusb_t *cookie, dest += tocopy; size -= tocopy; + /* Increment the total copied size */ + + copiedsize += tocopy; + /* Correct start and end points. */ cookie->_start = tocopy; cookie->_end = (size_t)recv - 1; } - return (0); + return copiedsize; } #endif diff --git a/lib/stream/builtin/limited.c b/lib/stream/builtin/limited.c index 5b6a244..d07a84d 100644 --- a/lib/stream/builtin/limited.c +++ b/lib/stream/builtin/limited.c @@ -37,35 +37,39 @@ typedef struct { * @arg vcookie the cookie (uncasted). * @arg data the data pointer. * @arg size the data size. - * @return the error code (0 if ok). + * @return the size written and -1 if error (error code is in errno). */ -CASIO_LOCAL int casio_limited_read(void *vcookie, unsigned char *dest, - size_t *psize) +CASIO_LOCAL size_t casio_limited_read(void *vcookie, unsigned char *dest, + size_t size) { int err; limited_cookie_t *cookie = (void*)vcookie; - size_t left; size_t size = *psize; + size_t left; size_t copiedsize; if (size > cookie->_left) { /* First, skip the required bytes. */ left = cookie->_left; cookie->_left = 0; - if ((err = casio_skip(cookie->_stream, left))) - return (err); + if ((err = casio_skip(cookie->_stream, left))) { + errno = err; + return (-1); + } /* Once the skip is done successfully, we return that we * have an EOF. */ - - return (casio_error_eof); + errno = casio_error_eof; + return (-1); } - if ((err = casio_read(cookie->_stream, dest, &size))) { + size = casio_read(cookie->_stream, dest, size); + if (size == (size_t)-1) { cookie->_left = 0; /* XXX: depends on the error? */ - return (err); + errno = errno; + return (-1); } cookie->_left -= size; - return (0); + return size; } /** diff --git a/lib/stream/builtin/memory.c b/lib/stream/builtin/memory.c index 0fd8b5a..33fa8e6 100644 --- a/lib/stream/builtin/memory.c +++ b/lib/stream/builtin/memory.c @@ -35,25 +35,27 @@ typedef struct { * @arg vcookie the cookie (uncasted) * @arg data the data pointer. * @arg size the data size. - * @return the error code (0 if ok). + * @return the size written and -1 if error (error code is in errno). */ -CASIO_LOCAL int casio_memory_read(void *vcookie, unsigned char *dest, size_t *psize) +CASIO_LOCAL size_t casio_memory_read(void *vcookie, unsigned char *dest, size_t size) { memory_cookie_t *cookie = (void*)vcookie; - size_t size = *psize; - if (((size_t)-1 - cookie->_offset) < size) /* overflow */ - return (casio_error_read); + if (((size_t)-1 - cookie->_offset) < size) { /* overflow */ + errno = casio_error_read; + return (-1); + } if (cookie->_offset + (casio_off_t)size > cookie->_size) { cookie->_offset = cookie->_size - 1; - return (casio_error_eof); + errno = casio_error_eof; + return (-1); } memcpy(dest, &cookie->_memory[cookie->_offset], size); cookie->_offset += size; - return (0); + return size; } /** diff --git a/lib/stream/builtin/streams/read.c b/lib/stream/builtin/streams/read.c index d3cf63a..2f61377 100644 --- a/lib/stream/builtin/streams/read.c +++ b/lib/stream/builtin/streams/read.c @@ -26,14 +26,14 @@ * @arg cookie the cookie. * @arg data the data pointer. * @arg size the data size. - * @return the error code (0 if ok). + * @return the size written and -1 if error (error code is in errno). */ -int CASIO_EXPORT casio_streams_read(streams_cookie_t *cookie, - unsigned char *dest, size_t *psize) +size_t CASIO_EXPORT casio_streams_read(streams_cookie_t *cookie, + unsigned char *dest, size_t size) { int fd = cookie->_readfd; - size_t size = *psize; + size_t copiedsize = 0; /* Transmit what's already in the buffer. */ @@ -45,6 +45,7 @@ int CASIO_EXPORT casio_streams_read(streams_cookie_t *cookie, cookie->_start += tocopy; dest += tocopy; size -= tocopy; + copiedsize += tocopy; } /* Main receiving loop. */ @@ -68,7 +69,8 @@ int CASIO_EXPORT casio_streams_read(streams_cookie_t *cookie, default: msg((ll_fatal, "error was %d: %s", errno, strerror(errno))); - return (casio_error_unknown); + errno = casio_error_unknown; + return (-1); } /* Get the current size to copy. */ @@ -82,13 +84,17 @@ int CASIO_EXPORT casio_streams_read(streams_cookie_t *cookie, dest += tocopy; size -= tocopy; + /* Increment total size copied */ + + copiedsize += tocopy; + /* Correct start and end points. */ cookie->_start = tocopy; cookie->_end = (size_t)recv - 1; } - return (0); + return copiedsize; } #endif diff --git a/lib/stream/builtin/streams/streams.h b/lib/stream/builtin/streams/streams.h index d1e71eb..d884a1c 100644 --- a/lib/stream/builtin/streams/streams.h +++ b/lib/stream/builtin/streams/streams.h @@ -51,9 +51,9 @@ CASIO_EXTERN int CASIO_EXPORT casio_streams_settm /* Character device callbacks. */ -CASIO_EXTERN int CASIO_EXPORT casio_streams_read +CASIO_EXTERN size_t CASIO_EXPORT casio_streams_read OF((streams_cookie_t *casio__cookie, - unsigned char *casio__dest, size_t *casio__psize)); + unsigned char *casio__dest, size_t casio__size)); CASIO_EXTERN int CASIO_EXPORT casio_streams_write OF((streams_cookie_t *casio__cookie, const unsigned char *casio__data, size_t casio__size)); diff --git a/lib/stream/read.c b/lib/stream/read.c index 3a70350..9c1028e 100644 --- a/lib/stream/read.c +++ b/lib/stream/read.c @@ -25,32 +25,37 @@ * @arg stream the stream to read from. * @arg dest the destination buffer. * @arg size the amount of bytes to read. - * @return the error code (0 if ok). + * @return the size written and -1 if error (error code is in errno). */ -int CASIO_EXPORT casio_read(casio_stream_t *stream, void *dest, size_t *psize) +size_t CASIO_EXPORT casio_read(casio_stream_t *stream, void *dest, size_t size) { - int err; + int err = casio_error_ok; /* check if we can read */ failure(~stream->casio_stream_mode & CASIO_OPENMODE_READ, casio_error_read) /* read */ - if (!(*psize)) return (0); - if (!psize) { - size_t size = -1; - err = (*getcb(stream, read))(stream->casio_stream_cookie, dest, &size); - } else { - err = (*getcb(stream, read))(stream->casio_stream_cookie, dest, psize); - } - if (err && err != casio_error_ieof) { + if (size == 0) return (0); + + size = (*getcb(stream, read))(stream->casio_stream_cookie, dest, size); + + if (size == (size_t)-1) { + err = errno; + if (err == casio_error_eof) { + msg((ll_info, "Stream reading get to the end (EOF)")); + goto fail; + } msg((ll_error, "Stream reading failure: %s", casio_strerror(err))); goto fail; } /* move the cursor and return */ - stream->casio_stream_offset += *psize; + if(size != 1) { + stream->casio_stream_offset += size; + } fail: stream->casio_stream_lasterr = err; - return (err); + errno = err; + return size; } diff --git a/lib/stream/seek.c b/lib/stream/seek.c index 419e53f..c688d0d 100644 --- a/lib/stream/seek.c +++ b/lib/stream/seek.c @@ -63,9 +63,11 @@ int CASIO_EXPORT casio_seek(casio_stream_t *stream, casio_off_t offset, size_t rd = min(to_skip, 128); /* Read that much from the stream. */ - - if ((err = casio_read(stream, buf, &rd))) + size_t readed = casio_read(stream, buf, rd); + if (readed == (size_t)-1) { + err = errno; goto fail; + } to_skip -= rd; } while (to_skip); diff --git a/src/p7/main.c b/src/p7/main.c index b87943c..1794a6b 100644 --- a/src/p7/main.c +++ b/src/p7/main.c @@ -316,11 +316,11 @@ int main(int ac, char **av) size_t size; do { - size = sizeof(buffer); - err = casio_read(fileStream, buffer, &size); + size = casio_read(fileStream, buffer, sizeof(buffer)); + if(size == (size_t)-1) + err = errno; + if(err == casio_error_eof) fwrite(buffer, 1, size, file); - if(err == casio_error_ieof) - break; } while (err == 0); /* All good so close streams and clear error */ diff --git a/src/p7/main.h b/src/p7/main.h index 2242152..f48fe23 100644 --- a/src/p7/main.h +++ b/src/p7/main.h @@ -20,6 +20,7 @@ # define MAIN_H # include # include +# include /* Menus */ typedef enum { diff --git a/src/p7os/procs/fxremote_flash.c b/src/p7os/procs/fxremote_flash.c index 7d53bb0..7fdc13c 100644 --- a/src/p7os/procs/fxremote_flash.c +++ b/src/p7os/procs/fxremote_flash.c @@ -60,7 +60,7 @@ struct data_copy_block { static int send_sector(casio_link_t *handle, casio_stream_t *buffer, unsigned long addr, unsigned long size) { - int err, buf_err; + int err; osdisp_t osdisp_cookie; /* Sending the data to the Update.Exe's. */ @@ -83,7 +83,8 @@ static int send_sector(casio_link_t *handle, casio_stream_t *buffer, /* Prepare the block. */ block.destination = casio_be32toh(buf); block.length = casio_be32toh(len); - if ((buf_err = casio_read(buffer, block.data, &len))) { + len = casio_read(buffer, block.data, len); + if (len == (size_t)-1) { osdisp_interrupt(&osdisp_cookie); return (casio_error_read); } diff --git a/src/p7os/procs/std_prepare.c b/src/p7os/procs/std_prepare.c index 6663169..5e13a80 100644 --- a/src/p7os/procs/std_prepare.c +++ b/src/p7os/procs/std_prepare.c @@ -55,7 +55,7 @@ int prepare(args_t *args) /* Sleep while the software on the calculator sets up the * communication interface. */ printf("Waiting for the Update.Exe to set up the communication...\n"); - casio_sleep(1000); + casio_sleep(3000); err = 0; fail: