X-Git-Url: https://git.draconx.ca/gitweb/liblbx.git/blobdiff_plain/f228da9594290408ee03ffddbd01eadefbf06f1d..4a5ac8356dbc71d84ad5e6b04499597eedf0d89d:/src/lbximg.c
diff --git a/src/lbximg.c b/src/lbximg.c
index 50dff07..4b664b7 100644
--- a/src/lbximg.c
+++ b/src/lbximg.c
@@ -16,7 +16,6 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
-#define _GNU_SOURCE
#include
#include
#include
@@ -365,11 +364,11 @@ err:
int main(int argc, char **argv)
{
- int mode = MODE_NONE;
- FILE *inf = stdin, *palf = NULL, *overf = NULL;
+ int mode = MODE_NONE, opt, rc = EXIT_FAILURE;
+ struct lbx_pipe_state state = { .f = stdin };
+ FILE *palf = NULL, *overf = NULL;
const char *name = "stdin";
LBX_IMG *img;
- int opt;
static const char *sopts = "idvf:p:O:V";
static const struct option lopts[] = {
@@ -402,14 +401,10 @@ int main(int argc, char **argv)
verbose = 1;
break;
case 'f':
- if (strcmp(optarg, "-") == 0)
- break;
-
name = strrchr(optarg, '/');
name = name ? name+1 : optarg;
- inf = fopen(optarg, "rb");
- if (!inf) {
+ if (!freopen(optarg, "rb", state.f)) {
errmsg("failed to open %s: %m\n", optarg);
return EXIT_FAILURE;
}
@@ -449,7 +444,11 @@ int main(int argc, char **argv)
return EXIT_FAILURE;
}
- img = lbximg_fopen(inf);
+ if (fseek(state.f, 0, SEEK_CUR) == 0)
+ img = lbximg_open(state.f, &lbx_default_fops, NULL);
+ else
+ img = lbximg_open(&state, &lbx_pipe_fops, NULL);
+
if (!img) {
errmsg("failed to open image: %s.\n", lbx_strerror());
return EXIT_FAILURE;
@@ -467,13 +466,10 @@ int main(int argc, char **argv)
switch (mode) {
case MODE_DECODE:
- if (decode(img, palf, overf, &argv[optind])) {
- lbximg_close(img);
- return EXIT_FAILURE;
- }
+ rc = decode(img, palf, overf, &argv[optind]);
break;
}
lbximg_close(img);
- return EXIT_SUCCESS;
+ return rc;
}