Use getopt for program parameters
Signed-off-by: Thomas Heijligen <src@posteo.de>
diff --git a/src/blockdev.c b/src/blockdev.c
index 443efcd..43c990c 100644
--- a/src/blockdev.c
+++ b/src/blockdev.c
@@ -1,5 +1,7 @@
#include <stdio.h>
+#include <errno.h>
+#include <string.h>
#include "blockdev.h"
@@ -11,10 +13,19 @@
{
block_device = fopen(name, "rwb");
if (!block_device) {
+ fprintf(stderr, "devopen: %s.\n", strerror(errno));
return 0;
}
- fseek(block_device, 0, SEEK_END);
+ int seek_res = 0;
+ seek_res |= fseek(block_device, 0, SEEK_END);
device_size = ftell(block_device);
+ seek_res |= fseek(block_device, 0, SEEK_SET);
+ if (seek_res || device_size == -1L || device_size % 512) {
+ fprintf(stderr, "devopen: Bad file %s.\n", name);
+ devclose();
+ return 0;
+ }
+
return 1;
}