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