X-Git-Url: https://git.draconx.ca/gitweb/liblbx.git/blobdiff_plain/81d9da9a3ab660a3ce6ac1f1660024f2b77dfc95..59d43aadba53b164dc529cfef069c5aeb9a65807:/src/lbx.c diff --git a/src/lbx.c b/src/lbx.c index 1d92f46..f96344d 100644 --- a/src/lbx.c +++ b/src/lbx.c @@ -1,7 +1,7 @@ /* * 2ooM: The Master of Orion II Reverse Engineering Project * Library for working with LBX archive files. - * Copyright (C) 2006-2010 Nick Bowler + * Copyright © 2006-2010, 2013-2014 Nick Bowler * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -166,14 +166,17 @@ struct lbx *lbx_fopen(const char *file) FILE *f; f = fopen(file, "rb"); - if (!f) + if (!f) { + lbx_error_raise(-errno); return NULL; + } if (fseek(f, 0, SEEK_CUR) == 0) return lbx_open(f, &lbx_default_fops, file_close, name); p = malloc(sizeof *p); if (!p) { + lbx_error_raise(LBX_ENOMEM); fclose(f); return NULL; } @@ -182,19 +185,13 @@ struct lbx *lbx_fopen(const char *file) return lbx_open(p, &lbx_pipe_fops, pipe_close, name); } -size_t lbx_numfiles(struct lbx *pub) -{ - return pub->nfiles; -} - -int -lbx_file_stat(struct lbx *pub, unsigned fileno, struct lbx_statbuf *buf) +int lbx_file_stat(struct lbx *pub, unsigned fileno, struct lbx_statbuf *buf) { struct lbx_priv *lbx = (struct lbx_priv *)pub; static char str[256]; /* FIXME */ if (fileno >= lbx->pub.nfiles) { - lbx_error_raise(LBX_ENOENT); + lbx_error_raise(LBX_EINVAL); buf->name = NULL; return -1; } @@ -224,7 +221,7 @@ struct lbx_file_state *lbx_file_open(struct lbx *pub, unsigned fileno) struct lbx_file_state *state; if (fileno >= lbx->pub.nfiles) { - lbx_error_raise(LBX_ENOENT); + lbx_error_raise(LBX_EINVAL); return NULL; } @@ -265,8 +262,13 @@ size_t lbx_file_read(struct lbx_file_state *f, void *buf, size_t n) rc = fops->read(buf, want, f->lbx->f); f->offset += rc; - if (want < n || (rc < want && fops->eof(f->lbx->f))) + if (rc < want) { + if (fops->eof(f->lbx->f)) + lbx_error_raise(LBX_EEOF); + } else if (rc < n) { f->eof = 1; + } + return rc; } @@ -285,10 +287,15 @@ int lbx_file_seek(struct lbx_file_state *f, long offset, int whence) case SEEK_END: pos = f->limit + offset; break; + default: + lbx_error_raise(LBX_EINVAL); + return -1; } - if (pos > f->limit) + if (pos > f->limit) { + lbx_error_raise(LBX_EINVAL); return -1; + } f->lbx->last_file = NULL; if (fops->seek(f->lbx->f, f->base + pos, SEEK_SET) != 0)