415b6a2fb48d42d2b35b3e6dfc0b7d3234caccb4
[oweals/busybox.git] / libbb / concat_path_file.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Utility routines.
4  *
5  * Copyright (C) many different people.
6  * If you wrote this, please acknowledge your work.
7  *
8  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
9  */
10
11 /* concatenate path and file name to new allocation buffer,
12  * not addition '/' if path name already have '/'
13 */
14
15 #include <string.h>
16 #include "libbb.h"
17
18 char *concat_path_file(const char *path, const char *filename)
19 {
20         char *lc;
21
22         if (!path)
23                 path = "";
24         lc = last_char_is(path, '/');
25         while (*filename == '/')
26                 filename++;
27         return bb_xasprintf("%s%s%s", path, (lc==NULL ? "/" : ""), filename);
28 }