Blame lib/readlink.c

Packit 33f14e
/* Stub for readlink().
Packit 33f14e
   Copyright (C) 2003-2007, 2009-2017 Free Software Foundation, Inc.
Packit 33f14e
Packit 33f14e
   This program is free software: you can redistribute it and/or modify
Packit 33f14e
   it under the terms of the GNU General Public License as published by
Packit 33f14e
   the Free Software Foundation; either version 3 of the License, or
Packit 33f14e
   (at your option) any later version.
Packit 33f14e
Packit 33f14e
   This program is distributed in the hope that it will be useful,
Packit 33f14e
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 33f14e
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 33f14e
   GNU General Public License for more details.
Packit 33f14e
Packit 33f14e
   You should have received a copy of the GNU General Public License
Packit 33f14e
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit 33f14e
Packit 33f14e
#include <config.h>
Packit 33f14e
Packit 33f14e
/* Specification.  */
Packit 33f14e
#include <unistd.h>
Packit 33f14e
Packit 33f14e
#include <errno.h>
Packit 33f14e
#include <string.h>
Packit 33f14e
#include <sys/stat.h>
Packit 33f14e
Packit 33f14e
#if !HAVE_READLINK
Packit 33f14e
Packit 33f14e
/* readlink() substitute for systems that don't have a readlink() function,
Packit 33f14e
   such as DJGPP 2.03 and mingw32.  */
Packit 33f14e
Packit 33f14e
ssize_t
Packit 33f14e
readlink (const char *name, char *buf _GL_UNUSED,
Packit 33f14e
          size_t bufsize _GL_UNUSED)
Packit 33f14e
{
Packit 33f14e
  struct stat statbuf;
Packit 33f14e
Packit 33f14e
  /* In general we should use lstat() here, not stat().  But on platforms
Packit 33f14e
     without symbolic links, lstat() - if it exists - would be equivalent to
Packit 33f14e
     stat(), therefore we can use stat().  This saves us a configure check.  */
Packit 33f14e
  if (stat (name, &statbuf) >= 0)
Packit 33f14e
    errno = EINVAL;
Packit 33f14e
  return -1;
Packit 33f14e
}
Packit 33f14e
Packit 33f14e
#else /* HAVE_READLINK */
Packit 33f14e
Packit 33f14e
# undef readlink
Packit 33f14e
Packit 33f14e
/* readlink() wrapper that uses correct types, for systems like cygwin
Packit 33f14e
   1.5.x where readlink returns int, and which rejects trailing slash,
Packit 33f14e
   for Solaris 9.  */
Packit 33f14e
Packit 33f14e
ssize_t
Packit 33f14e
rpl_readlink (const char *name, char *buf, size_t bufsize)
Packit 33f14e
{
Packit 33f14e
# if READLINK_TRAILING_SLASH_BUG
Packit 33f14e
  size_t len = strlen (name);
Packit 33f14e
  if (len && name[len - 1] == '/')
Packit 33f14e
    {
Packit 33f14e
      /* Even if name without the slash is a symlink to a directory,
Packit 33f14e
         both lstat() and stat() must resolve the trailing slash to
Packit 33f14e
         the directory rather than the symlink.  We can therefore
Packit 33f14e
         safely use stat() to distinguish between EINVAL and
Packit 33f14e
         ENOTDIR/ENOENT, avoiding extra overhead of rpl_lstat().  */
Packit 33f14e
      struct stat st;
Packit 33f14e
      if (stat (name, &st) == 0)
Packit 33f14e
        errno = EINVAL;
Packit 33f14e
      return -1;
Packit 33f14e
    }
Packit 33f14e
# endif /* READLINK_TRAILING_SLASH_BUG */
Packit 33f14e
  return readlink (name, buf, bufsize);
Packit 33f14e
}
Packit 33f14e
Packit 33f14e
#endif /* HAVE_READLINK */