death to trailing whitespace
[openafs.git] / src / tests / write-large.c
1 /*
2  * Copyright (c) 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 #ifndef _LARGEFILE64_SOURCE
35 # define _LARGEFILE64_SOURCE
36 #endif
37
38 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif
41
42 #include <sys/types.h>
43
44 #include <stdio.h>
45
46 #include <sys/stat.h>
47 #include <fcntl.h>
48 #include <unistd.h>
49
50
51 #include <err.h>
52
53 const char *fn = "foobar";
54
55 static void
56 check_size(const char *fn, size_t sz, int paranoia)
57 {
58     struct stat sb;
59
60     if (paranoia)
61         return;
62
63     if (stat(fn, &sb) < 0)
64         err(1, "stat");
65     if (sb.st_size != sz)
66         errx(1, "st_size mismatch %d != %d", (int)sb.st_size, (int)sz);
67 }
68
69 int
70 main(int argc, char **argv)
71 {
72     int fd, cnt, ret;
73     int buf[1024];
74
75 #ifdef O_LARGEFILE
76     fd = open(fn, O_RDWR | O_CREAT | O_TRUNC | O_LARGEFILE, 0644);
77 #else
78     fd = open(fn, O_RDWR | O_CREAT | O_TRUNC, 0644);
79 #endif
80     if (fd < 0)
81         err(1, "open1");
82     cnt = 0;
83     while (cnt < 2097151) {
84         ret = write(fd, buf, 1024);
85         if (ret != 1024)
86             errx(1, "write1 %d %d", cnt, ret);
87         cnt++;
88     }
89     ret = write(fd, buf, 1024);
90     if (ret != 1023)
91         errx(1, "write1 last %d", ret);
92     if (close(fd) < 0)
93         err(1, "close1");
94
95     check_size(fn, 2147483647, 0);
96
97     return 0;
98 }