• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 void setup(void)
40 {
41 	sprintf(filename, "creat01.%d", getpid());
42 }
43 
44 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 = creat(filename, tcases[i].mode);
57 
58 	if (fd == -1)
59 		tst_brk(TBROK | TERRNO, "creat() failed");
60 
61 	SAFE_STAT(filename, &buf);
62 
63 	if (buf.st_size != 0)
64 		tst_res(TFAIL, "creat() failed to truncate file to 0 bytes");
65 	else
66 		tst_res(TPASS, "creat() truncated file to 0 bytes");
67 
68 	if (write(fd, "A", 1) != 1)
69 		tst_res(TFAIL | TERRNO, "write was unsuccessful");
70 	else
71 		tst_res(TPASS, "file was created and written to successfully");
72 
73 	if (read(fd, &c, 1) != -1)
74 		tst_res(TFAIL, "read succeeded unexpectedly");
75 	else
76 		tst_res(TPASS | TERRNO, "read failed expectedly");
77 
78 	SAFE_CLOSE(fd);
79 }
80 
cleanup(void)81 static void cleanup(void)
82 {
83 	if (fd > 0)
84 		SAFE_CLOSE(fd);
85 }
86 
87 static struct tst_test test = {
88 	.tid = "creat01",
89 	.tcnt = 2,
90 	.test = verify_creat,
91 	.needs_tmpdir = 1,
92 	.cleanup = cleanup,
93 	.setup = setup,
94 };
95