chiark / gitweb /
Apply https://sourceware.org/git/?p=glibc.git;a=commit;h=d5dd6189d506068ed11c8bfa1e1e...
[eglibc.git] / io / tst-getcwd.c
1 /* Test of getcwd function.
2    Copyright (C) 2000, 2002 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, write to the Free
18    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19    02111-1307 USA.  */
20
21 #include <errno.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <sys/param.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   bufs[i] = getcwd (NULL, sbs);
63   lens[i] = sbs;
64   if (bufs[i] == NULL)
65     {
66       puts ("getcwd (NULL, sbs) failed");
67       return 1;
68     }
69   ++i;
70
71   for (; i < 10; sbs >>= 1, ++i)
72     {
73       bufs[i] = (char *) malloc (MAX (1, sbs));
74       lens[i] = sbs;
75     }
76
77   /* Before we test the result write something in the memory to see
78      whether the allocation went right.  */
79   for (i = 0; i < 10; ++i)
80     if (i != 4 && bufs[i] != NULL)
81       memset (bufs[i], '\xff', lens[i]);
82
83   if (strcmp (thepath, bufs[4]) != 0)
84     {
85       printf ("\
86 getcwd (NULL, sbs) = \"%s\", getcwd (thepath, sizeof thepath) = \"%s\"\n",
87               bufs[4], thepath);
88       return 1;
89     }
90
91   /* Now overwrite all buffers to see that getcwd allocated the buffer
92      of right size.  */
93   for (i = 0; i < 10; ++i)
94     memset (bufs[i], i, lens[i]);
95
96   for (i = 0; i < 10; ++i)
97     free (bufs[i]);
98
99   /* Test whether the function signals success despite the buffer
100      being too small.  */
101   if (getcwd (NULL, len) != NULL)
102     {
103       puts ("getcwd (NULL, len) didn't failed");
104       return 1;
105     }
106
107   bufs[0] = malloc (len);
108   bufs[1] = malloc (len);
109   bufs[2] = malloc (len);
110   if (bufs[1] != NULL)
111     {
112       if (getcwd (bufs[1], len) != NULL)
113         {
114           puts ("getcwd (bufs[1], len) didn't failed");
115           return 1;
116         }
117       free (bufs[0]);
118       free (bufs[1]);
119       free (bufs[2]);
120     }
121
122   memset (thepath, '\xfe', sizeof (thepath));
123   if (getcwd (thepath, len) != NULL)
124     {
125       puts ("getcwd (thepath, len) didn't failed");
126       return 1;
127     }
128
129   for (i = len; i < sizeof thepath; ++i)
130     if (thepath[i] != '\xfe')
131       {
132         puts ("thepath[i] != '\xfe'");
133         return 1;
134       }
135
136   /* Now test handling of correctly sized buffers.  */
137   bufs[0] = getcwd (NULL, len + 1);
138   if (bufs[0] == NULL)
139     {
140       puts ("getcwd (NULL, len + 1) failed");
141       return 1;
142     }
143   free (bufs[0]);
144
145   memset (thepath, '\xff', sizeof thepath);
146   if (getcwd (thepath, len + 1) == NULL)
147     {
148       puts ("getcwd (thepath, len + 1) failed");
149       return 1;
150     }
151
152   for (i = len + 1; i < sizeof thepath; ++i)
153     if (thepath[i] != '\xff')
154       {
155         printf ("thepath[%zd] != '\xff'\n", i);
156         return 1;
157       }
158
159   puts ("everything OK");
160
161   return 0;
162 }
163
164 #include "../test-skeleton.c"