1 /*
2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
3 * Copyright (c) 2017 Fujitsu Ltd.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 *
13 * Further, this software is distributed without any warranty that it is
14 * free of the rightful claim of any third person regarding infringement
15 * or the like. Any license provided herein, whether implied or
16 * otherwise, applies only to this software file. Patent licenses, if
17 * any, provided herein do not apply to combinations of this program with
18 * other software, or any other product whatsoever.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 */
24
25 #include <stdio.h>
26 #include <string.h>
27 #include <errno.h>
28 #include "tst_test.h"
29
30 static int fd;
31
verify_write(void)32 static void verify_write(void)
33 {
34 int i, badcount = 0;
35 char buf[BUFSIZ];
36
37 memset(buf, 'w', BUFSIZ);
38
39 SAFE_LSEEK(fd, 0, SEEK_SET);
40
41 for (i = BUFSIZ; i > 0; i--) {
42 TEST(write(fd, buf, i));
43 if (TEST_RETURN == -1) {
44 tst_res(TFAIL | TTERRNO, "write failed");
45 return;
46 }
47
48 if (TEST_RETURN != i) {
49 badcount++;
50 tst_res(TINFO, "write() returned %ld, expected %d",
51 TEST_RETURN, i);
52 }
53 }
54
55 if (badcount != 0)
56 tst_res(TFAIL, "write() failed to return proper count");
57 else
58 tst_res(TPASS, "write() passed");
59 }
60
setup(void)61 static void setup(void)
62 {
63 fd = SAFE_OPEN("test_file", O_RDWR | O_CREAT, 0700);
64 }
65
cleanup(void)66 static void cleanup(void)
67 {
68 if (fd > 0)
69 SAFE_CLOSE(fd);
70 }
71
72 static struct tst_test test = {
73 .setup = setup,
74 .cleanup = cleanup,
75 .test_all = verify_write,
76 .needs_tmpdir = 1,
77 };
78