Commits

catseye  committed 834063c

Fix up the mmap() example a bit.

  • Participants
  • Parent commits cafb33b

Comments (0)

Files changed (1)

 #include <sys/types.h>
 
 #include <stdio.h>
-#include <stdlib.h>
 #include <fcntl.h>
+#include <unistd.h>
 
 int main(int argc, char **argv)
 {
     fd = open(argv[1], O_RDONLY);
     if (fd == -1) {
 	perror("Can't open file for reading");
-	exit(1);
+	return 1;
     }
     fstat(fd, &fileinfo);
 
-    buffer = mmap(0, fileinfo.st_size, PROT_READ, MAP_SHARED, fd, 0);
-    if (buffer == MAP_FAILED) {
-	close(fd);
-	perror("Can't mmap the file");
-	exit(1);
+    if (fileinfo.st_size > 0) {
+        buffer = mmap(0, fileinfo.st_size, PROT_READ, MAP_SHARED, fd, 0);
+        if (buffer == MAP_FAILED) {
+            close(fd);
+            perror("Can't mmap the file");
+            return 1;
+        }
+
+        ptr = buffer;
+        for (i = 0; i < fileinfo.st_size; i++) {
+            printf("%c", *ptr);
+            ptr++;
+        }
+
+        if (munmap(buffer, fileinfo.st_size) == -1) {
+            perror("Can't un-mmap the file");
+        }
     }
 
-    ptr = buffer;
-    for (i = 0; i < fileinfo.st_size; i++) {
-        printf("%c", *ptr);
-        ptr++;
-    }
-
-    if (munmap(buffer, fileinfo.st_size) == -1) {
-	perror("Can't un-mmap the file");
-    }
     close(fd);
     return 0;    
 }