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
26 #include <sys/param.h>
29 #define TEST_FUNCTION do_test ()
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. */
41 if (getcwd (thepath
, sizeof thepath
) == NULL
)
44 /* The path is too long, skip all tests. */
47 puts ("getcwd (thepath, sizeof thepath) failed");
50 len
= strlen (thepath
);
56 for (i
= 0; i
< 4; ++i
)
59 bufs
[i
] = (char *) malloc (sbs
);
62 bufs
[i
] = getcwd (NULL
, sbs
);
66 puts ("getcwd (NULL, sbs) failed");
71 for (; i
< 10; sbs
>>= 1, ++i
)
73 bufs
[i
] = (char *) malloc (MAX (1, 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)
86 getcwd (NULL, sbs) = \"%s\", getcwd (thepath, sizeof thepath) = \"%s\"\n",
91 /* Now overwrite all buffers to see that getcwd allocated the buffer
93 for (i
= 0; i
< 10; ++i
)
94 memset (bufs
[i
], i
, lens
[i
]);
96 for (i
= 0; i
< 10; ++i
)
99 /* Test whether the function signals success despite the buffer
101 if (getcwd (NULL
, len
) != NULL
)
103 puts ("getcwd (NULL, len) didn't failed");
107 bufs
[0] = malloc (len
);
108 bufs
[1] = malloc (len
);
109 bufs
[2] = malloc (len
);
112 if (getcwd (bufs
[1], len
) != NULL
)
114 puts ("getcwd (bufs[1], len) didn't failed");
122 memset (thepath
, '\xfe', sizeof (thepath
));
123 if (getcwd (thepath
, len
) != NULL
)
125 puts ("getcwd (thepath, len) didn't failed");
129 for (i
= len
; i
< sizeof thepath
; ++i
)
130 if (thepath
[i
] != '\xfe')
132 puts ("thepath[i] != '\xfe'");
136 /* Now test handling of correctly sized buffers. */
137 bufs
[0] = getcwd (NULL
, len
+ 1);
140 puts ("getcwd (NULL, len + 1) failed");
145 memset (thepath
, '\xff', sizeof thepath
);
146 if (getcwd (thepath
, len
+ 1) == NULL
)
148 puts ("getcwd (thepath, len + 1) failed");
152 for (i
= len
+ 1; i
< sizeof thepath
; ++i
)
153 if (thepath
[i
] != '\xff')
155 printf ("thepath[%zd] != '\xff'\n", i
);
159 puts ("everything OK");
164 #include "../test-skeleton.c"