summaryrefslogtreecommitdiff
path: root/slurp/mmap.c (plain)
blob: 9a566512bf65d687b1e8cdaa62592ac62356e357
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/fcntl.h>
 
int verbose=0;
void usage(char *prog) {
  fprintf(stderr, "usage: %s [-v] <file>\n", prog);
  exit(-1);
}
 
char *map(char *file, size_t *len) {
  struct stat s;
  char *buf;
  int fd;

  if ( (fd = open(file, O_RDONLY)) == -1) {
      fprintf(stderr,"can't open %s: %s\n", file, strerror(errno));
      exit(-1);
  }
  if (fstat(fd, &s) == -1) {
      close(fd);
      fprintf(stderr,"can't stat %s: %s\n", file, strerror(errno));
      exit(-1);
  }
  *len = s.st_size;
  buf = mmap(0, s.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
  if (buf == MAP_FAILED) {
    close(fd);
    fprintf(stderr, "failed to mmap %s: %s\n", file, strerror(errno));
    exit(-1);
  }
  /* don't: close(fd); */
  return buf;
}

int main(int argc, char * argv[]) {
  int opt;
  char *file, *buf;
  size_t len;;
 
  while ( (opt = getopt(argc, argv, "v+")) != -1) {
    switch (opt) {
      case 'v':
        verbose++;
        break;
      default:
        usage(argv[0]);
        break;
    }
  }
 
  if (optind < argc) file=argv[optind++];
  else usage(argv[0]);
  buf = map(file, &len);
  printf("mmap'd %s: %u bytes\n", file, (unsigned)len);
}