Skip to content

Commit af88c1f

Browse files
authored
[test] Simplify test/unistd/curdir.c. NFC (#23770)
1 parent b1837cc commit af88c1f

File tree

2 files changed

+30
-71
lines changed

2 files changed

+30
-71
lines changed

test/unistd/curdir.c

Lines changed: 22 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
* found in the LICENSE file.
66
*/
77

8+
#include <assert.h>
89
#include <stdio.h>
10+
#include <string.h>
911
#include <errno.h>
1012
#include <unistd.h>
1113
#include <fcntl.h>
@@ -23,90 +25,53 @@ int main() {
2325
);
2426

2527
char buffer[256];
28+
2629
printf("getcwd: %s\n", getcwd(buffer, 256));
27-
printf("errno: %d\n", errno);
30+
printf("errno: %s\n", strerror(errno));
2831
errno = 0;
2932
printf("\n");
3033

3134
printf("chdir(file): %d\n", chdir("/file"));
32-
printf("errno: %d\n", errno);
33-
if (!errno) {
34-
errno = 0;
35-
printf("getcwd: %s\n", getcwd(buffer, 256));
36-
printf("errno: %d\n", errno);
37-
}
35+
printf("errno: %s\n", strerror(errno));
36+
assert(errno != 0);
3837
errno = 0;
3938
printf("\n");
4039

41-
printf("chdir(dir): %d\n", chdir("/dir"));
42-
printf("errno: %d\n", errno);
43-
if (!errno) {
44-
errno = 0;
45-
printf("getcwd: %s\n", getcwd(buffer, 256));
46-
printf("errno: %d\n", errno);
47-
}
48-
errno = 2;
49-
printf("\n");
50-
5140
printf("chdir(\"\"): %d\n", chdir(""));
52-
printf("errno: %d\n", errno);
53-
if (!errno) {
54-
errno = 0;
55-
printf("getcwd: %s\n", getcwd(buffer, 256));
56-
printf("errno: %d\n", errno);
57-
}
58-
errno = 2;
41+
printf("errno: %s\n", strerror(errno));
42+
assert(errno != 0);
43+
errno = 0;
5944
printf("\n");
6045

6146
printf("chdir(device): %d\n", chdir("/device"));
62-
printf("errno: %d\n", errno);
63-
if (!errno) {
64-
errno = 0;
65-
printf("getcwd: %s\n", getcwd(buffer, 256));
66-
printf("errno: %d\n", errno);
67-
}
47+
printf("errno: %s\n", strerror(errno));
48+
assert(errno != 0);
6849
errno = 0;
6950
printf("\n");
7051

7152
printf("chdir(folder): %d\n", chdir("/folder"));
72-
printf("errno: %d\n", errno);
73-
if (!errno) {
74-
errno = 0;
75-
printf("getcwd: %s\n", getcwd(buffer, 256));
76-
printf("errno: %d\n", errno);
77-
}
78-
errno = 0;
53+
printf("errno: %s\n", strerror(errno));
54+
assert(errno == 0);
55+
printf("getcwd: %s\n", getcwd(buffer, 256));
7956
printf("\n");
8057

8158
printf("chdir(nonexistent): %d\n", chdir("/nonexistent"));
82-
printf("errno: %d\n", errno);
83-
if (!errno) {
84-
errno = 0;
85-
printf("getcwd: %s\n", getcwd(buffer, 256));
86-
printf("errno: %d\n", errno);
87-
}
59+
printf("errno: %s\n", strerror(errno));
60+
assert(errno != 0);
8861
errno = 0;
8962
printf("\n");
9063

9164
printf("chdir(link): %d\n", chdir("/link"));
92-
printf("errno: %d\n", errno);
93-
if (!errno) {
94-
errno = 0;
95-
printf("getcwd: %s\n", getcwd(buffer, 256));
96-
printf("errno: %d\n", errno);
97-
}
98-
errno = 0;
65+
printf("errno: %s\n", strerror(errno));
66+
assert(errno == 0);
67+
printf("getcwd: %s\n", getcwd(buffer, 256));
9968
printf("\n");
10069

10170
errno = 0;
10271
printf("fchdir(/): %d\n", fchdir(open("/", O_RDONLY, 0777)));
103-
printf("errno: %d\n", errno);
104-
if (!errno) {
105-
errno = 0;
106-
printf("getcwd: %s\n", getcwd(buffer, 256));
107-
printf("errno: %d\n", errno);
108-
errno = 0;
109-
}
72+
printf("errno: %s\n", strerror(errno));
73+
assert(errno == 0);
74+
printf("getcwd: %s\n", getcwd(buffer, 256));
11075

11176
return 0;
11277
}

test/unistd/curdir.out

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,26 @@
11
getcwd: /
2-
errno: 0
2+
errno: No error information
33

44
chdir(file): -1
5-
errno: 54
6-
7-
chdir(dir): -1
8-
errno: 44
5+
errno: Not a directory
96

107
chdir(""): -1
11-
errno: 44
8+
errno: No such file or directory
129

1310
chdir(device): -1
14-
errno: 54
11+
errno: Not a directory
1512

1613
chdir(folder): 0
17-
errno: 0
14+
errno: No error information
1815
getcwd: /folder
19-
errno: 0
2016

2117
chdir(nonexistent): -1
22-
errno: 44
18+
errno: No such file or directory
2319

2420
chdir(link): 0
25-
errno: 0
21+
errno: No error information
2622
getcwd: /folder
27-
errno: 0
2823

2924
fchdir(/): 0
30-
errno: 0
25+
errno: No error information
3126
getcwd: /
32-
errno: 0

0 commit comments

Comments
 (0)