blob: dd6909fc2a652cd40ca7ce9297fef59e7b02c6e2 [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 *
"Robert P. J. Day"5d8843e2006-07-10 11:41:19 +00008 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersenbdfd0d72001-10-24 05:00:29 +00009 */
10
Denis Vlasenko6bf05cf2008-05-07 12:18:48 +000011/* Concatenate path and filename to new allocated buffer.
12 * Add '/' only as needed (no duplicate // are produced).
13 * If path is NULL, it is assumed to be "/".
14 * filename should not be NULL.
15 */
Eric Andersene5dfced2001-04-09 22:48:12 +000016
17#include "libbb.h"
18
Rob Landleydfba7412006-03-06 20:47:33 +000019char *concat_path_file(const char *path, const char *filename)
Eric Andersene5dfced2001-04-09 22:48:12 +000020{
Eric Andersenc911a432001-05-15 17:42:16 +000021 char *lc;
Eric Andersen5a071bc2001-07-07 04:27:35 +000022
23 if (!path)
Mike Frysinger9dc93ac2005-05-09 21:51:15 +000024 path = "";
Eric Andersenc911a432001-05-15 17:42:16 +000025 lc = last_char_is(path, '/');
Eric Andersen34506362001-08-02 05:02:46 +000026 while (*filename == '/')
Eric Andersen4ad13e52001-05-07 23:01:32 +000027 filename++;
Rob Landleyd921b2e2006-08-03 15:41:12 +000028 return xasprintf("%s%s%s", path, (lc==NULL ? "/" : ""), filename);
Eric Andersene5dfced2001-04-09 22:48:12 +000029}