Cleanup _IO_wfile_seekoff.
[glibc.git] / io / tst-getcwd.c
blob421eb18b5ed92a0be2a93f3853d6b1d996025e26
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.
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.
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.
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. */
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, i;
41 if (getcwd (thepath, sizeof thepath) == NULL)
43 if (errno == ERANGE)
44 /* The path is too long, skip all tests. */
45 return 0;
47 puts ("getcwd (thepath, sizeof thepath) failed");
48 return 1;
50 len = strlen (thepath);
52 sbs = 1;
53 while (sbs < len + 1)
54 sbs <<= 1;
56 for (i = 0; i < 4; ++i)
58 lens[i] = sbs;
59 bufs[i] = (char *) malloc (sbs);
62 bufs[i] = getcwd (NULL, sbs);
63 lens[i] = sbs;
64 if (bufs[i] == NULL)
66 puts ("getcwd (NULL, sbs) failed");
67 return 1;
69 ++i;
71 for (; i < 10; sbs >>= 1, ++i)
73 bufs[i] = (char *) malloc (MAX (1, sbs));
74 lens[i] = sbs;
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]);
83 if (strcmp (thepath, bufs[4]) != 0)
85 printf ("\
86 getcwd (NULL, sbs) = \"%s\", getcwd (thepath, sizeof thepath) = \"%s\"\n",
87 bufs[4], thepath);
88 return 1;
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]);
96 for (i = 0; i < 10; ++i)
97 free (bufs[i]);
99 /* Test whether the function signals success despite the buffer
100 being too small. */
101 if (getcwd (NULL, len) != NULL)
103 puts ("getcwd (NULL, len) didn't failed");
104 return 1;
107 bufs[0] = malloc (len);
108 bufs[1] = malloc (len);
109 bufs[2] = malloc (len);
110 if (bufs[1] != NULL)
112 if (getcwd (bufs[1], len) != NULL)
114 puts ("getcwd (bufs[1], len) didn't failed");
115 return 1;
117 free (bufs[0]);
118 free (bufs[1]);
119 free (bufs[2]);
122 memset (thepath, '\xfe', sizeof (thepath));
123 if (getcwd (thepath, len) != NULL)
125 puts ("getcwd (thepath, len) didn't failed");
126 return 1;
129 for (i = len; i < sizeof thepath; ++i)
130 if (thepath[i] != '\xfe')
132 puts ("thepath[i] != '\xfe'");
133 return 1;
136 /* Now test handling of correctly sized buffers. */
137 bufs[0] = getcwd (NULL, len + 1);
138 if (bufs[0] == NULL)
140 puts ("getcwd (NULL, len + 1) failed");
141 return 1;
143 free (bufs[0]);
145 memset (thepath, '\xff', sizeof thepath);
146 if (getcwd (thepath, len + 1) == NULL)
148 puts ("getcwd (thepath, len + 1) failed");
149 return 1;
152 for (i = len + 1; i < sizeof thepath; ++i)
153 if (thepath[i] != '\xff')
155 printf ("thepath[%zd] != '\xff'\n", i);
156 return 1;
159 puts ("everything OK");
161 return 0;
164 #include "../test-skeleton.c"