1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
4f08c3bdfSopenharmony_ci * Copyright (c) 2017 Fujitsu Ltd.
5f08c3bdfSopenharmony_ci */
6f08c3bdfSopenharmony_ci
7f08c3bdfSopenharmony_ci#include <stdio.h>
8f08c3bdfSopenharmony_ci#include <string.h>
9f08c3bdfSopenharmony_ci#include <errno.h>
10f08c3bdfSopenharmony_ci#include "tst_test.h"
11f08c3bdfSopenharmony_ci
12f08c3bdfSopenharmony_cistatic int fd;
13f08c3bdfSopenharmony_ci
14f08c3bdfSopenharmony_cistatic void verify_write(void)
15f08c3bdfSopenharmony_ci{
16f08c3bdfSopenharmony_ci	int i, badcount = 0;
17f08c3bdfSopenharmony_ci	char buf[BUFSIZ];
18f08c3bdfSopenharmony_ci
19f08c3bdfSopenharmony_ci	memset(buf, 'w', BUFSIZ);
20f08c3bdfSopenharmony_ci
21f08c3bdfSopenharmony_ci	SAFE_LSEEK(fd, 0, SEEK_SET);
22f08c3bdfSopenharmony_ci
23f08c3bdfSopenharmony_ci	for (i = BUFSIZ; i > 0; i--) {
24f08c3bdfSopenharmony_ci		TEST(write(fd, buf, i));
25f08c3bdfSopenharmony_ci		if (TST_RET == -1) {
26f08c3bdfSopenharmony_ci			tst_res(TFAIL | TTERRNO, "write failed");
27f08c3bdfSopenharmony_ci			return;
28f08c3bdfSopenharmony_ci		}
29f08c3bdfSopenharmony_ci
30f08c3bdfSopenharmony_ci		if (TST_RET != i) {
31f08c3bdfSopenharmony_ci			badcount++;
32f08c3bdfSopenharmony_ci			tst_res(TINFO, "write() returned %ld, expected %d",
33f08c3bdfSopenharmony_ci				TST_RET, i);
34f08c3bdfSopenharmony_ci		}
35f08c3bdfSopenharmony_ci	}
36f08c3bdfSopenharmony_ci
37f08c3bdfSopenharmony_ci	if (badcount != 0)
38f08c3bdfSopenharmony_ci		tst_res(TFAIL, "write() failed to return proper count");
39f08c3bdfSopenharmony_ci	else
40f08c3bdfSopenharmony_ci		tst_res(TPASS, "write() passed");
41f08c3bdfSopenharmony_ci}
42f08c3bdfSopenharmony_ci
43f08c3bdfSopenharmony_cistatic void setup(void)
44f08c3bdfSopenharmony_ci{
45f08c3bdfSopenharmony_ci	fd = SAFE_OPEN("test_file", O_RDWR | O_CREAT, 0700);
46f08c3bdfSopenharmony_ci}
47f08c3bdfSopenharmony_ci
48f08c3bdfSopenharmony_cistatic void cleanup(void)
49f08c3bdfSopenharmony_ci{
50f08c3bdfSopenharmony_ci	if (fd > 0)
51f08c3bdfSopenharmony_ci		SAFE_CLOSE(fd);
52f08c3bdfSopenharmony_ci}
53f08c3bdfSopenharmony_ci
54f08c3bdfSopenharmony_cistatic struct tst_test test = {
55f08c3bdfSopenharmony_ci	.setup = setup,
56f08c3bdfSopenharmony_ci	.cleanup = cleanup,
57f08c3bdfSopenharmony_ci	.test_all = verify_write,
58f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
59f08c3bdfSopenharmony_ci};
60