Blame lib/readlink.c

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