Import Upstream version 1.8.5
[hcoop/debian/openafs.git] / src / tests / read-vs-mmap2.c
1 /*
2 * Copyright (c) 1995 - 2001 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <sys/types.h>
41 #include <fcntl.h>
42 #include <time.h>
43 #include <unistd.h>
44 #include <string.h>
45 #include <sys/mman.h>
46 #include <sys/stat.h>
47 #include <err.h>
48
49 static void
50 generate_random_file(const char *filename, size_t sz)
51 {
52 int fd;
53 char *buf;
54 int i;
55
56 buf = malloc(sz);
57 if (buf == NULL)
58 err(1, "malloc %u", (unsigned)sz);
59
60 fd = open(filename, O_WRONLY | O_CREAT, 0666);
61 if (fd < 0)
62 err(1, "open %s", filename);
63
64 for (i = 0; i < sz; ++i)
65 buf[i] = rand();
66
67 if (write(fd, buf, sz) != sz)
68 err(1, "write");
69 if (close(fd))
70 err(1, "close");
71 free(buf);
72 }
73
74 static char *
75 read_file(int fd, size_t sz)
76 {
77 char *buf;
78 ssize_t ret;
79
80 buf = malloc(sz);
81 if (buf == NULL)
82 err(1, "malloc %u", (unsigned)sz);
83 ret = read(fd, buf, sz);
84 if (ret < 0)
85 err(1, "read");
86 if (ret != sz)
87 errx(1, "short read %d < %u", (int)ret, (unsigned)sz);
88 return buf;
89 }
90
91 #ifndef MAP_FAILED
92 #define MAP_FAILED ((void *)-1)
93 #endif
94
95 static void *
96 mmap_file(int fd, size_t sz)
97 {
98 void *ret;
99
100 ret = mmap(0, sz, PROT_READ, MAP_SHARED, fd, 0);
101 if (ret == (void *)MAP_FAILED)
102 err(1, "mmap");
103 return ret;
104 }
105
106 int
107 main(int argc, char **argv)
108 {
109 const char *file = "foo";
110 const size_t sz = 16384;
111 char *malloc_buf;
112 void *mmap_buf;
113 int fd;
114
115 srand(time(NULL));
116
117 generate_random_file(file, sz);
118
119 fd = open(file, O_RDONLY, 0);
120 if (fd < 0)
121 err(1, "open %s", file);
122
123 malloc_buf = read_file(fd, sz);
124 mmap_buf = mmap_file(fd, sz);
125 close(fd);
126 unlink(file);
127 if (memcmp(malloc_buf, mmap_buf, sz) != 0)
128 return 1;
129 return 0;
130 }