xreadlink.c 1.4 KB

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