Blame gettext-tools/gnulib-lib/areadlink.c

Packit Bot 06c835
/* areadlink.c -- readlink wrapper to return the link name in malloc'd storage
Packit Bot 06c835
   Unlike xreadlink and xreadlink_with_size, don't ever call exit.
Packit Bot 06c835
Packit Bot 06c835
   Copyright (C) 2001, 2003-2007, 2009-2015 Free Software Foundation, Inc.
Packit Bot 06c835
Packit Bot 06c835
   This program is free software: you can redistribute it and/or modify
Packit Bot 06c835
   it under the terms of the GNU General Public License as published by
Packit Bot 06c835
   the Free Software Foundation; either version 3 of the License, or
Packit Bot 06c835
   (at your option) any later version.
Packit Bot 06c835
Packit Bot 06c835
   This program is distributed in the hope that it will be useful,
Packit Bot 06c835
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Bot 06c835
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Bot 06c835
   GNU General Public License for more details.
Packit Bot 06c835
Packit Bot 06c835
   You should have received a copy of the GNU General Public License
Packit Bot 06c835
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit Bot 06c835
Packit Bot 06c835
/* Written by Jim Meyering <jim@meyering.net>
Packit Bot 06c835
   and Bruno Haible <bruno@clisp.org>.  */
Packit Bot 06c835
Packit Bot 06c835
#include <config.h>
Packit Bot 06c835
Packit Bot 06c835
/* Specification.  */
Packit Bot 06c835
#include "areadlink.h"
Packit Bot 06c835
Packit Bot 06c835
#include "careadlinkat.h"
Packit Bot 06c835
Packit Bot 06c835
#include <stdlib.h>
Packit Bot 06c835
#include <unistd.h>
Packit Bot 06c835
Packit Bot 06c835
/* Get the symbolic link value of FILENAME and put it into BUFFER, with
Packit Bot 06c835
   size BUFFER_SIZE.  This function acts like readlink but has
Packit Bot 06c835
   readlinkat's signature.  */
Packit Bot 06c835
static ssize_t
Packit Bot 06c835
careadlinkatcwd (int fd, char const *filename, char *buffer,
Packit Bot 06c835
                 size_t buffer_size)
Packit Bot 06c835
{
Packit Bot 06c835
  /* FD must be AT_FDCWD here, otherwise the caller is using this
Packit Bot 06c835
     function in contexts it was not meant for.  */
Packit Bot 06c835
  if (fd != AT_FDCWD)
Packit Bot 06c835
    abort ();
Packit Bot 06c835
  return readlink (filename, buffer, buffer_size);
Packit Bot 06c835
}
Packit Bot 06c835
Packit Bot 06c835
/* Call readlink to get the symbolic link value of FILENAME.
Packit Bot 06c835
   Return a pointer to that NUL-terminated string in malloc'd storage.
Packit Bot 06c835
   If readlink fails, return NULL and set errno.
Packit Bot 06c835
   If allocation fails, or if the link value is longer than SIZE_MAX :-),
Packit Bot 06c835
   return NULL and set errno to ENOMEM.  */
Packit Bot 06c835
Packit Bot 06c835
char *
Packit Bot 06c835
areadlink (char const *filename)
Packit Bot 06c835
{
Packit Bot 06c835
  return careadlinkat (AT_FDCWD, filename, NULL, 0, NULL, careadlinkatcwd);
Packit Bot 06c835
}