9
9
#include <errno.h>
10
10
#include <fcntl.h>
11
11
#include <stdio.h>
12
+ #include <sys/stat.h>
12
13
#include <unistd.h>
13
14
14
15
// FIXME: Merge this standalone test back into dup.c after new FS can support
@@ -56,12 +57,14 @@ int main() {
56
57
errno = 0 ;
57
58
f5 = dup2 (f4 , -1 );
58
59
assert (f5 == -1 );
60
+ // This error is not reported in the JS filesystem.
59
61
assert (errno == EBADF );
60
62
61
63
// Try calling dup2 with an invalid oldfd
62
64
errno = 0 ;
63
65
int f6 = dup2 (-1 , f5 );
64
66
assert (f6 == -1 );
67
+ // This error is not reported in the JS filesystem.
65
68
assert (errno == EBADF );
66
69
67
70
// Try assigning a large fd
@@ -72,8 +75,57 @@ int main() {
72
75
73
76
errno = 0 ;
74
77
int f9 = dup (-1 );
78
+ // This error is not reported in the JS filesystem.
75
79
assert (f9 == -1 );
76
80
assert (errno == EBADF );
77
81
82
+ off_t offset ;
83
+
84
+ errno = 0 ;
85
+ printf ("DUP\n" );
86
+ mkdir ("working" , 0700 );
87
+ f = open ("working/file" , O_RDWR | O_CREAT );
88
+ f2 = open ("working/file" , O_RDONLY );
89
+ f3 = dup (f );
90
+ printf ("errno: %d\n" , errno );
91
+ printf ("f: %d\n" , f != f2 && f != f3 );
92
+ printf ("f2,f3: %d\n" , f2 != f3 );
93
+
94
+ // dup()ed file descriptors should share all flags and even seek position
95
+ offset = lseek (f3 , 0 , SEEK_CUR );
96
+ printf ("1. f3 offset was %d. Should be 0\n" , (int )offset );
97
+ offset = lseek (f , 1 , SEEK_SET );
98
+ printf ("2. f offset set to %d. Should be 1\n" , (int )offset );
99
+ offset = lseek (f2 , 2 , SEEK_SET );
100
+ printf ("3. f2 offset set to %d. Should be 2\n" , (int )offset );
101
+ offset = lseek (f , 0 , SEEK_CUR );
102
+ printf ("4. f offset now is %d. Should be 1\n" , (int )offset );
103
+ offset = lseek (f2 , 0 , SEEK_CUR );
104
+ printf ("5. f2 offset now is %d. Should be 2\n" , (int )offset );
105
+ offset = lseek (f3 , 0 , SEEK_CUR );
106
+ printf ("6. f3 offset now is %d. Should be 1 (and not 0)\n" , (int )offset );
107
+ offset = lseek (f3 , 3 , SEEK_SET );
108
+ printf ("7. f3 offset set to %d. Should be 3\n" , (int )offset );
109
+ offset = lseek (f , 0 , SEEK_CUR );
110
+ printf ("8. f offset now is %d. Should be 3 (and not 1)\n" , (int )offset );
111
+
112
+ printf ("close(f1): %d\n" , close (f ));
113
+ printf ("close(f2): %d\n" , close (f2 ));
114
+ printf ("close(f3): %d\n" , close (f3 ));
115
+ printf ("\n" );
116
+ errno = 0 ;
117
+
118
+ printf ("DUP2\n" );
119
+ f = open ("/" , O_RDONLY );
120
+ f2 = open ("/" , O_RDONLY );
121
+ f3 = dup2 (f , f2 );
122
+ printf ("errno: %d\n" , errno );
123
+ printf ("f: %d\n" , f != f2 && f != f3 );
124
+ printf ("f2,f3: %d\n" , f2 == f3 );
125
+ printf ("close(f1): %d\n" , close (f ));
126
+ printf ("close(f2): %d\n" , close (f2 ));
127
+ printf ("close(f3): %d\n" , close (f3 ));
128
+ errno = 0 ;
129
+
78
130
return 0 ;
79
131
}
0 commit comments