1 /*
2 * Copyright (c) International Business Machines Corp., 2001
3 * 07/2001 Ported by Wayne Boyer
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 /*
21 * 1. creat() a file using 0444 mode, write to the fildes, write
22 * should return a positive count.
23 *
24 * 2. creat() should truncate a file to 0 bytes if it already
25 * exists, and should not fail.
26 *
27 */
28
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32 #include <stdio.h>
33
34 #include "tst_test.h"
35
36 static char filename[40];
37 static int fd;
38
setup(void)39 static void setup(void)
40 {
41 sprintf(filename, "creat01.%d", getpid());
42 }
43
44 static struct tcase {
45 int mode;
46 } tcases[] = {
47 {0644},
48 {0444}
49 };
50
verify_creat(unsigned int i)51 static void verify_creat(unsigned int i)
52 {
53 struct stat buf;
54 char c;
55
56 fd = SAFE_CREAT(filename, tcases[i].mode);
57
58 SAFE_STAT(filename, &buf);
59
60 if (buf.st_size != 0)
61 tst_res(TFAIL, "creat() failed to truncate file to 0 bytes");
62 else
63 tst_res(TPASS, "creat() truncated file to 0 bytes");
64
65 if (write(fd, "A", 1) != 1)
66 tst_res(TFAIL | TERRNO, "write was unsuccessful");
67 else
68 tst_res(TPASS, "file was created and written to successfully");
69
70 if (read(fd, &c, 1) != -1)
71 tst_res(TFAIL, "read succeeded unexpectedly");
72 else
73 tst_res(TPASS | TERRNO, "read failed expectedly");
74
75 SAFE_CLOSE(fd);
76 }
77
cleanup(void)78 static void cleanup(void)
79 {
80 if (fd > 0)
81 SAFE_CLOSE(fd);
82 }
83
84 static struct tst_test test = {
85 .tcnt = 2,
86 .test = verify_creat,
87 .needs_tmpdir = 1,
88 .cleanup = cleanup,
89 .setup = setup,
90 };
91