1 /*
2 * Copyright (C) 2015 Andrew Beekhof <andrew@beekhof.net>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #ifndef _GNU_SOURCE
20 # define _GNU_SOURCE
21 #endif
22
23 #include <crm_internal.h>
24 #include <stdio.h>
25 #include <unistd.h>
26 #include <limits.h>
27 #include <stdlib.h>
28 #include <errno.h>
29
30 /*!
31 * \internal
32 * \brief Return canonicalized form of a path name, like realpath(path, NULL)
33 *
34 * \param[in] path Pathname to canonicalize
35 *
36 * \return Canonicalized pathname
37 * \note The caller is responsible for freeing the result of this funtion.
38 */
39 char *
40 crm_compat_realpath(const char *path)
/* ![[previous]](../icons/n_left.png)
![[next]](../icons/n_right.png)
![[first]](../icons/n_first.png)
![[last]](../icons/n_last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
41 {
42 #if _POSIX_VERSION >= 200809L
43 /* Recent C libraries can dynamically allocate memory as needed */
44 return realpath(path, NULL);
45
46 #elif defined(PATH_MAX)
47 /* Older implementations require pre-allocated memory */
48 /* (this is less desirable because PATH_MAX may be huge or not defined) */
49 char *canonicalized = malloc(PATH_MAX);
50 if ((canonicalized == NULL) || (realpath(path, canonicalized) == NULL)) {
51 return NULL;
52 }
53 return canonicalized;
54 #else
55 errno = ENOTSUP;
56 return NULL;
57 #endif
58 }