|
Packit Service |
a2ae7a |
/* Test of getcwd() function.
|
|
Packit Service |
a2ae7a |
Copyright (C) 2009-2019 Free Software Foundation, Inc.
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
This program is free software: you can redistribute it and/or modify
|
|
Packit Service |
a2ae7a |
it under the terms of the GNU General Public License as published by
|
|
Packit Service |
a2ae7a |
the Free Software Foundation; either version 3 of the License, or
|
|
Packit Service |
a2ae7a |
(at your option) any later version.
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
This program is distributed in the hope that it will be useful,
|
|
Packit Service |
a2ae7a |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
a2ae7a |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit Service |
a2ae7a |
GNU General Public License for more details.
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
You should have received a copy of the GNU General Public License
|
|
Packit Service |
a2ae7a |
along with this program. If not, see <https://www.gnu.org/licenses/>. */
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
#include <config.h>
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
#include <unistd.h>
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
#include "signature.h"
|
|
Packit Service |
a2ae7a |
SIGNATURE_CHECK (getcwd, char *, (char *, size_t));
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
#include <errno.h>
|
|
Packit Service |
a2ae7a |
#include <stdio.h>
|
|
Packit Service |
a2ae7a |
#include <stdlib.h>
|
|
Packit Service |
a2ae7a |
#include <string.h>
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
#include "macros.h"
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
int
|
|
Packit Service |
a2ae7a |
main (int argc, char **argv)
|
|
Packit Service |
a2ae7a |
{
|
|
Packit Service |
a2ae7a |
char *pwd1;
|
|
Packit Service |
a2ae7a |
char *pwd2;
|
|
Packit Service |
a2ae7a |
/* If the user provides an argument, attempt to chdir there first. */
|
|
Packit Service |
a2ae7a |
if (1 < argc)
|
|
Packit Service |
a2ae7a |
{
|
|
Packit Service |
a2ae7a |
if (chdir (argv[1]) == 0)
|
|
Packit Service |
a2ae7a |
printf ("changed to directory %s\n", argv[1]);
|
|
Packit Service |
a2ae7a |
}
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
pwd1 = getcwd (NULL, 0);
|
|
Packit Service |
a2ae7a |
ASSERT (pwd1 && *pwd1);
|
|
Packit Service |
a2ae7a |
if (1 < argc)
|
|
Packit Service |
a2ae7a |
printf ("cwd=%s\n", pwd1);
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
/* Make sure the result is usable. */
|
|
Packit Service |
a2ae7a |
ASSERT (chdir (pwd1) == 0);
|
|
Packit Service |
a2ae7a |
ASSERT (chdir (".//./.") == 0);
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
/* Make sure that result is normalized. */
|
|
Packit Service |
a2ae7a |
pwd2 = getcwd (NULL, 0);
|
|
Packit Service |
a2ae7a |
ASSERT (pwd2);
|
|
Packit Service |
a2ae7a |
ASSERT (strcmp (pwd1, pwd2) == 0);
|
|
Packit Service |
a2ae7a |
free (pwd2);
|
|
Packit Service |
a2ae7a |
{
|
|
Packit Service |
a2ae7a |
size_t len = strlen (pwd1);
|
|
Packit Service |
a2ae7a |
ssize_t i = len - 10;
|
|
Packit Service |
a2ae7a |
if (i < 1)
|
|
Packit Service |
a2ae7a |
i = 1;
|
|
Packit Service |
a2ae7a |
pwd2 = getcwd (NULL, len + 1);
|
|
Packit Service |
a2ae7a |
ASSERT (pwd2);
|
|
Packit Service |
a2ae7a |
free (pwd2);
|
|
Packit Service |
a2ae7a |
pwd2 = malloc (len + 2);
|
|
Packit Service |
a2ae7a |
for ( ; i <= len; i++)
|
|
Packit Service |
a2ae7a |
{
|
|
Packit Service |
a2ae7a |
char *tmp;
|
|
Packit Service |
a2ae7a |
errno = 0;
|
|
Packit Service |
a2ae7a |
ASSERT (getcwd (pwd2, i) == NULL);
|
|
Packit Service |
a2ae7a |
ASSERT (errno == ERANGE);
|
|
Packit Service |
a2ae7a |
/* Allow either glibc or BSD behavior, since POSIX allows both. */
|
|
Packit Service |
a2ae7a |
errno = 0;
|
|
Packit Service |
a2ae7a |
tmp = getcwd (NULL, i);
|
|
Packit Service |
a2ae7a |
if (tmp)
|
|
Packit Service |
a2ae7a |
{
|
|
Packit Service |
a2ae7a |
ASSERT (strcmp (pwd1, tmp) == 0);
|
|
Packit Service |
a2ae7a |
free (tmp);
|
|
Packit Service |
a2ae7a |
}
|
|
Packit Service |
a2ae7a |
else
|
|
Packit Service |
a2ae7a |
{
|
|
Packit Service |
a2ae7a |
ASSERT (errno == ERANGE);
|
|
Packit Service |
a2ae7a |
}
|
|
Packit Service |
a2ae7a |
}
|
|
Packit Service |
a2ae7a |
ASSERT (getcwd (pwd2, len + 1) == pwd2);
|
|
Packit Service |
a2ae7a |
pwd2[len] = '/';
|
|
Packit Service |
a2ae7a |
pwd2[len + 1] = '\0';
|
|
Packit Service |
a2ae7a |
}
|
|
Packit Service |
a2ae7a |
ASSERT (strstr (pwd2, "/./") == NULL);
|
|
Packit Service |
a2ae7a |
ASSERT (strstr (pwd2, "/../") == NULL);
|
|
Packit Service |
a2ae7a |
ASSERT (strstr (pwd2 + 1 + (pwd2[1] == '/'), "//") == NULL);
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
/* Validate a POSIX requirement on size. */
|
|
Packit Service |
a2ae7a |
errno = 0;
|
|
Packit Service |
a2ae7a |
ASSERT (getcwd(pwd2, 0) == NULL);
|
|
Packit Service |
a2ae7a |
ASSERT (errno == EINVAL);
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
free (pwd1);
|
|
Packit Service |
a2ae7a |
free (pwd2);
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
return 0;
|
|
Packit Service |
a2ae7a |
}
|