open_by_handle: make -h (help) a valid option
[xfstests-dev.git] / src / t_mmap_writev.c
1 /*
2     mmap() a file and writev() back to another file
3          - kernel bug #22452 testcase
4
5     Copyright (C) 2010
6          by D.Buczek - Max Planck Institute for Molecular Genetics Berlin
7
8     This program is free software; you can redistribute it and/or modify
9     it under the terms of the GNU General Public License as published by
10     the Free Software Foundation; either version 2 of the License, or
11     (at your option) any later version.
12
13     This program is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17
18     You should have received a copy of the GNU General Public License along
19     with this program; if not, write to the Free Software Foundation, Inc.,
20     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23 #include <sys/types.h>
24 #include <fcntl.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27
28 #include <sys/mman.h>
29 #include <string.h>
30 #include <sys/uio.h>
31 #include <unistd.h>
32
33 int main(int argc, char **argv)
34 {
35         char *file = argv[1];
36         char *new_file = argv[2];
37         int fd;
38         int fd_new;
39         void *base;
40
41         struct iovec iovec[3]=
42         {
43                 { "aaaaaaaaaa" , 10 },
44                 { "bbbbbbbbbb" , 10 },
45                 { NULL , 10 }
46         };
47
48         int i;
49
50         fd=open(file, O_RDONLY);
51         if (fd==-1) {perror("open");exit(1);}
52
53         base = mmap(NULL,16384,PROT_READ,MAP_SHARED,fd,0);
54         if  (base == (void *)-1) { perror("mmap");exit(1); }
55
56         unlink(new_file);
57
58         fd_new=open(new_file,O_RDWR|O_CREAT,0666);
59         if (fd_new==-1) {perror("creat");exit(1);}
60
61         iovec[2].iov_base=(char *)base;
62         i=writev(fd_new,iovec,sizeof(iovec)/sizeof(*iovec));
63         if (i==-1) {perror("writev");exit(1);}
64
65         close(fd_new);
66         munmap(base,16384);
67         close(fd);
68
69         return 0;
70 }