io/lockf: Include bits/types.h before __OFF_T_MATCHES_OFF64_T check
[platform/upstream/glibc.git] / io / tst-getcwd.c
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.
5
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.
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
14    Lesser General Public License for more details.
15
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/>.  */
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>
26 #include <libc-diag.h>
27
28
29 #define TEST_FUNCTION do_test ()
30 static int
31 do_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;
39   size_t len, i;
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
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");
66   bufs[i] = getcwd (NULL, sbs);
67   DIAG_POP_NEEDS_COMMENT;
68
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 ("\
92 getcwd (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
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");
111   if (getcwd (NULL, len) != NULL)
112     {
113       puts ("getcwd (NULL, len) didn't failed");
114       return 1;
115     }
116   DIAG_POP_NEEDS_COMMENT;
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
133   memset (thepath, '\xfe', sizeof (thepath));
134   if (getcwd (thepath, len) != NULL)
135     {
136       puts ("getcwd (thepath, len) didn't failed");
137       return 1;
138     }
139
140   for (i = len; i < sizeof thepath; ++i)
141     if (thepath[i] != '\xfe')
142       {
143         puts ("thepath[i] != '\xfe'");
144         return 1;
145       }
146
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);
153   if (bufs[0] == NULL)
154     {
155       puts ("getcwd (NULL, len + 1) failed");
156       return 1;
157     }
158   DIAG_POP_NEEDS_COMMENT;
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       {
171         printf ("thepath[%zd] != '\xff'\n", i);
172         return 1;
173       }
174
175   puts ("everything OK");
176
177   return 0;
178 }
179
180 #include "../test-skeleton.c"