Blame lib/xreadlink.c

Packit 9e4112
/* xreadlink.c -- readlink wrapper to return the link name in malloc'd storage
Packit 9e4112
Packit 9e4112
   Copyright (C) 2001, 2003-2007, 2009-2018 Free Software Foundation, Inc.
Packit 9e4112
Packit 9e4112
   This program is free software: you can redistribute it and/or modify
Packit 9e4112
   it under the terms of the GNU General Public License as published by
Packit 9e4112
   the Free Software Foundation; either version 3 of the License, or
Packit 9e4112
   (at your option) any later version.
Packit 9e4112
Packit 9e4112
   This program is distributed in the hope that it will be useful,
Packit 9e4112
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 9e4112
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 9e4112
   GNU General Public License for more details.
Packit 9e4112
Packit 9e4112
   You should have received a copy of the GNU General Public License
Packit 9e4112
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
Packit 9e4112
Packit 9e4112
/* Written by Jim Meyering <jim@meyering.net>
Packit 9e4112
   and Bruno Haible <bruno@clisp.org>.  */
Packit 9e4112
Packit 9e4112
#include <config.h>
Packit 9e4112
Packit 9e4112
/* Specification.  */
Packit 9e4112
#include "xreadlink.h"
Packit 9e4112
Packit 9e4112
#include <errno.h>
Packit 9e4112
Packit 9e4112
#include "areadlink.h"
Packit 9e4112
#include "xalloc.h"
Packit 9e4112
Packit 9e4112
/* Call readlink to get the symbolic link value of FILENAME.
Packit 9e4112
   Return a pointer to that NUL-terminated string in malloc'd storage.
Packit 9e4112
   If readlink fails, return NULL and set errno.
Packit 9e4112
   If realloc fails, or if the link value is longer than SIZE_MAX :-),
Packit 9e4112
   give a diagnostic and exit.  */
Packit 9e4112
Packit 9e4112
char *
Packit 9e4112
xreadlink (char const *filename)
Packit 9e4112
{
Packit 9e4112
  char *result = areadlink (filename);
Packit 9e4112
  if (result == NULL && errno == ENOMEM)
Packit 9e4112
    xalloc_die ();
Packit 9e4112
  return result;
Packit 9e4112
}