Update.
[glibc.git] / io / tst-getcwd.c
blob4b5bc62c7cfb1e65cfed5bb00f92083bc67c16e2
1 /* Test of getcwd function.
2 Copyright (C) 2000 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 Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 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 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
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>
29 #define TEST_FUNCTION do_test ()
30 static int
31 do_test (void)
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;
40 int i;
42 if (getcwd (thepath, sizeof thepath) == NULL)
44 if (errno == ERANGE)
45 /* The path is too long, skip all tests. */
46 return 0;
48 puts ("getcwd (thepath, sizeof thepath) failed");
49 return 1;
51 len = strlen (thepath);
53 sbs = 1;
54 while (sbs < len + 1)
55 sbs <<= 1;
57 for (i = 0; i < 4; ++i)
59 lens[i] = sbs;
60 bufs[i] = (char *) malloc (sbs);
63 bufs[i] = getcwd (NULL, sbs);
64 lens[i] = sbs;
65 if (bufs[i] == NULL)
67 puts ("getcwd (NULL, sbs) failed");
68 return 1;
70 ++i;
72 for (; i < 10; sbs >>= 1, ++i)
74 bufs[i] = (char *) malloc (MAX (1, sbs));
75 lens[i] = sbs;
78 /* Before we test the result write something in the memory to see
79 whether the allocation went right. */
80 for (i = 0; i < 10; ++i)
81 if (i != 4 && bufs[i] != NULL)
82 memset (bufs[i], '\xff', lens[i]);
84 if (strcmp (thepath, bufs[4]) != 0)
86 printf ("\
87 getcwd (NULL, sbs) = \"%s\", getcwd (thepath, sizeof thepath) = \"%s\"\n",
88 bufs[4], thepath);
89 return 1;
92 /* Now overwrite all buffers to see that getcwd allocated the buffer
93 of right size. */
94 for (i = 0; i < 10; ++i)
95 memset (bufs[i], i, lens[i]);
97 for (i = 0; i < 10; ++i)
98 free (bufs[i]);
100 /* Test whether the function signals success despite the buffer
101 being too small. */
102 if (getcwd (NULL, len) != NULL)
104 puts ("getcwd (NULL, len) didn't failed");
105 return 1;
108 bufs[0] = malloc (len);
109 bufs[1] = malloc (len);
110 bufs[2] = malloc (len);
111 if (bufs[1] != NULL)
113 if (getcwd (bufs[1], len) != NULL)
115 puts ("getcwd (bufs[1], len) didn't failed");
116 return 1;
118 free (bufs[0]);
119 free (bufs[1]);
120 free (bufs[2]);
123 memset (thepath, '\xfe', sizeof (thepath));
124 if (getcwd (thepath, len) != NULL)
126 puts ("getcwd (thepath, len) didn't failed");
127 return 1;
130 for (i = len; i < sizeof thepath; ++i)
131 if (thepath[i] != '\xfe')
133 puts ("thepath[i] != '\xfe'");
134 return 1;
137 /* Now test handling of correctly sized buffers. */
138 bufs[0] = getcwd (NULL, len + 1);
139 if (bufs[0] == NULL)
141 puts ("getcwd (NULL, len + 1) failed");
142 return 1;
144 free (bufs[0]);
146 memset (thepath, '\xff', sizeof thepath);
147 if (getcwd (thepath, len + 1) == NULL)
149 puts ("getcwd (thepath, len + 1) failed");
150 return 1;
153 for (i = len + 1; i < sizeof thepath; ++i)
154 if (thepath[i] != '\xff')
156 printf ("thepath[%d] != '\xff'\n", i);
157 return 1;
160 puts ("everything OK");
162 return 0;
165 #include "../test-skeleton.c"