1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines Corp., 2001
4f08c3bdfSopenharmony_ci * Ported to LTP: Wayne Boyer
5f08c3bdfSopenharmony_ci */
6f08c3bdfSopenharmony_ci
7f08c3bdfSopenharmony_ci/*
8f08c3bdfSopenharmony_ci * 1. creat() a file using 0444 mode, write to the fildes, write
9f08c3bdfSopenharmony_ci *    should return a positive count.
10f08c3bdfSopenharmony_ci *
11f08c3bdfSopenharmony_ci * 2. creat() should truncate a file to 0 bytes if it already
12f08c3bdfSopenharmony_ci *    exists, and should not fail.
13f08c3bdfSopenharmony_ci *
14f08c3bdfSopenharmony_ci */
15f08c3bdfSopenharmony_ci
16f08c3bdfSopenharmony_ci#include <sys/types.h>
17f08c3bdfSopenharmony_ci#include <sys/stat.h>
18f08c3bdfSopenharmony_ci#include <fcntl.h>
19f08c3bdfSopenharmony_ci#include <stdio.h>
20f08c3bdfSopenharmony_ci
21f08c3bdfSopenharmony_ci#include "tst_test.h"
22f08c3bdfSopenharmony_ci
23f08c3bdfSopenharmony_cistatic char filename[40];
24f08c3bdfSopenharmony_cistatic int fd;
25f08c3bdfSopenharmony_ci
26f08c3bdfSopenharmony_cistatic void setup(void)
27f08c3bdfSopenharmony_ci{
28f08c3bdfSopenharmony_ci	sprintf(filename, "creat01.%d", getpid());
29f08c3bdfSopenharmony_ci}
30f08c3bdfSopenharmony_ci
31f08c3bdfSopenharmony_cistatic struct tcase {
32f08c3bdfSopenharmony_ci	int mode;
33f08c3bdfSopenharmony_ci} tcases[] = {
34f08c3bdfSopenharmony_ci	{0644},
35f08c3bdfSopenharmony_ci	{0444}
36f08c3bdfSopenharmony_ci};
37f08c3bdfSopenharmony_ci
38f08c3bdfSopenharmony_cistatic void verify_creat(unsigned int i)
39f08c3bdfSopenharmony_ci{
40f08c3bdfSopenharmony_ci	struct stat buf;
41f08c3bdfSopenharmony_ci	char c;
42f08c3bdfSopenharmony_ci
43f08c3bdfSopenharmony_ci	fd = SAFE_CREAT(filename, tcases[i].mode);
44f08c3bdfSopenharmony_ci
45f08c3bdfSopenharmony_ci	SAFE_STAT(filename, &buf);
46f08c3bdfSopenharmony_ci
47f08c3bdfSopenharmony_ci	if (buf.st_size != 0)
48f08c3bdfSopenharmony_ci		tst_res(TFAIL, "creat() failed to truncate file to 0 bytes");
49f08c3bdfSopenharmony_ci	else
50f08c3bdfSopenharmony_ci		tst_res(TPASS, "creat() truncated file to 0 bytes");
51f08c3bdfSopenharmony_ci
52f08c3bdfSopenharmony_ci	if (write(fd, "A", 1) != 1)
53f08c3bdfSopenharmony_ci		tst_res(TFAIL | TERRNO, "write was unsuccessful");
54f08c3bdfSopenharmony_ci	else
55f08c3bdfSopenharmony_ci		tst_res(TPASS, "file was created and written to successfully");
56f08c3bdfSopenharmony_ci
57f08c3bdfSopenharmony_ci	if (read(fd, &c, 1) != -1)
58f08c3bdfSopenharmony_ci		tst_res(TFAIL, "read succeeded unexpectedly");
59f08c3bdfSopenharmony_ci	else
60f08c3bdfSopenharmony_ci		tst_res(TPASS | TERRNO, "read failed expectedly");
61f08c3bdfSopenharmony_ci
62f08c3bdfSopenharmony_ci	SAFE_CLOSE(fd);
63f08c3bdfSopenharmony_ci}
64f08c3bdfSopenharmony_ci
65f08c3bdfSopenharmony_cistatic void cleanup(void)
66f08c3bdfSopenharmony_ci{
67f08c3bdfSopenharmony_ci	if (fd > 0)
68f08c3bdfSopenharmony_ci		SAFE_CLOSE(fd);
69f08c3bdfSopenharmony_ci}
70f08c3bdfSopenharmony_ci
71f08c3bdfSopenharmony_cistatic struct tst_test test = {
72f08c3bdfSopenharmony_ci	.tcnt = 2,
73f08c3bdfSopenharmony_ci	.test = verify_creat,
74f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
75f08c3bdfSopenharmony_ci	.cleanup = cleanup,
76f08c3bdfSopenharmony_ci	.setup = setup,
77f08c3bdfSopenharmony_ci};
78