]> git.ipfire.org Git - thirdparty/glibc.git/blame - io/tst-getcwd.c
Update copyright dates with scripts/update-copyrights
[thirdparty/glibc.git] / io / tst-getcwd.c
CommitLineData
9ff9add9 1/* Test of getcwd function.
2b778ceb 2 Copyright (C) 2000-2021 Free Software Foundation, Inc.
9ff9add9
UD
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000.
5
6 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
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.
9ff9add9
UD
10
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
41bdb6e2 14 Lesser General Public License for more details.
9ff9add9 15
41bdb6e2 16 You should have received a copy of the GNU Lesser General Public
59ba27a6 17 License along with the GNU C Library; if not, see
5a82c748 18 <https://www.gnu.org/licenses/>. */
9ff9add9
UD
19
20#include <errno.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <unistd.h>
25#include <sys/param.h>
978e8ac3 26#include <libc-diag.h>
9ff9add9
UD
27
28
29#define TEST_FUNCTION do_test ()
30static int
31do_test (void)
32{
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. */
36 char *bufs[10];
37 size_t lens[10];
38 size_t sbs;
57b36a0a 39 size_t len, i;
9ff9add9
UD
40
41 if (getcwd (thepath, sizeof thepath) == NULL)
42 {
43 if (errno == ERANGE)
44 /* The path is too long, skip all tests. */
45 return 0;
46
47 puts ("getcwd (thepath, sizeof thepath) failed");
48 return 1;
49 }
50 len = strlen (thepath);
51
52 sbs = 1;
53 while (sbs < len + 1)
54 sbs <<= 1;
55
56 for (i = 0; i < 4; ++i)
57 {
58 lens[i] = sbs;
59 bufs[i] = (char *) malloc (sbs);
60 }
61
978e8ac3
MS
62 /* Avoid warnings about the first argument being null when the second
63 is nonzero. */
64 DIAG_PUSH_NEEDS_COMMENT;
65 DIAG_IGNORE_NEEDS_COMMENT (10.1, "-Wnonnull");
9ff9add9 66 bufs[i] = getcwd (NULL, sbs);
978e8ac3
MS
67 DIAG_POP_NEEDS_COMMENT;
68
9ff9add9
UD
69 lens[i] = sbs;
70 if (bufs[i] == NULL)
71 {
72 puts ("getcwd (NULL, sbs) failed");
73 return 1;
74 }
75 ++i;
76
77 for (; i < 10; sbs >>= 1, ++i)
78 {
79 bufs[i] = (char *) malloc (MAX (1, sbs));
80 lens[i] = sbs;
81 }
82
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]);
88
89 if (strcmp (thepath, bufs[4]) != 0)
90 {
91 printf ("\
92getcwd (NULL, sbs) = \"%s\", getcwd (thepath, sizeof thepath) = \"%s\"\n",
93 bufs[4], thepath);
94 return 1;
95 }
96
97 /* Now overwrite all buffers to see that getcwd allocated the buffer
98 of right size. */
99 for (i = 0; i < 10; ++i)
100 memset (bufs[i], i, lens[i]);
101
102 for (i = 0; i < 10; ++i)
103 free (bufs[i]);
104
105 /* Test whether the function signals success despite the buffer
978e8ac3
MS
106 being too small.
107 Avoid warnings about the first argument being null when the second
108 is nonzero. */
109 DIAG_PUSH_NEEDS_COMMENT;
110 DIAG_IGNORE_NEEDS_COMMENT (10.1, "-Wnonnull");
9ff9add9
UD
111 if (getcwd (NULL, len) != NULL)
112 {
113 puts ("getcwd (NULL, len) didn't failed");
114 return 1;
115 }
978e8ac3 116 DIAG_POP_NEEDS_COMMENT;
9ff9add9
UD
117
118 bufs[0] = malloc (len);
119 bufs[1] = malloc (len);
120 bufs[2] = malloc (len);
121 if (bufs[1] != NULL)
122 {
123 if (getcwd (bufs[1], len) != NULL)
124 {
125 puts ("getcwd (bufs[1], len) didn't failed");
126 return 1;
127 }
128 free (bufs[0]);
129 free (bufs[1]);
130 free (bufs[2]);
131 }
132
ca41028b 133 memset (thepath, '\xfe', sizeof (thepath));
9ff9add9
UD
134 if (getcwd (thepath, len) != NULL)
135 {
136 puts ("getcwd (thepath, len) didn't failed");
137 return 1;
138 }
139
ca41028b
UD
140 for (i = len; i < sizeof thepath; ++i)
141 if (thepath[i] != '\xfe')
142 {
143 puts ("thepath[i] != '\xfe'");
144 return 1;
145 }
146
978e8ac3
MS
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");
9ff9add9
UD
152 bufs[0] = getcwd (NULL, len + 1);
153 if (bufs[0] == NULL)
154 {
155 puts ("getcwd (NULL, len + 1) failed");
156 return 1;
157 }
978e8ac3 158 DIAG_POP_NEEDS_COMMENT;
9ff9add9
UD
159 free (bufs[0]);
160
161 memset (thepath, '\xff', sizeof thepath);
162 if (getcwd (thepath, len + 1) == NULL)
163 {
164 puts ("getcwd (thepath, len + 1) failed");
165 return 1;
166 }
167
168 for (i = len + 1; i < sizeof thepath; ++i)
169 if (thepath[i] != '\xff')
170 {
9a2d7205 171 printf ("thepath[%zd] != '\xff'\n", i);
9ff9add9
UD
172 return 1;
173 }
174
175 puts ("everything OK");
176
177 return 0;
178}
179
180#include "../test-skeleton.c"