Blame gnulib-tests/test-getcwd-lgpl.c

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