check alloc
[dos_imgv] / src / main.c
index 7ab236f..31e8a71 100644 (file)
@@ -5,6 +5,11 @@
 #include "video.h"
 #include "mouse.h"
 #include "imago2.h"
+#include "chkalloc.h"
+
+int switch_mode(int m);
+int find_best_mode(int minx, int miny);
+void drop_equiv_modes(void);
 
 void display8(void);
 void display16(void);
@@ -26,6 +31,7 @@ static struct video_mode *vmlist;
 static int vmlist_size, cur_vm = -1;
 static void *backbuf;
 
+static int opt_width, opt_height;
 static int opt_bpp = 16;
 static const char *opt_fname;
 
@@ -57,7 +63,7 @@ int main(int argc, char **argv)
        num = num_video_modes();
        vmodes = video_modes();
 
-       if(!(vmlist = malloc(num * sizeof *vmlist))) {
+       if(!(vmlist = chk_malloc(num * sizeof *vmlist))) {
                fprintf(stderr, "failed to allocate video mode list\n");
                return 1;
        }
@@ -96,7 +102,11 @@ int main(int argc, char **argv)
 
        init_mouse();
 
-       modeidx = find_best_mode(img.width, img.height);
+       if(!opt_width) {
+               opt_width = img.width;
+               opt_height = img.height;
+       }
+       modeidx = find_best_mode(opt_width, opt_height);
        if(switch_mode(modeidx) == -1) {
                goto end;
        }
@@ -146,12 +156,13 @@ int main(int argc, char **argv)
 end:
 
        img_destroy(&img);
-       free(backbuf);
-       free(vmlist);
+       chk_free(backbuf);
+       chk_free(vmlist);
        if(cur_vm >= 0) {
                set_text_mode();
        }
        cleanup_video();
+       chk_check();
        return 0;
 }
 
@@ -160,8 +171,8 @@ int switch_mode(int m)
        vmode = vmlist + m;
        cur_vm = m;
 
-       free(backbuf);
-       if(!(backbuf = malloc(vmode->ysz * vmode->pitch))) {
+       chk_free(backbuf);
+       if(!(backbuf = chk_malloc(vmode->ysz * vmode->pitch))) {
                fprintf(stderr, "failed to allocate back buffer\n");
                return -1;
        }
@@ -335,12 +346,19 @@ int parse_args(int argc, char **argv)
        int i;
        static const char *usage_fmt = "Usage: %s [options] <image file>\n"
                "Options:\n"
+               " -s <WxH>: choose screen resolution if available\n"
                " -bpp <n>: video mode color depth (8, 15, 16, 24, 32)\n"
                " -h,-help: print usage information and exit\n";
 
        for(i=1; i<argc; i++) {
                if(argv[i][0] == '-') {
-                       if(strcmp(argv[i], "-bpp") == 0) {
+                       if(strcmp(argv[i], "-s") == 0) {
+                               if(!argv[++i] || sscanf(argv[i], "%dx%d", &opt_width, &opt_height) != 2 ||
+                                               opt_width <= 0 || opt_height <= 0) {
+                                       fprintf(stderr, "invalid resolution\n");
+                                       return -1;
+                               }
+                       } else if(strcmp(argv[i], "-bpp") == 0) {
                                if(!argv[++i] || (opt_bpp = atoi(argv[i])) <= 0) {
                                        fprintf(stderr, "invalid -bpp\n");
                                        return -1;