Blame lib/readlink.c

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