view tests/test-fchdir.c @ 12060:95a12a00ea4f

readlink: document portability issue with symlink length Per comments in areadlink, ERANGE on a too-small buffer is expected on some platforms; making the readlink module guarantee GNU behavior of truncated contents is counter-productive, since we would be duplicating areadlink to learn a-priori how large to make the buffer, and since truncated contents are not as useful. * doc/posix-functions/lstat.texi (lstat): Mention that some file systems have bogus st_size on symlinks, and mention the areadlink-with-size module. * doc/posix-functions/fstatat.texi (fstatat): Likewise. * doc/posix-functions/readlink.texi (readlink): Mention the areadlink module, and ERANGE failure. * doc/posix-functions/readlinkat.texi (readlinkat): Likewise. * tests/test-readlink.c (main): Relax test for AIX, HP-UX. Signed-off-by: Eric Blake <ebb9@byu.net>
author Eric Blake <ebb9@byu.net>
date Mon, 21 Sep 2009 14:40:20 -0600
parents 9750527ffbab
children e45d9bb2233e
line wrap: on
line source

/* Test changing to a directory named by a file descriptor.
   Copyright (C) 2009 Free Software Foundation, Inc.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

/* Written by Eric Blake <ebb9@byu.net>, 2009.  */

#include <config.h>

#include <unistd.h>

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define ASSERT(expr) \
  do									     \
    {									     \
      if (!(expr))							     \
        {								     \
          fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
          fflush (stderr);						     \
          abort ();							     \
        }								     \
    }									     \
  while (0)

int
main ()
{
  char *cwd = getcwd (NULL, 0);
  int fd = open (".", O_RDONLY);
  int i;

  ASSERT (cwd);
  ASSERT (0 <= fd);

  /* Check for failure cases.  */
  {
    int bad_fd = open ("/dev/null", O_RDONLY);
    ASSERT (0 <= bad_fd);
    errno = 0;
    ASSERT (fchdir (bad_fd) == -1);
    ASSERT (errno == ENOTDIR);
    ASSERT (close (bad_fd) == 0);
    errno = 0;
    ASSERT (fchdir (-1) == -1);
    ASSERT (errno == EBADF);
  }

  /* Repeat test twice, once in '.' and once in '..'.  */
  for (i = 0; i < 2; i++)
    {
      ASSERT (chdir (".." + 1 - i) == 0);
      ASSERT (fchdir (fd) == 0);
      {
	size_t len = strlen (cwd) + 1;
	char *new_dir = malloc (len);
	ASSERT (new_dir);
	ASSERT (getcwd (new_dir, len) == new_dir);
	ASSERT (strcmp (cwd, new_dir) == 0);
	free (new_dir);
      }

      /* For second iteration, use a cloned fd, to ensure that dup
	 remembers whether an fd was associated with a directory.  */
      if (!i)
	{
	  int new_fd = dup (fd);
	  ASSERT (0 <= new_fd);
	  ASSERT (close (fd) == 0);
	  fd = new_fd;
	}
    }

  free (cwd);
  return 0;
}