Import upstream version 1.28
[debian/tar] / gnu / areadlink.c
1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* areadlink.c -- readlink wrapper to return the link name in malloc'd storage
4    Unlike xreadlink and xreadlink_with_size, don't ever call exit.
5
6    Copyright (C) 2001, 2003-2007, 2009-2014 Free Software Foundation, Inc.
7
8    This program is free software: you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20
21 /* Written by Jim Meyering <jim@meyering.net>
22    and Bruno Haible <bruno@clisp.org>.  */
23
24 #include <config.h>
25
26 /* Specification.  */
27 #include "areadlink.h"
28
29 #include "careadlinkat.h"
30
31 #include <stdlib.h>
32 #include <unistd.h>
33
34 /* Get the symbolic link value of FILENAME and put it into BUFFER, with
35    size BUFFER_SIZE.  This function acts like readlink but has
36    readlinkat's signature.  */
37 static ssize_t
38 careadlinkatcwd (int fd, char const *filename, char *buffer,
39                  size_t buffer_size)
40 {
41   /* FD must be AT_FDCWD here, otherwise the caller is using this
42      function in contexts it was not meant for.  */
43   if (fd != AT_FDCWD)
44     abort ();
45   return readlink (filename, buffer, buffer_size);
46 }
47
48 /* Call readlink to get the symbolic link value of FILENAME.
49    Return a pointer to that NUL-terminated string in malloc'd storage.
50    If readlink fails, return NULL and set errno.
51    If allocation fails, or if the link value is longer than SIZE_MAX :-),
52    return NULL and set errno to ENOMEM.  */
53
54 char *
55 areadlink (char const *filename)
56 {
57   return careadlinkat (AT_FDCWD, filename, NULL, 0, NULL, careadlinkatcwd);
58 }