tar: use utime() to restore timestamps
[minix.git] / test / t40b.c
blob967e13522c33adad5c73b868bb5d41cd0ba12e6f
1 /* t40b.c
3 * Test regular files
5 * Select works on regular files, (pseudo) terminal devices, streams-based
6 * files, FIFOs, pipes, and sockets. This test verifies selecting for regular
7 * file descriptors. "File descriptors associated with regular files shall
8 * always select true for ready to read, ready to write, and error conditions"
9 * - Open Group. Although we set a timeout, the select should return
10 * immediately.
12 * This test is part of a bigger select test. It expects as argument which sub-
13 * test it is.
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <unistd.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <fcntl.h>
22 #include <sys/select.h>
23 #include <errno.h>
24 #include <time.h>
26 #define FILE1 "selecttestb-1"
27 #define FILES 2
28 #define TIME 3
30 #define MAX_ERROR 10
32 int errct = 0, subtest = -1;
33 char errorbuf[1000];
35 void e(int n, char *s) {
36 printf("Subtest %d, error %d, %s\n", subtest, n, s);
38 if (errct++ > MAX_ERROR) {
39 printf("Too many errors; test aborted\n");
40 exit(errct);
44 int main(int argc, char **argv) {
45 int fd1, fd2, retval;
46 fd_set fds_read, fds_write, fds_error;
47 struct timeval tv;
48 time_t start, end;
50 /* Get subtest number */
51 if(argc != 2) {
52 printf("Usage: %s subtest_no\n", argv[0]);
53 exit(-1);
54 } else if(sscanf(argv[1], "%d", &subtest) != 1) {
55 printf("Usage: %s subtest_no\n", argv[0]);
56 exit(-1);
59 /* Set timeout */
60 tv.tv_sec = TIME;
61 tv.tv_usec = 0;
63 /* Open a file for writing */
64 if((fd1 = open(FILE1, O_WRONLY|O_CREAT, 0644)) == -1) {
65 snprintf(errorbuf, sizeof(errorbuf), "failed to open file %s for writing",
66 FILE1);
67 e(1, errorbuf);
68 perror(NULL);
69 exit(1);
72 /* Open the same file for reading */
73 if((fd2 = open(FILE1, O_RDONLY)) == -1) {
74 snprintf(errorbuf, sizeof(errorbuf), "failed to open file %s for reading",
75 FILE1);
76 e(2, errorbuf);
77 perror(NULL);
78 exit(1);
81 /* Clear file descriptor bit masks */
82 FD_ZERO(&fds_read); FD_ZERO(&fds_write); FD_ZERO(&fds_error);
84 /* Fill bit mask */
85 FD_SET(fd1, &fds_write);
86 FD_SET(fd2, &fds_read);
87 FD_SET(fd1, &fds_error);
88 FD_SET(fd2, &fds_error);
90 /* Do the select and time how long it takes */
91 start = time(NULL);
92 retval = select(fd2+1, &fds_read, &fds_write, &fds_error, &tv);
93 end = time(NULL);
95 /* Correct amount of ready file descriptors? 1 read + 1 write + 2 errors */
96 if(retval != 4) {
97 e(3, "four fds should be set");
100 /* Test resulting bit masks */
101 if(!FD_ISSET(fd1, &fds_write)) e(4, "write should be set");
102 if(!FD_ISSET(fd2, &fds_read)) e(5, "read should be set");
103 if(!FD_ISSET(fd1, &fds_error)) e(6, "error should be set");
104 if(!FD_ISSET(fd2, &fds_error)) e(7, "error should be set");
106 /* Was it instantaneous? */
107 if(end-start != TIME - TIME) {
108 snprintf(errorbuf,sizeof(errorbuf),"time spent blocking is not %d, but %ld",
109 TIME - TIME, (long int) (end-start));
110 e(8, errorbuf);
113 /* Wait for read to become ready on O_WRONLY. This should fail immediately. */
114 FD_ZERO(&fds_read); FD_ZERO(&fds_write); FD_ZERO(&fds_error);
115 FD_SET(fd1, &fds_read);
116 FD_SET(fd1, &fds_error);
117 FD_SET(fd2, &fds_error);
118 tv.tv_sec = TIME;
119 tv.tv_usec = 0;
120 retval = select(fd2+1, &fds_read, NULL, &fds_error, &tv);
122 /* Correct amount of ready file descriptors? 1 read + 2 error */
123 if(retval != 3) e(9, "incorrect amount of ready file descriptors");
124 if(!FD_ISSET(fd1, &fds_read)) e(10, "read should be set");
125 if(!FD_ISSET(fd1, &fds_error)) e(11, "error should be set");
126 if(!FD_ISSET(fd2, &fds_error)) e(12, "error should be set");
128 /* Try again as above, bit this time with O_RDONLY in the write set */
129 FD_ZERO(&fds_error);
130 FD_SET(fd2, &fds_write);
131 FD_SET(fd1, &fds_error);
132 FD_SET(fd2, &fds_error);
133 tv.tv_sec = TIME;
134 tv.tv_usec = 0;
135 retval = select(fd2+1, NULL, &fds_write, &fds_error, &tv);
137 /* Correct amount of ready file descriptors? 1 write + 2 errors */
138 if(retval != 3) e(13, "incorrect amount of ready file descriptors");
139 if(!FD_ISSET(fd2, &fds_write)) e(14, "write should be set");
140 if(!FD_ISSET(fd1, &fds_error)) e(15, "error should be set");
141 if(!FD_ISSET(fd2, &fds_error)) e(16, "error should be set");
143 close(fd1);
144 close(fd2);
145 unlink(FILE1);
147 exit(errct);