]> git.draconx.ca Git - liblbx.git/blobdiff - src/fops.c
liblbx: Fix uninitialized value warning in pipe_seek.
[liblbx.git] / src / fops.c
index 834c8b2e776ef0f7c22bdd0ff3b3cc080ced3e04..5ae349faa33ea6057b8c51dc475a57e79b03d572 100644 (file)
  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 #include <stdio.h>
+#include <errno.h>
+#include <assert.h>
 
 #include "misc.h"
+#include "error.h"
 #include "lbx.h"
 
 /* Default I/O operations for ordinary files. */
 static size_t file_read(void *buf, size_t size, void *handle)
 {
-       return fread(buf, 1, size, (FILE *)handle);
+       size_t rc = fread(buf, 1, size, (FILE *)handle);
+
+       if (rc < size && ferror((FILE *)handle))
+               lbx_error_raise(-errno);
+       return rc;
 }
 
 static int file_seek(void *handle, long offset, int whence)
 {
-       return fseek((FILE *)handle, offset, whence);
+       if (fseek((FILE *)handle, offset, whence) == -1) {
+               lbx_error_raise(-errno);
+               return -1;
+       }
+
+       return 0;
 }
 
 static long file_tell(void *handle)
@@ -55,7 +67,7 @@ static size_t pipe_read(void *buf, size_t size, void *handle)
        struct lbx_pipe_state *state = handle;
        size_t rc;
 
-       rc = fread(buf, 1, size, state->f);
+       rc = file_read(buf, size, state->f);
        state->offset += rc;
        return rc;
 }
@@ -76,6 +88,8 @@ static int pipe_seek(void *handle, long offset, int whence)
        case SEEK_END:
                distance = -1;
                break;
+       default:
+               assert(0);
        }
 
        if (distance < 0)
@@ -92,6 +106,7 @@ static int pipe_seek(void *handle, long offset, int whence)
                        return -1;
        }
 
+       clearerr(state->f);
        return 0;
 }
 
@@ -115,3 +130,30 @@ const struct lbx_file_ops lbx_pipe_fops = {
        .tell = pipe_tell,
        .eof  = pipe_eof,
 };
+
+static size_t lbx_read(void *buf, size_t size, void *handle)
+{
+       return lbx_file_read(handle, buf, size);
+}
+
+static int lbx_seek(void *handle, long offset, int whence)
+{
+       return lbx_file_seek(handle, offset, whence);
+}
+
+static long lbx_tell(void *handle)
+{
+       return lbx_file_tell(handle);
+}
+
+static int lbx_eof(void *handle)
+{
+       return lbx_file_eof(handle);
+}
+
+const struct lbx_file_ops lbx_arch_fops = {
+       .read = lbx_read,
+       .seek = lbx_seek,
+       .tell = lbx_tell,
+       .eof  = lbx_eof,
+};