blob: 5b4b7f113110abe51823c57a376bc1c8311e804f [file] [log] [blame]
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001/* vi: set sw=4 ts=4: */
Eric Andersene5dfced2001-04-09 22:48:12 +00002/*
Eric Andersenbdfd0d72001-10-24 05:00:29 +00003 * Utility routines.
Eric Andersene5dfced2001-04-09 22:48:12 +00004 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) many different people.
Eric Andersencb81e642003-07-14 21:21:08 +00006 * If you wrote this, please acknowledge your work.
Eric Andersenbdfd0d72001-10-24 05:00:29 +00007 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02008 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersenbdfd0d72001-10-24 05:00:29 +00009 */
Denys Vlasenkoebe6d9d2017-10-05 14:40:24 +020010#include "libbb.h"
Eric Andersenbdfd0d72001-10-24 05:00:29 +000011
Denis Vlasenko6bf05cf2008-05-07 12:18:48 +000012/* Concatenate path and filename to new allocated buffer.
13 * Add '/' only as needed (no duplicate // are produced).
14 * If path is NULL, it is assumed to be "/".
15 * filename should not be NULL.
16 */
Eric Andersene5dfced2001-04-09 22:48:12 +000017
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000018char* FAST_FUNC concat_path_file(const char *path, const char *filename)
Eric Andersene5dfced2001-04-09 22:48:12 +000019{
Eric Andersenc911a432001-05-15 17:42:16 +000020 char *lc;
Eric Andersen5a071bc2001-07-07 04:27:35 +000021
22 if (!path)
Mike Frysinger9dc93ac2005-05-09 21:51:15 +000023 path = "";
Eric Andersenc911a432001-05-15 17:42:16 +000024 lc = last_char_is(path, '/');
Eric Andersen34506362001-08-02 05:02:46 +000025 while (*filename == '/')
Eric Andersen4ad13e52001-05-07 23:01:32 +000026 filename++;
Rob Landleyd921b2e2006-08-03 15:41:12 +000027 return xasprintf("%s%s%s", path, (lc==NULL ? "/" : ""), filename);
Eric Andersene5dfced2001-04-09 22:48:12 +000028}