Blame io/tst-getcwd.c

Packit 6c4009
/* Test of getcwd function.
Packit 6c4009
   Copyright (C) 2000-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <sys/param.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
#define TEST_FUNCTION do_test ()
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  char thepath[4096];	/* Yes, this limits the environment this test
Packit 6c4009
			   can run it but I honestly don't care about
Packit 6c4009
			   people which have this problem.  */
Packit 6c4009
  char *bufs[10];
Packit 6c4009
  size_t lens[10];
Packit 6c4009
  size_t sbs;
Packit 6c4009
  size_t len, i;
Packit 6c4009
Packit 6c4009
  if (getcwd (thepath, sizeof thepath) == NULL)
Packit 6c4009
    {
Packit 6c4009
      if (errno == ERANGE)
Packit 6c4009
	/* The path is too long, skip all tests.  */
Packit 6c4009
	return 0;
Packit 6c4009
Packit 6c4009
      puts ("getcwd (thepath, sizeof thepath) failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  len = strlen (thepath);
Packit 6c4009
Packit 6c4009
  sbs = 1;
Packit 6c4009
  while (sbs < len + 1)
Packit 6c4009
    sbs <<= 1;
Packit 6c4009
Packit 6c4009
  for (i = 0; i < 4; ++i)
Packit 6c4009
    {
Packit 6c4009
      lens[i] = sbs;
Packit 6c4009
      bufs[i] = (char *) malloc (sbs);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  bufs[i] = getcwd (NULL, sbs);
Packit 6c4009
  lens[i] = sbs;
Packit 6c4009
  if (bufs[i] == NULL)
Packit 6c4009
    {
Packit 6c4009
      puts ("getcwd (NULL, sbs) failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  ++i;
Packit 6c4009
Packit 6c4009
  for (; i < 10; sbs >>= 1, ++i)
Packit 6c4009
    {
Packit 6c4009
      bufs[i] = (char *) malloc (MAX (1, sbs));
Packit 6c4009
      lens[i] = sbs;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Before we test the result write something in the memory to see
Packit 6c4009
     whether the allocation went right.  */
Packit 6c4009
  for (i = 0; i < 10; ++i)
Packit 6c4009
    if (i != 4 && bufs[i] != NULL)
Packit 6c4009
      memset (bufs[i], '\xff', lens[i]);
Packit 6c4009
Packit 6c4009
  if (strcmp (thepath, bufs[4]) != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("\
Packit 6c4009
getcwd (NULL, sbs) = \"%s\", getcwd (thepath, sizeof thepath) = \"%s\"\n",
Packit 6c4009
	      bufs[4], thepath);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Now overwrite all buffers to see that getcwd allocated the buffer
Packit 6c4009
     of right size.  */
Packit 6c4009
  for (i = 0; i < 10; ++i)
Packit 6c4009
    memset (bufs[i], i, lens[i]);
Packit 6c4009
Packit 6c4009
  for (i = 0; i < 10; ++i)
Packit 6c4009
    free (bufs[i]);
Packit 6c4009
Packit 6c4009
  /* Test whether the function signals success despite the buffer
Packit 6c4009
     being too small.  */
Packit 6c4009
  if (getcwd (NULL, len) != NULL)
Packit 6c4009
    {
Packit 6c4009
      puts ("getcwd (NULL, len) didn't failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  bufs[0] = malloc (len);
Packit 6c4009
  bufs[1] = malloc (len);
Packit 6c4009
  bufs[2] = malloc (len);
Packit 6c4009
  if (bufs[1] != NULL)
Packit 6c4009
    {
Packit 6c4009
      if (getcwd (bufs[1], len) != NULL)
Packit 6c4009
	{
Packit 6c4009
	  puts ("getcwd (bufs[1], len) didn't failed");
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
      free (bufs[0]);
Packit 6c4009
      free (bufs[1]);
Packit 6c4009
      free (bufs[2]);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  memset (thepath, '\xfe', sizeof (thepath));
Packit 6c4009
  if (getcwd (thepath, len) != NULL)
Packit 6c4009
    {
Packit 6c4009
      puts ("getcwd (thepath, len) didn't failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  for (i = len; i < sizeof thepath; ++i)
Packit 6c4009
    if (thepath[i] != '\xfe')
Packit 6c4009
      {
Packit 6c4009
	puts ("thepath[i] != '\xfe'");
Packit 6c4009
	return 1;
Packit 6c4009
      }
Packit 6c4009
Packit 6c4009
  /* Now test handling of correctly sized buffers.  */
Packit 6c4009
  bufs[0] = getcwd (NULL, len + 1);
Packit 6c4009
  if (bufs[0] == NULL)
Packit 6c4009
    {
Packit 6c4009
      puts ("getcwd (NULL, len + 1) failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  free (bufs[0]);
Packit 6c4009
Packit 6c4009
  memset (thepath, '\xff', sizeof thepath);
Packit 6c4009
  if (getcwd (thepath, len + 1) == NULL)
Packit 6c4009
    {
Packit 6c4009
      puts ("getcwd (thepath, len + 1) failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  for (i = len + 1; i < sizeof thepath; ++i)
Packit 6c4009
    if (thepath[i] != '\xff')
Packit 6c4009
      {
Packit 6c4009
	printf ("thepath[%zd] != '\xff'\n", i);
Packit 6c4009
	return 1;
Packit 6c4009
      }
Packit 6c4009
Packit 6c4009
  puts ("everything OK");
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#include "../test-skeleton.c"