~libbls/libbls/bench-vs-plain

« back to all changes in this revision

Viewing changes to src/util.c

  • Committer: alf82 at freemail
  • Date: 2009-03-21 11:09:31 UTC
  • mfrom: (33.1.8 options-impl)
  • Revision ID: alf82@freemail.gr-20090321110931-6s65h81h3ue3pnmr
Merged buffer options implementation.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2008, 2009 Alexandros Frantzis, Michael Iatrou
 
3
 *
 
4
 * This file is part of libbls.
 
5
 *
 
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
 
9
 * version.
 
10
 *
 
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
 
14
 * details.
 
15
 *
 
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/>.
 
18
 */
 
19
 
 
20
/**
 
21
 * @file util.c
 
22
 *
 
23
 * Implementation of utility functions used by libbls.
 
24
 */
 
25
 
 
26
#include "util.h"
 
27
#include "stdlib.h"
 
28
#include "string.h"
 
29
#include "errno.h"
 
30
#include "sys/types.h"
 
31
 
 
32
/** 
 
33
 * Joins two path components to form a full path.
 
34
 *
 
35
 * The resulting path should be freed using free() when it is not needed
 
36
 * anymore.
 
37
 * 
 
38
 * @param[out] result the resulting combined path
 
39
 * @param path1 first path component
 
40
 * @param path2 second path component
 
41
 * 
 
42
 * @return the operation error code
 
43
 */
 
44
int path_join(char **result, char *path1, char *path2)
 
45
{
 
46
        if (result == NULL || path1 == NULL || path2 == NULL)
 
47
                return_error(EINVAL);
 
48
 
 
49
        /* TODO: Path separator depends on platform! */
 
50
        char *sep = "/";
 
51
        int need_sep = 0;
 
52
 
 
53
        size_t result_size = strlen(path1) + strlen(path2) + 1;
 
54
 
 
55
        /* Check if we need to add a path separator between the two components. */
 
56
        if (path1[strlen(path1) - 1] != sep[0]) {
 
57
                need_sep = 1;
 
58
                result_size += 1;
 
59
        }
 
60
 
 
61
        *result = malloc(result_size);
 
62
        if (*result == NULL)
 
63
                return_error(ENOMEM);
 
64
 
 
65
        strncpy(*result, path1, result_size);
 
66
 
 
67
        if (need_sep == 1)
 
68
                strncat(*result, sep, result_size - strlen(*result) - 1);
 
69
 
 
70
        strncat(*result, path2, result_size - strlen(*result) - 1);
 
71
 
 
72
        return 0;
 
73
}
 
74