1 /* Test of getcwd function.
2 Copyright (C) 2000-2020 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <https://www.gnu.org/licenses/>. */
25 #include <sys/param.h>
26 #include <libc-diag.h>
29 #define TEST_FUNCTION do_test ()
33 char thepath
[4096]; /* Yes, this limits the environment this test
34 can run it but I honestly don't care about
35 people which have this problem. */
41 if (getcwd (thepath
, sizeof thepath
) == NULL
)
44 /* The path is too long, skip all tests. */
47 puts ("getcwd (thepath, sizeof thepath) failed");
50 len
= strlen (thepath
);
56 for (i
= 0; i
< 4; ++i
)
59 bufs
[i
] = (char *) malloc (sbs
);
62 /* Avoid warnings about the first argument being null when the second
64 DIAG_PUSH_NEEDS_COMMENT
;
65 DIAG_IGNORE_NEEDS_COMMENT (10.1, "-Wnonnull");
66 bufs
[i
] = getcwd (NULL
, sbs
);
67 DIAG_POP_NEEDS_COMMENT
;
72 puts ("getcwd (NULL, sbs) failed");
77 for (; i
< 10; sbs
>>= 1, ++i
)
79 bufs
[i
] = (char *) malloc (MAX (1, sbs
));
83 /* Before we test the result write something in the memory to see
84 whether the allocation went right. */
85 for (i
= 0; i
< 10; ++i
)
86 if (i
!= 4 && bufs
[i
] != NULL
)
87 memset (bufs
[i
], '\xff', lens
[i
]);
89 if (strcmp (thepath
, bufs
[4]) != 0)
92 getcwd (NULL, sbs) = \"%s\", getcwd (thepath, sizeof thepath) = \"%s\"\n",
97 /* Now overwrite all buffers to see that getcwd allocated the buffer
99 for (i
= 0; i
< 10; ++i
)
100 memset (bufs
[i
], i
, lens
[i
]);
102 for (i
= 0; i
< 10; ++i
)
105 /* Test whether the function signals success despite the buffer
107 Avoid warnings about the first argument being null when the second
109 DIAG_PUSH_NEEDS_COMMENT
;
110 DIAG_IGNORE_NEEDS_COMMENT (10.1, "-Wnonnull");
111 if (getcwd (NULL
, len
) != NULL
)
113 puts ("getcwd (NULL, len) didn't failed");
116 DIAG_POP_NEEDS_COMMENT
;
118 bufs
[0] = malloc (len
);
119 bufs
[1] = malloc (len
);
120 bufs
[2] = malloc (len
);
123 if (getcwd (bufs
[1], len
) != NULL
)
125 puts ("getcwd (bufs[1], len) didn't failed");
133 memset (thepath
, '\xfe', sizeof (thepath
));
134 if (getcwd (thepath
, len
) != NULL
)
136 puts ("getcwd (thepath, len) didn't failed");
140 for (i
= len
; i
< sizeof thepath
; ++i
)
141 if (thepath
[i
] != '\xfe')
143 puts ("thepath[i] != '\xfe'");
147 /* Now test handling of correctly sized buffers.
148 Again. avoid warnings about the first argument being null when
149 the second is nonzero. */
150 DIAG_PUSH_NEEDS_COMMENT
;
151 DIAG_IGNORE_NEEDS_COMMENT (10.1, "-Wnonnull");
152 bufs
[0] = getcwd (NULL
, len
+ 1);
155 puts ("getcwd (NULL, len + 1) failed");
158 DIAG_POP_NEEDS_COMMENT
;
161 memset (thepath
, '\xff', sizeof thepath
);
162 if (getcwd (thepath
, len
+ 1) == NULL
)
164 puts ("getcwd (thepath, len + 1) failed");
168 for (i
= len
+ 1; i
< sizeof thepath
; ++i
)
169 if (thepath
[i
] != '\xff')
171 printf ("thepath[%zd] != '\xff'\n", i
);
175 puts ("everything OK");
180 #include "../test-skeleton.c"