1*433d6423SLionel Sambuc #include <sys/types.h>
2*433d6423SLionel Sambuc #include <sys/stat.h>
3*433d6423SLionel Sambuc #include <errno.h>
4*433d6423SLionel Sambuc #include <fcntl.h>
5*433d6423SLionel Sambuc #include <limits.h>
6*433d6423SLionel Sambuc #include <signal.h>
7*433d6423SLionel Sambuc #include <stdlib.h>
8*433d6423SLionel Sambuc #include <unistd.h>
9*433d6423SLionel Sambuc #include <stdio.h>
10*433d6423SLionel Sambuc
11*433d6423SLionel Sambuc int max_error = 3;
12*433d6423SLionel Sambuc #include "common.h"
13*433d6423SLionel Sambuc
14*433d6423SLionel Sambuc #define NB 30L
15*433d6423SLionel Sambuc #define NBOUNDS 6
16*433d6423SLionel Sambuc
17*433d6423SLionel Sambuc int subtest, passes, pipesigs;
18*433d6423SLionel Sambuc long t1;
19*433d6423SLionel Sambuc
20*433d6423SLionel Sambuc char aa[100];
21*433d6423SLionel Sambuc char b[4] = {0, 1, 2, 3}, c[4] = {10, 20, 30, 40}, d[4] = {6, 7, 8, 9};
22*433d6423SLionel Sambuc long bounds[NBOUNDS] = {7, 9, 50, 519, 520, 40000L};
23*433d6423SLionel Sambuc char buff[30000];
24*433d6423SLionel Sambuc
25*433d6423SLionel Sambuc
26*433d6423SLionel Sambuc int main(int argc, char *argv[]);
27*433d6423SLionel Sambuc void test19a(void);
28*433d6423SLionel Sambuc void test19b(void);
29*433d6423SLionel Sambuc void test19c(void);
30*433d6423SLionel Sambuc void test19d(void);
31*433d6423SLionel Sambuc void test19e(void);
32*433d6423SLionel Sambuc void test19f(void);
33*433d6423SLionel Sambuc void test19g(void);
34*433d6423SLionel Sambuc void clraa(void);
35*433d6423SLionel Sambuc void pipecatcher(int s);
36*433d6423SLionel Sambuc
main(argc,argv)37*433d6423SLionel Sambuc int main(argc, argv)
38*433d6423SLionel Sambuc int argc;
39*433d6423SLionel Sambuc char *argv[];
40*433d6423SLionel Sambuc {
41*433d6423SLionel Sambuc int i, m;
42*433d6423SLionel Sambuc
43*433d6423SLionel Sambuc start(19);
44*433d6423SLionel Sambuc
45*433d6423SLionel Sambuc m = (argc == 2 ? atoi(argv[1]) : 0xFFFF);
46*433d6423SLionel Sambuc
47*433d6423SLionel Sambuc for (i = 0; i < 4; i++) {
48*433d6423SLionel Sambuc if (m & 0001) test19a();
49*433d6423SLionel Sambuc if (m & 0002) test19b();
50*433d6423SLionel Sambuc if (m & 0004) test19c();
51*433d6423SLionel Sambuc if (m & 0010) test19d();
52*433d6423SLionel Sambuc if (m & 0020) test19e();
53*433d6423SLionel Sambuc if (m & 0040) test19f();
54*433d6423SLionel Sambuc if (m & 0100) test19g();
55*433d6423SLionel Sambuc passes++;
56*433d6423SLionel Sambuc }
57*433d6423SLionel Sambuc quit();
58*433d6423SLionel Sambuc return(-1); /* impossible */
59*433d6423SLionel Sambuc }
60*433d6423SLionel Sambuc
test19a()61*433d6423SLionel Sambuc void test19a()
62*433d6423SLionel Sambuc {
63*433d6423SLionel Sambuc /* Test open with O_CREAT and O_EXCL. */
64*433d6423SLionel Sambuc
65*433d6423SLionel Sambuc int fd;
66*433d6423SLionel Sambuc
67*433d6423SLionel Sambuc subtest = 1;
68*433d6423SLionel Sambuc
69*433d6423SLionel Sambuc if ( (fd = creat("T19.a1", 0777)) != 3) e(1); /* create test file */
70*433d6423SLionel Sambuc if (close(fd) != 0) e(2);
71*433d6423SLionel Sambuc if ( (fd = open("T19.a1", O_RDONLY)) != 3) e(3);
72*433d6423SLionel Sambuc if (close(fd) != 0) e(4);
73*433d6423SLionel Sambuc if ( (fd = open("T19.a1", O_WRONLY)) != 3) e(5);
74*433d6423SLionel Sambuc if (close(fd) != 0) e(6);
75*433d6423SLionel Sambuc if ( (fd = open("T19.a1", O_RDWR)) != 3) e(7);
76*433d6423SLionel Sambuc if (close(fd) != 0) e(8);
77*433d6423SLionel Sambuc
78*433d6423SLionel Sambuc /* See if O_CREAT actually creates a file. */
79*433d6423SLionel Sambuc if ( (fd = open("T19.a2", O_RDONLY)) != -1) e(9); /* must fail */
80*433d6423SLionel Sambuc if ( (fd = open("T19.a2", O_RDONLY | O_CREAT, 0444)) != 3) e(10);
81*433d6423SLionel Sambuc if (close(fd) != 0) e(11);
82*433d6423SLionel Sambuc if ( (fd = open("T19.a2", O_RDONLY)) != 3) e(12);
83*433d6423SLionel Sambuc if (close(fd) != 0) e(13);
84*433d6423SLionel Sambuc if ( (fd = open("T19.a2", O_WRONLY)) != -1) e(14);
85*433d6423SLionel Sambuc if ( (fd = open("T19.a2", O_RDWR)) != -1) e(15);
86*433d6423SLionel Sambuc
87*433d6423SLionel Sambuc /* See what O_CREAT does on an existing file. */
88*433d6423SLionel Sambuc if ( (fd = open("T19.a2", O_RDONLY | O_CREAT, 0777)) != 3) e(16);
89*433d6423SLionel Sambuc if (close(fd) != 0) e(17);
90*433d6423SLionel Sambuc if ( (fd = open("T19.a2", O_RDONLY)) != 3) e(18);
91*433d6423SLionel Sambuc if (close(fd) != 0) e(19);
92*433d6423SLionel Sambuc if ( (fd = open("T19.a2", O_WRONLY)) != -1) e(20);
93*433d6423SLionel Sambuc if ( (fd = open("T19.a2", O_RDWR)) != -1) e(21);
94*433d6423SLionel Sambuc
95*433d6423SLionel Sambuc /* See if O_EXCL works. */
96*433d6423SLionel Sambuc if ( (fd = open("T19.a2", O_RDONLY | O_EXCL)) != 3) e(22);
97*433d6423SLionel Sambuc if (close(fd) != 0) e(23);
98*433d6423SLionel Sambuc if ( (fd = open("T19.a2", O_WRONLY | O_EXCL)) != -1) e(24);
99*433d6423SLionel Sambuc if ( (fd = open("T19.a3", O_RDONLY | O_EXCL)) != -1) e(25);
100*433d6423SLionel Sambuc if ( (fd = open("T19.a3", O_RDONLY | O_CREAT | O_EXCL, 0444)) != 3) e(26);
101*433d6423SLionel Sambuc if (close(fd) != 0) e(27);
102*433d6423SLionel Sambuc errno = 0;
103*433d6423SLionel Sambuc if ( (fd = open("T19.a3", O_RDONLY | O_CREAT | O_EXCL, 0444)) != -1) e(28);
104*433d6423SLionel Sambuc if (errno != EEXIST) e(29);
105*433d6423SLionel Sambuc
106*433d6423SLionel Sambuc if (unlink("T19.a1") != 0) e(30);
107*433d6423SLionel Sambuc if (unlink("T19.a2") != 0) e(31);
108*433d6423SLionel Sambuc if (unlink("T19.a3") != 0) e(32);
109*433d6423SLionel Sambuc }
110*433d6423SLionel Sambuc
test19b()111*433d6423SLionel Sambuc void test19b()
112*433d6423SLionel Sambuc {
113*433d6423SLionel Sambuc /* Test open with O_APPEND and O_TRUNC. */
114*433d6423SLionel Sambuc
115*433d6423SLionel Sambuc int fd;
116*433d6423SLionel Sambuc
117*433d6423SLionel Sambuc subtest = 2;
118*433d6423SLionel Sambuc
119*433d6423SLionel Sambuc if ( (fd = creat("T19.b1", 0777)) != 3) e(1); /* create test file */
120*433d6423SLionel Sambuc if (write(fd, b, 4) != 4) e(2);
121*433d6423SLionel Sambuc if (close(fd) != 0) e(3);
122*433d6423SLionel Sambuc clraa();
123*433d6423SLionel Sambuc if ( (fd = open("T19.b1", O_RDWR | O_APPEND)) != 3) e(4);
124*433d6423SLionel Sambuc if (read(fd, aa, 100) != 4) e(5);
125*433d6423SLionel Sambuc if (aa[0] != 0 || aa[1] != 1 || aa[2] != 2 || aa[3] != 3) e(6);
126*433d6423SLionel Sambuc if (close(fd) != 0) e(7);
127*433d6423SLionel Sambuc if ( (fd = open("T19.b1", O_RDWR | O_APPEND)) != 3) e(8);
128*433d6423SLionel Sambuc if (write(fd, b, 4) != 4) e(9);
129*433d6423SLionel Sambuc if (lseek(fd, 0L, SEEK_SET) != 0L) e(10);
130*433d6423SLionel Sambuc clraa();
131*433d6423SLionel Sambuc if (read(fd, aa, 100) != 8) e(11);
132*433d6423SLionel Sambuc if (aa[4] != 0 || aa[5] != 1 || aa[6] != 2 || aa[7] != 3) e(12);
133*433d6423SLionel Sambuc if (close(fd) != 0) e(13);
134*433d6423SLionel Sambuc
135*433d6423SLionel Sambuc if ( (fd = open("T19.b1", O_RDWR | O_TRUNC)) != 3) e(14);
136*433d6423SLionel Sambuc if (read(fd, aa, 100) != 0) e(15);
137*433d6423SLionel Sambuc if (close(fd) != 0) e(16);
138*433d6423SLionel Sambuc
139*433d6423SLionel Sambuc unlink("T19.b1");
140*433d6423SLionel Sambuc }
141*433d6423SLionel Sambuc
test19c()142*433d6423SLionel Sambuc void test19c()
143*433d6423SLionel Sambuc {
144*433d6423SLionel Sambuc /* Test program for open(), close(), creat(), read(), write(), lseek(). */
145*433d6423SLionel Sambuc
146*433d6423SLionel Sambuc int i, n, n1, n2;
147*433d6423SLionel Sambuc
148*433d6423SLionel Sambuc subtest = 3;
149*433d6423SLionel Sambuc if ((n = creat("foop", 0777)) != 3) e(1);
150*433d6423SLionel Sambuc if ((n1 = creat("foop", 0777)) != 4) e(2);
151*433d6423SLionel Sambuc if ((n2 = creat("/", 0777)) != -1) e(3);
152*433d6423SLionel Sambuc if (close(n) != 0) e(4);
153*433d6423SLionel Sambuc if ((n = open("foop", O_RDONLY)) != 3) e(5);
154*433d6423SLionel Sambuc if ((n2 = open("nofile", O_RDONLY)) != -1) e(6);
155*433d6423SLionel Sambuc if (close(n1) != 0) e(7);
156*433d6423SLionel Sambuc
157*433d6423SLionel Sambuc /* N is the only one open now. */
158*433d6423SLionel Sambuc for (i = 0; i < 2; i++) {
159*433d6423SLionel Sambuc n1 = creat("File2", 0777);
160*433d6423SLionel Sambuc if (n1 != 4) {
161*433d6423SLionel Sambuc printf("creat yielded fd=%d, expected 4\n", n1);
162*433d6423SLionel Sambuc e(8);
163*433d6423SLionel Sambuc }
164*433d6423SLionel Sambuc if ((n2 = open("File2", O_RDONLY)) != 5) e(9);
165*433d6423SLionel Sambuc if (close(n1) != 0) e(10);
166*433d6423SLionel Sambuc if (close(n2) != 0) e(11);
167*433d6423SLionel Sambuc }
168*433d6423SLionel Sambuc unlink("File2");
169*433d6423SLionel Sambuc if (close(n) != 0) e(12);
170*433d6423SLionel Sambuc
171*433d6423SLionel Sambuc /* All files closed now. */
172*433d6423SLionel Sambuc for (i = 0; i < 2; i++) {
173*433d6423SLionel Sambuc if ((n = creat("foop", 0777)) != 3) e(13);
174*433d6423SLionel Sambuc if (close(n) != 0) e(14);
175*433d6423SLionel Sambuc if ((n = open("foop", O_RDWR)) != 3) e(15);
176*433d6423SLionel Sambuc
177*433d6423SLionel Sambuc /* Read/write tests */
178*433d6423SLionel Sambuc if (write(n, b, 4) != 4) e(16);
179*433d6423SLionel Sambuc if (read(n, aa, 4) != 0) e(17);
180*433d6423SLionel Sambuc if (lseek(n, 0L, SEEK_SET) != 0L) e(18);
181*433d6423SLionel Sambuc if (read(n, aa, 4) != 4) e(19);
182*433d6423SLionel Sambuc if (aa[0] != 0 || aa[1] != 1 || aa[2] != 2 || aa[3] != 3) e(20);
183*433d6423SLionel Sambuc if (lseek(n, 0L, SEEK_SET) != 0L) e(21);
184*433d6423SLionel Sambuc if (lseek(n, 2L, SEEK_CUR) != 2L) e(22);
185*433d6423SLionel Sambuc if (read(n, aa, 4) != 2) e(23);
186*433d6423SLionel Sambuc if (aa[0] != 2 || aa[1] != 3 || aa[2] != 2 || aa[3] != 3) e(24);
187*433d6423SLionel Sambuc if (lseek(n, 2L, SEEK_SET) != 2L) e(25);
188*433d6423SLionel Sambuc clraa();
189*433d6423SLionel Sambuc if (write(n, c, 4) != 4) e(26);
190*433d6423SLionel Sambuc if (lseek(n, 0L, SEEK_SET) != 0L) e(27);
191*433d6423SLionel Sambuc if (read(n, aa, 10) != 6) e(28);
192*433d6423SLionel Sambuc if (aa[0] != 0 || aa[1] != 1 || aa[2] != 10 || aa[3] != 20) e(29);
193*433d6423SLionel Sambuc if (lseek(n, 16L, SEEK_SET) != 16L) e(30);
194*433d6423SLionel Sambuc if (lseek(n, 2040L, SEEK_END) != 2046L) e(31);
195*433d6423SLionel Sambuc if (read(n, aa, 10) != 0) e(32);
196*433d6423SLionel Sambuc if (lseek(n, 0L, SEEK_CUR) != 2046L) e(33);
197*433d6423SLionel Sambuc clraa();
198*433d6423SLionel Sambuc if (write(n, c, 4) != 4) e(34);
199*433d6423SLionel Sambuc if (lseek(n, 0L, SEEK_CUR) != 2050L) e(35);
200*433d6423SLionel Sambuc if (lseek(n, 2040L, SEEK_SET) != 2040L) e(36);
201*433d6423SLionel Sambuc clraa();
202*433d6423SLionel Sambuc if (read(n, aa, 20) != 10) e(37);
203*433d6423SLionel Sambuc if (aa[0] != 0 || aa[5] != 0 || aa[6] != 10 || aa[9] != 40) e(38);
204*433d6423SLionel Sambuc if (lseek(n, 10239L, SEEK_SET) != 10239L) e(39);
205*433d6423SLionel Sambuc if (write(n, d, 2) != 2) e(40);
206*433d6423SLionel Sambuc if (lseek(n, -2L, SEEK_END) != 10239L) e(41);
207*433d6423SLionel Sambuc if (read(n, aa, 2) != 2) e(42);
208*433d6423SLionel Sambuc if (aa[0] != 6 || aa[1] != 7) e(43);
209*433d6423SLionel Sambuc if (lseek(n, NB * 1024L - 2L, SEEK_SET) != NB * 1024L - 2L) e(44);
210*433d6423SLionel Sambuc if (write(n, b, 4) != 4) e(45);
211*433d6423SLionel Sambuc if (lseek(n, 0L, SEEK_SET) != 0L) e(46);
212*433d6423SLionel Sambuc if (lseek(n, -6L, SEEK_END) != 1024L * NB - 4) e(47);
213*433d6423SLionel Sambuc clraa();
214*433d6423SLionel Sambuc if (read(n, aa, 100) != 6) e(48);
215*433d6423SLionel Sambuc if (aa[0] != 0 || aa[1] != 0 || aa[3] != 1 || aa[4] != 2|| aa[5] != 3)
216*433d6423SLionel Sambuc e(49);
217*433d6423SLionel Sambuc if (lseek(n, 20000L, SEEK_SET) != 20000L) e(50);
218*433d6423SLionel Sambuc if (write(n, c, 4) != 4) e(51);
219*433d6423SLionel Sambuc if (lseek(n, -4L, SEEK_CUR) != 20000L) e(52);
220*433d6423SLionel Sambuc if (read(n, aa, 4) != 4) e(53);
221*433d6423SLionel Sambuc if (aa[0] != 10 || aa[1] != 20 || aa[2] != 30 || aa[3] != 40) e(54);
222*433d6423SLionel Sambuc if (close(n) != 0) e(55);
223*433d6423SLionel Sambuc if ((n1 = creat("foop", 0777)) != 3) e(56);
224*433d6423SLionel Sambuc if (close(n1) != 0) e(57);
225*433d6423SLionel Sambuc unlink("foop");
226*433d6423SLionel Sambuc
227*433d6423SLionel Sambuc }
228*433d6423SLionel Sambuc }
229*433d6423SLionel Sambuc
test19d()230*433d6423SLionel Sambuc void test19d()
231*433d6423SLionel Sambuc {
232*433d6423SLionel Sambuc /* Test read. */
233*433d6423SLionel Sambuc
234*433d6423SLionel Sambuc int i, fd, pd[2];
235*433d6423SLionel Sambuc char bb[100];
236*433d6423SLionel Sambuc
237*433d6423SLionel Sambuc subtest = 4;
238*433d6423SLionel Sambuc
239*433d6423SLionel Sambuc for (i = 0; i < 100; i++) bb[i] = i;
240*433d6423SLionel Sambuc if ( (fd = creat("T19.d1", 0777)) != 3) e(1); /* create test file */
241*433d6423SLionel Sambuc if (write(fd, bb, 100) != 100) e(2);
242*433d6423SLionel Sambuc if (close(fd) != 0) e(3);
243*433d6423SLionel Sambuc clraa();
244*433d6423SLionel Sambuc if ( (fd = open("T19.d1", O_RDONLY)) != 3) e(4);
245*433d6423SLionel Sambuc errno = 1000;
246*433d6423SLionel Sambuc if (read(fd, aa, 0) != 0) e(5);
247*433d6423SLionel Sambuc if (errno != 1000) e(6);
248*433d6423SLionel Sambuc if (read(fd, aa, 100) != 100) e(7);
249*433d6423SLionel Sambuc if (lseek(fd, 37L, SEEK_SET) != 37L) e(8);
250*433d6423SLionel Sambuc if (read(fd, aa, 10) != 10) e(9);
251*433d6423SLionel Sambuc if (lseek(fd, 0L, SEEK_CUR) != 47L) e(10);
252*433d6423SLionel Sambuc if (read(fd, aa, 100) != 53) e(11);
253*433d6423SLionel Sambuc if (aa[0] != 47) e(12);
254*433d6423SLionel Sambuc if (read(fd, aa, 1) != 0) e(13);
255*433d6423SLionel Sambuc if (close(fd) != 0) e(14);
256*433d6423SLionel Sambuc
257*433d6423SLionel Sambuc /* Read from pipe with no writer open. */
258*433d6423SLionel Sambuc if (pipe(pd) != 0) e(15);
259*433d6423SLionel Sambuc if (close(pd[1]) != 0) e(16);
260*433d6423SLionel Sambuc errno = 2000;
261*433d6423SLionel Sambuc if (read(pd[0], aa, 1) != 0) e(17); /* must return EOF */
262*433d6423SLionel Sambuc if (errno != 2000) e(18);
263*433d6423SLionel Sambuc
264*433d6423SLionel Sambuc /* Read from a pipe with O_NONBLOCK set. */
265*433d6423SLionel Sambuc if (fcntl(pd[0], F_SETFL, O_NONBLOCK) != 0) e(19); /* set O_NONBLOCK */
266*433d6423SLionel Sambuc /*
267*433d6423SLionel Sambuc if (read(pd[0], aa, 1) != -1) e(20);
268*433d6423SLionel Sambuc if (errno != EAGAIN) e(21);
269*433d6423SLionel Sambuc */
270*433d6423SLionel Sambuc if (close(pd[0]) != 0) e(19);
271*433d6423SLionel Sambuc if (unlink("T19.d1") != 0) e(20);
272*433d6423SLionel Sambuc }
273*433d6423SLionel Sambuc
test19e()274*433d6423SLionel Sambuc void test19e()
275*433d6423SLionel Sambuc {
276*433d6423SLionel Sambuc /* Test link, unlink, stat, fstat, dup, umask. */
277*433d6423SLionel Sambuc
278*433d6423SLionel Sambuc int i, j, n, n1, flag;
279*433d6423SLionel Sambuc char a[255], b[255];
280*433d6423SLionel Sambuc struct stat s, s1;
281*433d6423SLionel Sambuc
282*433d6423SLionel Sambuc subtest = 5;
283*433d6423SLionel Sambuc for (i = 0; i < 2; i++) {
284*433d6423SLionel Sambuc umask(0);
285*433d6423SLionel Sambuc
286*433d6423SLionel Sambuc if ((n = creat("T3", 0702)) < 0) e(1);
287*433d6423SLionel Sambuc if (link("T3", "newT3") < 0) e(2);
288*433d6423SLionel Sambuc if ((n1 = open("newT3", O_RDWR)) < 0) e(3);
289*433d6423SLionel Sambuc for (j = 0; j < 255; j++) a[j] = j;
290*433d6423SLionel Sambuc if (write(n, a, 255) != 255) e(4);
291*433d6423SLionel Sambuc if (read(n1, b, 255) != 255) e(5);
292*433d6423SLionel Sambuc flag = 0;
293*433d6423SLionel Sambuc for (j = 0; j < 255; j++)
294*433d6423SLionel Sambuc if (a[j] != b[j]) flag++;
295*433d6423SLionel Sambuc if (flag) e(6);
296*433d6423SLionel Sambuc if (unlink("T3") < 0) e(7);
297*433d6423SLionel Sambuc if (close(n) < 0) e(8);
298*433d6423SLionel Sambuc if (close(n1) < 0) e(9);
299*433d6423SLionel Sambuc if ((n1 = open("newT3", O_RDONLY)) < 0) e(10);
300*433d6423SLionel Sambuc if (read(n1, b, 255) != 255) e(11);
301*433d6423SLionel Sambuc flag = 0;
302*433d6423SLionel Sambuc for (j = 0; j < 255; j++)
303*433d6423SLionel Sambuc if (a[j] != b[j]) flag++;
304*433d6423SLionel Sambuc if (flag) e(12);
305*433d6423SLionel Sambuc
306*433d6423SLionel Sambuc /* Now check out stat, fstat. */
307*433d6423SLionel Sambuc if (stat("newT3", &s) < 0) e(13);
308*433d6423SLionel Sambuc if (s.st_mode != (mode_t) 0100702) e(14);
309*433d6423SLionel Sambuc /* The cast was because regular modes are
310*433d6423SLionel Sambuc * negative :-(. Anyway, the magic number
311*433d6423SLionel Sambuc * should be (S_IFREG | S_IRWXU | S_IWOTH)
312*433d6423SLionel Sambuc * for POSIX.
313*433d6423SLionel Sambuc */
314*433d6423SLionel Sambuc if (s.st_nlink != 1) e(15);
315*433d6423SLionel Sambuc if (s.st_size != 255L) e(16);
316*433d6423SLionel Sambuc if (fstat(n1, &s1) < 0) e(17);
317*433d6423SLionel Sambuc if (s.st_dev != s1.st_dev) e(18);
318*433d6423SLionel Sambuc if (s.st_ino != s1.st_ino) e(19);
319*433d6423SLionel Sambuc if (s.st_mode != s1.st_mode) e(20);
320*433d6423SLionel Sambuc if (s.st_nlink != s1.st_nlink) e(21);
321*433d6423SLionel Sambuc if (s.st_uid != s1.st_uid) e(22);
322*433d6423SLionel Sambuc if (s.st_gid != s1.st_gid) e(23);
323*433d6423SLionel Sambuc if (s.st_rdev != s1.st_rdev) e(24);
324*433d6423SLionel Sambuc if (s.st_size != s1.st_size) e(25);
325*433d6423SLionel Sambuc if (s.st_atime != s1.st_atime) e(26);
326*433d6423SLionel Sambuc if (close(n1) < 0) e(27);
327*433d6423SLionel Sambuc if (unlink("newT3") < 0) e(28);
328*433d6423SLionel Sambuc
329*433d6423SLionel Sambuc umask(040);
330*433d6423SLionel Sambuc if ((n = creat("T3a", 0777)) < 0) e(29);
331*433d6423SLionel Sambuc if (stat("T3a", &s) < 0) e(30);
332*433d6423SLionel Sambuc if (s.st_mode != (mode_t) 0100737) e(31); /* negative :-( */
333*433d6423SLionel Sambuc if (unlink("T3a") < 0) e(32);
334*433d6423SLionel Sambuc if (close(n1) < 0) e(33);
335*433d6423SLionel Sambuc
336*433d6423SLionel Sambuc /* Dup */
337*433d6423SLionel Sambuc if ((n = creat("T3b", 0777)) < 0) e(34);
338*433d6423SLionel Sambuc if (close(n) < 0) e(35);
339*433d6423SLionel Sambuc if ((n = open("T3b", O_RDWR)) < 0) e(36);
340*433d6423SLionel Sambuc if ((n1 = dup(n)) != n + 1) e(37);
341*433d6423SLionel Sambuc if (write(n, a, 255) != 255) e(38);
342*433d6423SLionel Sambuc read(n1, b, 20);
343*433d6423SLionel Sambuc if (lseek(n, 0L, SEEK_SET) != 0L) e(39);
344*433d6423SLionel Sambuc if ((j = read(n1, b, 512)) != 255) e(40);
345*433d6423SLionel Sambuc if (unlink("T3b") < 0) e(41);
346*433d6423SLionel Sambuc if (close(n) < 0) e(42);
347*433d6423SLionel Sambuc if (close(n1) < 0) e(43);
348*433d6423SLionel Sambuc
349*433d6423SLionel Sambuc }
350*433d6423SLionel Sambuc }
351*433d6423SLionel Sambuc
test19f()352*433d6423SLionel Sambuc void test19f()
353*433d6423SLionel Sambuc {
354*433d6423SLionel Sambuc /* Test large files to see if indirect block stuff works. */
355*433d6423SLionel Sambuc
356*433d6423SLionel Sambuc int fd, i;
357*433d6423SLionel Sambuc long pos;
358*433d6423SLionel Sambuc
359*433d6423SLionel Sambuc subtest = 6;
360*433d6423SLionel Sambuc
361*433d6423SLionel Sambuc if (passes > 0) return; /* takes too long to repeat this test */
362*433d6423SLionel Sambuc for (i = 0; i < NBOUNDS; i ++) {
363*433d6423SLionel Sambuc pos = 1024L * bounds[i];
364*433d6423SLionel Sambuc fd = creat("T19f", 0777);
365*433d6423SLionel Sambuc if (fd < 0) e(10*i+1);
366*433d6423SLionel Sambuc if (lseek(fd, pos, 0) < 0) e(10*i+2);
367*433d6423SLionel Sambuc if (write(fd, buff, 30720) != 30720) e(10*i+3);
368*433d6423SLionel Sambuc if (close(fd) < 0) e(10*i+3);
369*433d6423SLionel Sambuc if (unlink("T19f") < 0) e(10*i+4);
370*433d6423SLionel Sambuc }
371*433d6423SLionel Sambuc }
372*433d6423SLionel Sambuc
test19g()373*433d6423SLionel Sambuc void test19g()
374*433d6423SLionel Sambuc {
375*433d6423SLionel Sambuc /* Test POSIX calls for pipe, read, write, lseek and close. */
376*433d6423SLionel Sambuc
377*433d6423SLionel Sambuc int pipefd[2], n, i, fd;
378*433d6423SLionel Sambuc char buf[512], buf2[512];
379*433d6423SLionel Sambuc
380*433d6423SLionel Sambuc subtest = 7;
381*433d6423SLionel Sambuc
382*433d6423SLionel Sambuc for (i = 0; i < 512; i++) buf[i] = i % 128;
383*433d6423SLionel Sambuc
384*433d6423SLionel Sambuc if (pipe(pipefd) < 0) e(1);
385*433d6423SLionel Sambuc if (write(pipefd[1], buf, 512) != 512) e(2);
386*433d6423SLionel Sambuc if (read(pipefd[0], buf2, 512) != 512) e(3);
387*433d6423SLionel Sambuc if (close(pipefd[1]) != 0) e(4);
388*433d6423SLionel Sambuc if (close(pipefd[1]) >= 0) e(5);
389*433d6423SLionel Sambuc if (read(pipefd[0], buf2, 1) != 0) e(6);
390*433d6423SLionel Sambuc if (close(pipefd[0]) != 0) e(7);
391*433d6423SLionel Sambuc
392*433d6423SLionel Sambuc /* Test O_NONBLOCK on pipes. */
393*433d6423SLionel Sambuc if (pipe(pipefd) < 0) e(8);
394*433d6423SLionel Sambuc if (fcntl(pipefd[0], F_SETFL, O_NONBLOCK) != 0) e(9);
395*433d6423SLionel Sambuc if (read(pipefd[0], buf2, 1) != -1) e(10);
396*433d6423SLionel Sambuc if (errno != EAGAIN) e(11);
397*433d6423SLionel Sambuc if (close(pipefd[0]) != 0) e(12);
398*433d6423SLionel Sambuc if (close(pipefd[1]) != 0) e(13);
399*433d6423SLionel Sambuc
400*433d6423SLionel Sambuc /* Test read and lseek. */
401*433d6423SLionel Sambuc if ( (fd = creat("T19.g1", 0777)) != 3) e(14); /* create test file */
402*433d6423SLionel Sambuc if (write(fd, buf, 512) != 512) e(15);
403*433d6423SLionel Sambuc errno = 3000;
404*433d6423SLionel Sambuc if (read(fd, buf2, 512) != -1) e(16);
405*433d6423SLionel Sambuc if (errno != EBADF) e(17);
406*433d6423SLionel Sambuc if (close(fd) != 0) e(18);
407*433d6423SLionel Sambuc if ( (fd = open("T19.g1", O_RDWR)) != 3) e(19);
408*433d6423SLionel Sambuc if (read(fd, buf2, 512) != 512) e(20);
409*433d6423SLionel Sambuc if (read(fd, buf2, 512) != 0) e(21);
410*433d6423SLionel Sambuc if (lseek(fd, 100L, SEEK_SET) != 100L) e(22);
411*433d6423SLionel Sambuc if (read(fd, buf2, 512) != 412) e(23);
412*433d6423SLionel Sambuc if (lseek(fd, 1000L, SEEK_SET) != 1000L) e(24);
413*433d6423SLionel Sambuc
414*433d6423SLionel Sambuc /* Test write. */
415*433d6423SLionel Sambuc if (lseek(fd, -1000L, SEEK_CUR) != 0) e(25);
416*433d6423SLionel Sambuc if (write(fd, buf, 512) != 512) e(26);
417*433d6423SLionel Sambuc if (lseek(fd, 2L, SEEK_SET) != 2) e(27);
418*433d6423SLionel Sambuc if (write(fd, buf, 3) != 3) e(28);
419*433d6423SLionel Sambuc if (lseek(fd, -2L, SEEK_CUR) != 3) e(29);
420*433d6423SLionel Sambuc if (write(fd, &buf[30], 1) != 1) e(30);
421*433d6423SLionel Sambuc if (lseek(fd, 2L, SEEK_CUR) != 6) e(31);
422*433d6423SLionel Sambuc if (write(fd, &buf[60], 1) != 1) e(32);
423*433d6423SLionel Sambuc if (lseek(fd, -512L, SEEK_END) != 0) e(33);
424*433d6423SLionel Sambuc if (read(fd, buf2, 8) != 8) e(34);
425*433d6423SLionel Sambuc errno = 4000;
426*433d6423SLionel Sambuc if (buf2[0] != 0 || buf2[1] != 1 || buf2[2] != 0 || buf2[3] != 30) e(35);
427*433d6423SLionel Sambuc if (buf2[4] != 2 || buf2[5] != 5 || buf2[6] != 60 || buf2[7] != 7) e(36);
428*433d6423SLionel Sambuc
429*433d6423SLionel Sambuc /* Turn the O_APPEND flag on. */
430*433d6423SLionel Sambuc if (fcntl(fd, F_SETFL, O_APPEND) != 0) e(37);
431*433d6423SLionel Sambuc if (lseek(fd, 0L, SEEK_SET) != 0) e(38);
432*433d6423SLionel Sambuc if (write(fd, &buf[100], 1) != 1) e(39);
433*433d6423SLionel Sambuc if (lseek(fd, 0L, SEEK_SET) != 0) e(40);
434*433d6423SLionel Sambuc if (read(fd, buf2, 10) != 10) e(41);
435*433d6423SLionel Sambuc if (buf2[0] != 0) e(42);
436*433d6423SLionel Sambuc if (lseek(fd, -1L, SEEK_END) != 512) e(43);
437*433d6423SLionel Sambuc if (read(fd, buf2, 10) != 1) e(44);
438*433d6423SLionel Sambuc if (buf2[0] != 100) e(45);
439*433d6423SLionel Sambuc if (close(fd) != 0) e(46);
440*433d6423SLionel Sambuc
441*433d6423SLionel Sambuc /* Now try write with O_NONBLOCK. */
442*433d6423SLionel Sambuc if (pipe(pipefd) != 0) e(47);
443*433d6423SLionel Sambuc if (fcntl(pipefd[1], F_SETFL, O_NONBLOCK) != 0) e(48);
444*433d6423SLionel Sambuc if (write(pipefd[1], buf, 512) != 512) e(49);
445*433d6423SLionel Sambuc if (write(pipefd[1], buf, 512) != 512) e(50);
446*433d6423SLionel Sambuc errno = 0;
447*433d6423SLionel Sambuc for (i = 1; i < 20; i++) {
448*433d6423SLionel Sambuc n = write(pipefd[1], buf, 512);
449*433d6423SLionel Sambuc if (n == 512) continue;
450*433d6423SLionel Sambuc if (n != -1 || errno != EAGAIN) {e(51); break;}
451*433d6423SLionel Sambuc }
452*433d6423SLionel Sambuc if (read(pipefd[0], buf, 512) != 512) e(52);
453*433d6423SLionel Sambuc if (close(pipefd[0]) != 0) e(53);
454*433d6423SLionel Sambuc
455*433d6423SLionel Sambuc /* Write to a pipe with no reader. This should generate a signal. */
456*433d6423SLionel Sambuc signal(SIGPIPE, pipecatcher);
457*433d6423SLionel Sambuc errno = 0;
458*433d6423SLionel Sambuc if (write(pipefd[1], buf, 1) != -1) e(54);
459*433d6423SLionel Sambuc if (errno != EPIPE) e(55);
460*433d6423SLionel Sambuc if (pipesigs != passes + 1) e(56); /* we should have had the sig now */
461*433d6423SLionel Sambuc if (close(pipefd[1]) != 0) e(57);
462*433d6423SLionel Sambuc errno = 0;
463*433d6423SLionel Sambuc if (write(100, buf, 512) != -1) e(58);
464*433d6423SLionel Sambuc if (errno != EBADF) e(59);
465*433d6423SLionel Sambuc if (unlink("T19.g1") != 0) e(60);
466*433d6423SLionel Sambuc }
467*433d6423SLionel Sambuc
clraa()468*433d6423SLionel Sambuc void clraa()
469*433d6423SLionel Sambuc {
470*433d6423SLionel Sambuc int i;
471*433d6423SLionel Sambuc for (i = 0; i < 100; i++) aa[i] = 0;
472*433d6423SLionel Sambuc }
473*433d6423SLionel Sambuc
pipecatcher(s)474*433d6423SLionel Sambuc void pipecatcher(s)
475*433d6423SLionel Sambuc int s; /* it is supposed to have an arg */
476*433d6423SLionel Sambuc {
477*433d6423SLionel Sambuc pipesigs++;
478*433d6423SLionel Sambuc }
479*433d6423SLionel Sambuc
480