log-writes: Add support to output human readable flags
[xfstests-dev.git] / src / pwrite_mmap_blocked.c
1 /*    Copyright (c) 2010 Intel Corporation
2  *
3  *    This program is free software; you can redistribute it and/or modify it
4  *    under the terms of the GNU General Public License as published by the Free
5  *    Software Foundation; version 2 of the License
6  *
7  *    This program is distributed in the hope that it will be useful, but
8  *    WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
9  *    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
10  *    for more details.
11  *
12  *    You should have received a copy of the GNU General Public License along
13  *    with this program; if not, write to the Free Software Foundation, Inc., 59
14  *    Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15  */
16
17 #include <stdio.h>
18 #include <unistd.h>
19 #include <fcntl.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <errno.h>
23 #include <time.h>
24 #include <sys/mman.h>
25 #include <signal.h>
26 #include <sys/stat.h>
27
28
29
30 int main(int argc, char *argv[])
31 {
32         int ret;
33         char *cc = "01234";
34         char *progname;
35         loff_t size;
36         loff_t amount = 1;
37         loff_t from = 2;
38         loff_t to = 3;
39         int fd;
40         void *mapped_mem;
41
42         progname = argv[0];
43         size = 5;
44         fd = open(argv[1], O_RDWR|O_TRUNC|O_CREAT, 0666);
45         if (fd < 0) {
46                 fprintf(stderr, "%s: Cannot open `%s': %s\n",
47                         progname, argv[1], strerror(errno));
48                 exit(1);
49         }
50
51         if ((ret = pwrite(fd, (const char *)cc,
52                                 size, 0)) != size) {
53                 perror("pwrite");
54                 exit(1);
55         }
56
57         mapped_mem = mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
58         if (mapped_mem == MAP_FAILED) {
59                 perror("mmap");
60                 exit(1);
61         }
62         printf("pwrite %Ld bytes from %Ld to %Ld\n",
63                 (long long) amount, (long long) from, (long long) to);
64
65         ret = pwrite(fd, (char *)mapped_mem + from, amount, to);
66         if (ret != amount) {
67                 perror("pwrite");
68                 exit(1);
69         }
70
71         munmap(mapped_mem,0);
72         close(fd);
73         exit(0);
74 }