aboutsummaryrefslogtreecommitdiffstats
path: root/src/examples/mcat.c
blob: 7b397601045cb8f24c4b00e055b26fbc851ebf91 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/* mcat.c

   Prints files specified on command line to the console, using
   mmap. */

#include <stdio.h>
#include <syscall.h>

int
main (int argc, char *argv[]) 
{
  int i;
  
  for (i = 1; i < argc; i++) 
    {
      int fd;
      mapid_t map;
      void *data = (void *) 0x10000000;
      int size;

      /* Open input file. */
      fd = open (argv[i]);
      if (fd < 0) 
        {
          printf ("%s: open failed\n", argv[i]);
          return EXIT_FAILURE;
        }
      size = filesize (fd);

      /* Map files. */
      map = mmap (fd, data);
      if (map == MAP_FAILED) 
        {
          printf ("%s: mmap failed\n", argv[i]);
          return EXIT_FAILURE;
        }

      /* Write file to console. */
      write (STDOUT_FILENO, data, size);

      /* Unmap files (optional). */
      munmap (map);
    }
  return EXIT_SUCCESS;
}