path.cpp revision a835c21f3b4c17e3141e7e3e3abf5aeb215357b1
32ded45b3caba42c8a2315a20bbfabb513fa54c1vboxsync * IPRT - Path Manipulation.
e64031e20c39650a7bc902a3e1aba613b9415deevboxsync * Copyright (C) 2006-2007 Sun Microsystems, Inc.
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync * available from http://www.virtualbox.org. This file is free software;
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync * you can redistribute it and/or modify it under the terms of the GNU
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync * General Public License (GPL) as published by the Free Software
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync * The contents of this file may alternatively be used under the terms
7d9e0d289e799c0ee5b9307fe7358ddc53bd34ddvboxsync * of the Common Development and Distribution License Version 1.0
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync * VirtualBox OSE distribution, in which case the provisions of the
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync * CDDL are applicable instead of those of the GPL.
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync * You may elect to license modified versions of this file under the
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync * terms and conditions of either the GPL or the CDDL or both.
9148e7ca5f9ee1d467112d347bfb2de2b97254d2vboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
af1bd0025dd5d8be5f1468689d0d77d4839a3be5vboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync * additional information or have any questions.
9148e7ca5f9ee1d467112d347bfb2de2b97254d2vboxsync/*******************************************************************************
9148e7ca5f9ee1d467112d347bfb2de2b97254d2vboxsync* Header Files *
295a2ae5a4db2c930f7096c6cf55b5fa4ee5b018vboxsync*******************************************************************************/
9148e7ca5f9ee1d467112d347bfb2de2b97254d2vboxsync * Strips the filename from a path.
9148e7ca5f9ee1d467112d347bfb2de2b97254d2vboxsync * @param pszPath Path which filename should be extracted from.
9148e7ca5f9ee1d467112d347bfb2de2b97254d2vboxsync /* handle separators. */
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync /* the end */
be641d8f2dc377d6a4403f3822d8bed08f51e28avboxsync /* will never get here */
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync * Strips the extension from a path.
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync * @param pszPath Path which extension should be stripped.
32ded45b3caba42c8a2315a20bbfabb513fa54c1vboxsync /* handle separators. */
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync /* the end */
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync /* will never get here */
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync * Parses a path.
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync * It figures the length of the directory component, the offset of
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync * the file name and the location of the suffix dot.
9148e7ca5f9ee1d467112d347bfb2de2b97254d2vboxsync * @returns The path length.
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync * @param pszPath Path to find filename in.
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync * @param pcbDir Where to put the length of the directory component.
9148e7ca5f9ee1d467112d347bfb2de2b97254d2vboxsync * If no directory, this will be 0. Optional.
9148e7ca5f9ee1d467112d347bfb2de2b97254d2vboxsync * @param poffName Where to store the filename offset.
9148e7ca5f9ee1d467112d347bfb2de2b97254d2vboxsync * If empty string or if it's ending with a slash this
295a2ae5a4db2c930f7096c6cf55b5fa4ee5b018vboxsync * will be set to -1. Optional.
9148e7ca5f9ee1d467112d347bfb2de2b97254d2vboxsync * @param poffSuff Where to store the suffix offset (the last dot).
9148e7ca5f9ee1d467112d347bfb2de2b97254d2vboxsync * If empty string or if it's ending with a slash this
9148e7ca5f9ee1d467112d347bfb2de2b97254d2vboxsync * will be set to -1. Optional.
9148e7ca5f9ee1d467112d347bfb2de2b97254d2vboxsync * @param pfFlags Where to set flags returning more information about
9148e7ca5f9ee1d467112d347bfb2de2b97254d2vboxsync * the path. For the future. Optional.
9148e7ca5f9ee1d467112d347bfb2de2b97254d2vboxsyncRTDECL(size_t) RTPathParse(const char *pszPath, size_t *pcchDir, ssize_t *poffName, ssize_t *poffSuff)
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync /* handle separators. */
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync * The end. Complete the results.
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync ssize_t offName = *pszName != '\0' ? pszName - pszPath : -1;
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync while (off >= offRoot && RTPATH_IS_SLASH(pszPath[off]))
32ded45b3caba42c8a2315a20bbfabb513fa54c1vboxsync /* will never get here */
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync * Finds the filename in a path.
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync * @returns Pointer to filename within pszPath.
32ded45b3caba42c8a2315a20bbfabb513fa54c1vboxsync * @returns NULL if no filename (i.e. empty string or ends with a slash).
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync * @param pszPath Path to find filename in.
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync /* handle separators. */
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync /* the end */
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync return (char *)(void *)pszName;
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync /* will never get here */
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync * Strips the trailing slashes of a path name.
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync * @param pszPath Path to strip.
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsyncRTDECL(void) RTPathStripTrailingSlash(char *pszPath)
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync * Finds the extension part of in a path.
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync * @returns Pointer to extension within pszPath.
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync * @returns NULL if no extension.
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync * @param pszPath Path to find extension in.
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync /* handle separators. */
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync /* the end */
32ded45b3caba42c8a2315a20bbfabb513fa54c1vboxsync return (char *)(void *)pszExt;
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync /* will never get here */
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync * Checks if a path have an extension.
32ded45b3caba42c8a2315a20bbfabb513fa54c1vboxsync * @returns true if extension present.
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync * @returns false if no extension.
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync * @param pszPath Path to check.
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync * Checks if a path includes more than a filename.
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync * @returns true if path present.
9148e7ca5f9ee1d467112d347bfb2de2b97254d2vboxsync * @returns false if no path.
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync * @param pszPath Path to check.
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync * Helper for RTPathCompare() and RTPathStartsWith().
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync * @returns similar to strcmp.
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync * @param pszPath1 Path to compare.
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync * @param pszPath2 Path to compare.
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync * @param fLimit Limit the comparison to the length of \a pszPath2
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync * @internal
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsyncstatic int rtPathCompare(const char *pszPath1, const char *pszPath2, bool fLimit)
iDiff = 0;
if (!uc1)
puszTmpPath1++;
puszTmpPath2++;
return iDiff;
if (!fLimit)
return NULL;
return NULL;
/* This isn't quite right when looking at the above stuff, but it works assuming that '//' does not mean UNC. */
RTDECL(int) RTPathAbsEx(const char *pszBase, const char *pszPath, char *pszAbsPath, unsigned cchAbsPath)
#if defined(RT_OS_WINDOWS)
return VERR_INVALID_NAME;
return VERR_FILENAME_TOO_LONG;
if (fRootSpec)
return NULL;
#ifndef RT_MINI
return VINF_SUCCESS;
return VERR_BUFFER_OVERFLOW;
char *pszUtf8Path;
int rc;
return rc;
char *pszUtf8Path;
int rc;
return rc;
* a common global directory where ld.so can found them.
char *pszUtf8Path;
int rc;
return rc;
char *pszUtf8Path;
int rc;
return rc;