1 /* Test of getcwd function.
2 Copyright (C) 2000-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library 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 the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
24 #include <sys/param.h>
25 #include <libc-diag.h>
28 #define TEST_FUNCTION do_test ()
32 char thepath
[4096]; /* Yes, this limits the environment this test
33 can run it but I honestly don't care about
34 people which have this problem. */
40 if (getcwd (thepath
, sizeof thepath
) == NULL
)
43 /* The path is too long, skip all tests. */
46 puts ("getcwd (thepath, sizeof thepath) failed");
49 len
= strlen (thepath
);
55 for (i
= 0; i
< 4; ++i
)
58 bufs
[i
] = (char *) malloc (sbs
);
61 /* Avoid warnings about the first argument being null when the second
63 DIAG_PUSH_NEEDS_COMMENT
;
64 DIAG_IGNORE_NEEDS_COMMENT (10.1, "-Wnonnull");
65 bufs
[i
] = getcwd (NULL
, sbs
);
66 DIAG_POP_NEEDS_COMMENT
;
71 puts ("getcwd (NULL, sbs) failed");
76 for (; i
< 10; sbs
>>= 1, ++i
)
78 bufs
[i
] = (char *) malloc (MAX (1, sbs
));
82 /* Before we test the result write something in the memory to see
83 whether the allocation went right. */
84 for (i
= 0; i
< 10; ++i
)
85 if (i
!= 4 && bufs
[i
] != NULL
)
86 memset (bufs
[i
], '\xff', lens
[i
]);
88 if (strcmp (thepath
, bufs
[4]) != 0)
91 getcwd (NULL, sbs) = \"%s\", getcwd (thepath, sizeof thepath) = \"%s\"\n",
96 /* Now overwrite all buffers to see that getcwd allocated the buffer
98 for (i
= 0; i
< 10; ++i
)
99 memset (bufs
[i
], i
, lens
[i
]);
101 for (i
= 0; i
< 10; ++i
)
104 /* Test whether the function signals success despite the buffer
106 Avoid warnings about the first argument being null when the second
108 DIAG_PUSH_NEEDS_COMMENT
;
109 DIAG_IGNORE_NEEDS_COMMENT (10.1, "-Wnonnull");
110 if (getcwd (NULL
, len
) != NULL
)
112 puts ("getcwd (NULL, len) didn't failed");
115 DIAG_POP_NEEDS_COMMENT
;
117 bufs
[0] = malloc (len
);
118 bufs
[1] = malloc (len
);
119 bufs
[2] = malloc (len
);
122 if (getcwd (bufs
[1], len
) != NULL
)
124 puts ("getcwd (bufs[1], len) didn't failed");
132 memset (thepath
, '\xfe', sizeof (thepath
));
133 if (getcwd (thepath
, len
) != NULL
)
135 puts ("getcwd (thepath, len) didn't failed");
139 for (i
= len
; i
< sizeof thepath
; ++i
)
140 if (thepath
[i
] != '\xfe')
142 puts ("thepath[i] != '\xfe'");
146 /* Now test handling of correctly sized buffers.
147 Again. avoid warnings about the first argument being null when
148 the second is nonzero. */
149 DIAG_PUSH_NEEDS_COMMENT
;
150 DIAG_IGNORE_NEEDS_COMMENT (10.1, "-Wnonnull");
151 bufs
[0] = getcwd (NULL
, len
+ 1);
154 puts ("getcwd (NULL, len + 1) failed");
157 DIAG_POP_NEEDS_COMMENT
;
160 memset (thepath
, '\xff', sizeof thepath
);
161 if (getcwd (thepath
, len
+ 1) == NULL
)
163 puts ("getcwd (thepath, len + 1) failed");
167 for (i
= len
+ 1; i
< sizeof thepath
; ++i
)
168 if (thepath
[i
] != '\xff')
170 printf ("thepath[%zd] != '\xff'\n", i
);
174 puts ("everything OK");
179 #include "../test-skeleton.c"