picoproj: replace mmap() with more portable file stream operations
authorAntonio Ospite <ospite@studenti.unina.it>
Thu, 10 May 2012 10:13:03 +0000 (12:13 +0200)
committerAntonio Ospite <ospite@studenti.unina.it>
Thu, 10 May 2012 13:58:00 +0000 (15:58 +0200)
This makes picoproj compile for Windows, for instance.

examples/picoproj.c

index 79927c2..8503612 100644 (file)
@@ -24,7 +24,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <sys/mman.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
@@ -58,7 +57,7 @@ int main(int argc, char *argv[])
        int opt;
 
        char filename[FILENAME_MAX] = {0};
        int opt;
 
        char filename[FILENAME_MAX] = {0};
-       int image_fd;
+       FILE *image_fp;
        struct stat st;
        am7xxx_context *ctx;
        am7xxx_device *dev;
        struct stat st;
        am7xxx_context *ctx;
        am7xxx_device *dev;
@@ -144,31 +143,43 @@ int main(int argc, char *argv[])
                goto out;
        }
 
                goto out;
        }
 
-       image_fd = open(filename, O_RDONLY);
-       if (image_fd < 0) {
-               perror("open");
+       image_fp = fopen(filename, "rb");
+       if (image_fp == NULL) {
+               perror("fopen");
                exit_code = EXIT_FAILURE;
                goto out;
        }
                exit_code = EXIT_FAILURE;
                goto out;
        }
-       if (fstat(image_fd, &st) < 0) {
+       if (fstat(fileno(image_fp), &st) < 0) {
                perror("fstat");
                exit_code = EXIT_FAILURE;
                perror("fstat");
                exit_code = EXIT_FAILURE;
-               goto out_close_image_fd;
+               goto out_close_image_fp;
        }
        size = st.st_size;
 
        }
        size = st.st_size;
 
-       image = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, image_fd, 0);
+       image = malloc(size * sizeof(unsigned char));
        if (image == NULL) {
        if (image == NULL) {
-               perror("mmap");
+               perror("malloc");
                exit_code = EXIT_FAILURE;
                exit_code = EXIT_FAILURE;
-               goto out_close_image_fd;
+               goto out_close_image_fp;
+       }
+
+       ret = fread(image, size, 1, image_fp);
+       if (ret != 1) {
+               if (feof(image_fp))
+                       fprintf(stderr, "Unexpected end of file.\n");
+               else if (ferror(image_fp))
+                       perror("fread");
+               else
+                       fprintf(stderr, "Unexpected error condition.\n");
+
+               goto out_free_image;
        }
 
        ret = am7xxx_init(&ctx);
        if (ret < 0) {
                perror("am7xxx_init");
                exit_code = EXIT_FAILURE;
        }
 
        ret = am7xxx_init(&ctx);
        if (ret < 0) {
                perror("am7xxx_init");
                exit_code = EXIT_FAILURE;
-               goto out_munmap;
+               goto out_free_image;
        }
 
        am7xxx_set_log_level(ctx, log_level);
        }
 
        am7xxx_set_log_level(ctx, log_level);
@@ -227,15 +238,13 @@ int main(int argc, char *argv[])
 cleanup:
        am7xxx_shutdown(ctx);
 
 cleanup:
        am7xxx_shutdown(ctx);
 
-out_munmap:
-       ret = munmap(image, size);
-       if (ret < 0)
-               perror("munmap");
+out_free_image:
+       free(image);
 
 
-out_close_image_fd:
-       ret = close(image_fd);
-       if (ret < 0)
-               perror("close");
+out_close_image_fp:
+       ret = fclose(image_fp);
+       if (ret == EOF)
+               perror("fclose");
 
 out:
        exit(exit_code);
 
 out:
        exit(exit_code);