]> sourceware.org Git - glibc.git/blame - io/tst-getcwd.c
Update.
[glibc.git] / io / tst-getcwd.c
CommitLineData
9ff9add9
UD
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.
5
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.
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 Library General Public License for more details.
15
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. */
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 ()
30static int
31do_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;
40 int i;
41
42 if (getcwd (thepath, sizeof thepath) == NULL)
43 {
44 if (errno == ERANGE)
45 /* The path is too long, skip all tests. */
46 return 0;
47
48 puts ("getcwd (thepath, sizeof thepath) failed");
49 return 1;
50 }
51 len = strlen (thepath);
52
53 sbs = 1;
54 while (sbs < len + 1)
55 sbs <<= 1;
56
57 for (i = 0; i < 4; ++i)
58 {
59 lens[i] = sbs;
60 bufs[i] = (char *) malloc (sbs);
61 }
62
63 bufs[i] = getcwd (NULL, sbs);
64 lens[i] = sbs;
65 if (bufs[i] == NULL)
66 {
67 puts ("getcwd (NULL, sbs) failed");
68 return 1;
69 }
70 ++i;
71
72 for (; i < 10; sbs >>= 1, ++i)
73 {
74 bufs[i] = (char *) malloc (MAX (1, sbs));
75 lens[i] = sbs;
76 }
77
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]);
83
84 if (strcmp (thepath, bufs[4]) != 0)
85 {
86 printf ("\
87getcwd (NULL, sbs) = \"%s\", getcwd (thepath, sizeof thepath) = \"%s\"\n",
88 bufs[4], thepath);
89 return 1;
90 }
91
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]);
96
97 for (i = 0; i < 10; ++i)
98 free (bufs[i]);
99
100 /* Test whether the function signals success despite the buffer
101 being too small. */
102 if (getcwd (NULL, len) != NULL)
103 {
104 puts ("getcwd (NULL, len) didn't failed");
105 return 1;
106 }
107
108 bufs[0] = malloc (len);
109 bufs[1] = malloc (len);
110 bufs[2] = malloc (len);
111 if (bufs[1] != NULL)
112 {
113 if (getcwd (bufs[1], len) != NULL)
114 {
115 puts ("getcwd (bufs[1], len) didn't failed");
116 return 1;
117 }
118 free (bufs[0]);
119 free (bufs[1]);
120 free (bufs[2]);
121 }
122
ca41028b 123 memset (thepath, '\xfe', sizeof (thepath));
9ff9add9
UD
124 if (getcwd (thepath, len) != NULL)
125 {
126 puts ("getcwd (thepath, len) didn't failed");
127 return 1;
128 }
129
ca41028b
UD
130 for (i = len; i < sizeof thepath; ++i)
131 if (thepath[i] != '\xfe')
132 {
133 puts ("thepath[i] != '\xfe'");
134 return 1;
135 }
136
9ff9add9
UD
137 /* Now test handling of correctly sized buffers. */
138 bufs[0] = getcwd (NULL, len + 1);
139 if (bufs[0] == NULL)
140 {
141 puts ("getcwd (NULL, len + 1) failed");
142 return 1;
143 }
144 free (bufs[0]);
145
146 memset (thepath, '\xff', sizeof thepath);
147 if (getcwd (thepath, len + 1) == NULL)
148 {
149 puts ("getcwd (thepath, len + 1) failed");
150 return 1;
151 }
152
153 for (i = len + 1; i < sizeof thepath; ++i)
154 if (thepath[i] != '\xff')
155 {
156 printf ("thepath[%d] != '\xff'\n", i);
157 return 1;
158 }
159
160 puts ("everything OK");
161
162 return 0;
163}
164
165#include "../test-skeleton.c"
This page took 0.040504 seconds and 5 git commands to generate.