2
* Copyright 2008, 2009 Alexandros Frantzis, Michael Iatrou
4
* This file is part of libbls.
6
* libbls is free software: you can redistribute it and/or modify it under the
7
* terms of the GNU Lesser General Public License as published by the Free Software
8
* Foundation, either version 3 of the License, or (at your option) any later
11
* libbls is distributed in the hope that it will be useful, but WITHOUT ANY
12
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16
* You should have received a copy of the GNU General Public License along with
17
* Foobar. If not, see <http://www.gnu.org/licenses/>.
23
* Implementation of utility functions used by libbls.
30
#include "sys/types.h"
33
* Joins two path components to form a full path.
35
* The resulting path should be freed using free() when it is not needed
38
* @param[out] result the resulting combined path
39
* @param path1 first path component
40
* @param path2 second path component
42
* @return the operation error code
44
int path_join(char **result, char *path1, char *path2)
46
if (result == NULL || path1 == NULL || path2 == NULL)
49
/* TODO: Path separator depends on platform! */
53
size_t result_size = strlen(path1) + strlen(path2) + 1;
55
/* Check if we need to add a path separator between the two components. */
56
if (path1[strlen(path1) - 1] != sep[0]) {
61
*result = malloc(result_size);
65
strncpy(*result, path1, result_size);
68
strncat(*result, sep, result_size - strlen(*result) - 1);
70
strncat(*result, path2, result_size - strlen(*result) - 1);