diff options
author | Daniel Baumann <mail@daniel-baumann.ch> | 2015-11-07 15:29:08 +0000 |
---|---|---|
committer | Daniel Baumann <mail@daniel-baumann.ch> | 2015-11-07 15:29:08 +0000 |
commit | ebfebbfb4b77fa42f84e07b6fe671c56cdd12a35 (patch) | |
tree | a55e5d8f3b0da9372f59932cbd3079309a55fecc /compress.cc | |
parent | Adding upstream version 0.8. (diff) | |
download | plzip-ebfebbfb4b77fa42f84e07b6fe671c56cdd12a35.tar.xz plzip-ebfebbfb4b77fa42f84e07b6fe671c56cdd12a35.zip |
Adding upstream version 0.9.upstream/0.9
Signed-off-by: Daniel Baumann <mail@daniel-baumann.ch>
Diffstat (limited to 'compress.cc')
-rw-r--r-- | compress.cc | 45 |
1 files changed, 36 insertions, 9 deletions
diff --git a/compress.cc b/compress.cc index cf0135a..bb50358 100644 --- a/compress.cc +++ b/compress.cc @@ -35,15 +35,42 @@ #include "plzip.h" -#ifndef LLONG_MAX -#define LLONG_MAX 0x7FFFFFFFFFFFFFFFLL -#endif -#ifndef LLONG_MIN -#define LLONG_MIN (-LLONG_MAX - 1LL) -#endif -#ifndef ULLONG_MAX -#define ULLONG_MAX 0xFFFFFFFFFFFFFFFFULL -#endif + +// Returns the number of bytes really read. +// If (returned value < size) and (errno == 0), means EOF was reached. +// +int readblock( const int fd, uint8_t * const buf, const int size ) + { + int rest = size; + errno = 0; + while( rest > 0 ) + { + errno = 0; + const int n = read( fd, buf + size - rest, rest ); + if( n > 0 ) rest -= n; + else if( n == 0 ) break; + else if( errno != EINTR && errno != EAGAIN ) break; + } + return ( rest > 0 ) ? size - rest : size; + } + + +// Returns the number of bytes really written. +// If (returned value < size), it is always an error. +// +int writeblock( const int fd, const uint8_t * const buf, const int size ) + { + int rest = size; + errno = 0; + while( rest > 0 ) + { + errno = 0; + const int n = write( fd, buf + size - rest, rest ); + if( n > 0 ) rest -= n; + else if( n < 0 && errno != EINTR && errno != EAGAIN ) break; + } + return ( rest > 0 ) ? size - rest : size; + } void xinit( pthread_mutex_t * const mutex ) |