Don't use a nested function in rpmatch.
[platform/upstream/glibc.git] / io / tst-getcwd.c
1 /* Test of getcwd function.
2    Copyright (C) 2000-2014 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    <http://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
27
28 #define TEST_FUNCTION do_test ()
29 static int
30 do_test (void)
31 {
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.  */
35   char *bufs[10];
36   size_t lens[10];
37   size_t sbs;
38   size_t len, i;
39
40   if (getcwd (thepath, sizeof thepath) == NULL)
41     {
42       if (errno == ERANGE)
43         /* The path is too long, skip all tests.  */
44         return 0;
45
46       puts ("getcwd (thepath, sizeof thepath) failed");
47       return 1;
48     }
49   len = strlen (thepath);
50
51   sbs = 1;
52   while (sbs < len + 1)
53     sbs <<= 1;
54
55   for (i = 0; i < 4; ++i)
56     {
57       lens[i] = sbs;
58       bufs[i] = (char *) malloc (sbs);
59     }
60
61   bufs[i] = getcwd (NULL, sbs);
62   lens[i] = sbs;
63   if (bufs[i] == NULL)
64     {
65       puts ("getcwd (NULL, sbs) failed");
66       return 1;
67     }
68   ++i;
69
70   for (; i < 10; sbs >>= 1, ++i)
71     {
72       bufs[i] = (char *) malloc (MAX (1, sbs));
73       lens[i] = sbs;
74     }
75
76   /* Before we test the result write something in the memory to see
77      whether the allocation went right.  */
78   for (i = 0; i < 10; ++i)
79     if (i != 4 && bufs[i] != NULL)
80       memset (bufs[i], '\xff', lens[i]);
81
82   if (strcmp (thepath, bufs[4]) != 0)
83     {
84       printf ("\
85 getcwd (NULL, sbs) = \"%s\", getcwd (thepath, sizeof thepath) = \"%s\"\n",
86               bufs[4], thepath);
87       return 1;
88     }
89
90   /* Now overwrite all buffers to see that getcwd allocated the buffer
91      of right size.  */
92   for (i = 0; i < 10; ++i)
93     memset (bufs[i], i, lens[i]);
94
95   for (i = 0; i < 10; ++i)
96     free (bufs[i]);
97
98   /* Test whether the function signals success despite the buffer
99      being too small.  */
100   if (getcwd (NULL, len) != NULL)
101     {
102       puts ("getcwd (NULL, len) didn't failed");
103       return 1;
104     }
105
106   bufs[0] = malloc (len);
107   bufs[1] = malloc (len);
108   bufs[2] = malloc (len);
109   if (bufs[1] != NULL)
110     {
111       if (getcwd (bufs[1], len) != NULL)
112         {
113           puts ("getcwd (bufs[1], len) didn't failed");
114           return 1;
115         }
116       free (bufs[0]);
117       free (bufs[1]);
118       free (bufs[2]);
119     }
120
121   memset (thepath, '\xfe', sizeof (thepath));
122   if (getcwd (thepath, len) != NULL)
123     {
124       puts ("getcwd (thepath, len) didn't failed");
125       return 1;
126     }
127
128   for (i = len; i < sizeof thepath; ++i)
129     if (thepath[i] != '\xfe')
130       {
131         puts ("thepath[i] != '\xfe'");
132         return 1;
133       }
134
135   /* Now test handling of correctly sized buffers.  */
136   bufs[0] = getcwd (NULL, len + 1);
137   if (bufs[0] == NULL)
138     {
139       puts ("getcwd (NULL, len + 1) failed");
140       return 1;
141     }
142   free (bufs[0]);
143
144   memset (thepath, '\xff', sizeof thepath);
145   if (getcwd (thepath, len + 1) == NULL)
146     {
147       puts ("getcwd (thepath, len + 1) failed");
148       return 1;
149     }
150
151   for (i = len + 1; i < sizeof thepath; ++i)
152     if (thepath[i] != '\xff')
153       {
154         printf ("thepath[%zd] != '\xff'\n", i);
155         return 1;
156       }
157
158   puts ("everything OK");
159
160   return 0;
161 }
162
163 #include "../test-skeleton.c"