path.cpp revision a835c21f3b4c17e3141e7e3e3abf5aeb215357b1
32ded45b3caba42c8a2315a20bbfabb513fa54c1vboxsync/* $Id$ */
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync/** @file
32ded45b3caba42c8a2315a20bbfabb513fa54c1vboxsync * IPRT - Path Manipulation.
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync */
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync/*
e64031e20c39650a7bc902a3e1aba613b9415deevboxsync * Copyright (C) 2006-2007 Sun Microsystems, Inc.
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync *
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 *
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 *
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.
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync *
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 */
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync
9148e7ca5f9ee1d467112d347bfb2de2b97254d2vboxsync/*******************************************************************************
9148e7ca5f9ee1d467112d347bfb2de2b97254d2vboxsync* Header Files *
295a2ae5a4db2c930f7096c6cf55b5fa4ee5b018vboxsync*******************************************************************************/
9148e7ca5f9ee1d467112d347bfb2de2b97254d2vboxsync#include <iprt/path.h>
9148e7ca5f9ee1d467112d347bfb2de2b97254d2vboxsync#include <iprt/dir.h>
9148e7ca5f9ee1d467112d347bfb2de2b97254d2vboxsync#include <iprt/param.h>
9148e7ca5f9ee1d467112d347bfb2de2b97254d2vboxsync#include <iprt/string.h>
32ded45b3caba42c8a2315a20bbfabb513fa54c1vboxsync#include <iprt/assert.h>
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync#include <iprt/string.h>
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync#include <iprt/ctype.h>
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync#include <iprt/err.h>
32ded45b3caba42c8a2315a20bbfabb513fa54c1vboxsync#include <iprt/uni.h>
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync#include "internal/fs.h"
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync#include "internal/path.h"
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync#include "internal/process.h"
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync
9148e7ca5f9ee1d467112d347bfb2de2b97254d2vboxsync/**
9148e7ca5f9ee1d467112d347bfb2de2b97254d2vboxsync * Strips the filename from a path.
295a2ae5a4db2c930f7096c6cf55b5fa4ee5b018vboxsync *
9148e7ca5f9ee1d467112d347bfb2de2b97254d2vboxsync * @param pszPath Path which filename should be extracted from.
9148e7ca5f9ee1d467112d347bfb2de2b97254d2vboxsync *
32ded45b3caba42c8a2315a20bbfabb513fa54c1vboxsync */
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsyncRTDECL(void) RTPathStripFilename(char *pszPath)
32ded45b3caba42c8a2315a20bbfabb513fa54c1vboxsync{
32ded45b3caba42c8a2315a20bbfabb513fa54c1vboxsync char *psz = pszPath;
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync char *pszLastSep = pszPath;
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync
9148e7ca5f9ee1d467112d347bfb2de2b97254d2vboxsync for (;; psz++)
9148e7ca5f9ee1d467112d347bfb2de2b97254d2vboxsync {
9148e7ca5f9ee1d467112d347bfb2de2b97254d2vboxsync switch (*psz)
295a2ae5a4db2c930f7096c6cf55b5fa4ee5b018vboxsync {
9148e7ca5f9ee1d467112d347bfb2de2b97254d2vboxsync /* handle separators. */
9148e7ca5f9ee1d467112d347bfb2de2b97254d2vboxsync#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
9148e7ca5f9ee1d467112d347bfb2de2b97254d2vboxsync case ':':
be641d8f2dc377d6a4403f3822d8bed08f51e28avboxsync pszLastSep = psz + 1;
be641d8f2dc377d6a4403f3822d8bed08f51e28avboxsync break;
9148e7ca5f9ee1d467112d347bfb2de2b97254d2vboxsync
295a2ae5a4db2c930f7096c6cf55b5fa4ee5b018vboxsync case '\\':
9148e7ca5f9ee1d467112d347bfb2de2b97254d2vboxsync#endif
295a2ae5a4db2c930f7096c6cf55b5fa4ee5b018vboxsync case '/':
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync pszLastSep = psz;
9148e7ca5f9ee1d467112d347bfb2de2b97254d2vboxsync break;
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync /* the end */
295a2ae5a4db2c930f7096c6cf55b5fa4ee5b018vboxsync case '\0':
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync if (pszLastSep == pszPath)
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync *pszLastSep++ = '.';
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync *pszLastSep = '\0';
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync return;
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync }
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync }
be641d8f2dc377d6a4403f3822d8bed08f51e28avboxsync /* will never get here */
295a2ae5a4db2c930f7096c6cf55b5fa4ee5b018vboxsync}
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync
be641d8f2dc377d6a4403f3822d8bed08f51e28avboxsync
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync/**
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync * Strips the extension from a path.
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync *
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync * @param pszPath Path which extension should be stripped.
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync */
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsyncRTDECL(void) RTPathStripExt(char *pszPath)
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync{
9148e7ca5f9ee1d467112d347bfb2de2b97254d2vboxsync char *pszDot = NULL;
295a2ae5a4db2c930f7096c6cf55b5fa4ee5b018vboxsync for (;; pszPath++)
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync {
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync switch (*pszPath)
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync {
32ded45b3caba42c8a2315a20bbfabb513fa54c1vboxsync /* handle separators. */
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync case ':':
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync case '\\':
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync#endif
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync case '/':
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync pszDot = NULL;
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync break;
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync case '.':
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync pszDot = pszPath;
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync break;
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync /* the end */
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync case '\0':
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync if (pszDot)
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync *pszDot = '\0';
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync return;
32ded45b3caba42c8a2315a20bbfabb513fa54c1vboxsync }
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync }
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync /* will never get here */
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync}
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync/**
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync * Parses a path.
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync *
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync * It figures the length of the directory component, the offset of
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync * the file name and the location of the suffix dot.
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync *
9148e7ca5f9ee1d467112d347bfb2de2b97254d2vboxsync * @returns The path length.
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync *
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.
9148e7ca5f9ee1d467112d347bfb2de2b97254d2vboxsync */
9148e7ca5f9ee1d467112d347bfb2de2b97254d2vboxsyncRTDECL(size_t) RTPathParse(const char *pszPath, size_t *pcchDir, ssize_t *poffName, ssize_t *poffSuff)
9148e7ca5f9ee1d467112d347bfb2de2b97254d2vboxsync{
32ded45b3caba42c8a2315a20bbfabb513fa54c1vboxsync const char *psz = pszPath;
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync ssize_t offRoot = 0;
9148e7ca5f9ee1d467112d347bfb2de2b97254d2vboxsync const char *pszName = pszPath;
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync const char *pszLastDot = NULL;
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync for (;; psz++)
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync {
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync switch (*psz)
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync {
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync /* handle separators. */
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync case ':':
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync pszName = psz + 1;
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync offRoot = pszName - psz;
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync break;
9148e7ca5f9ee1d467112d347bfb2de2b97254d2vboxsync
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync case '\\':
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync#endif
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync case '/':
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync pszName = psz + 1;
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync break;
32ded45b3caba42c8a2315a20bbfabb513fa54c1vboxsync
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync case '.':
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync pszLastDot = psz;
32ded45b3caba42c8a2315a20bbfabb513fa54c1vboxsync break;
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync /*
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync * The end. Complete the results.
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync */
32ded45b3caba42c8a2315a20bbfabb513fa54c1vboxsync case '\0':
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync {
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync ssize_t offName = *pszName != '\0' ? pszName - pszPath : -1;
32ded45b3caba42c8a2315a20bbfabb513fa54c1vboxsync if (poffName)
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync *poffName = offName;
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync
9148e7ca5f9ee1d467112d347bfb2de2b97254d2vboxsync if (poffSuff)
32ded45b3caba42c8a2315a20bbfabb513fa54c1vboxsync {
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync ssize_t offSuff = -1;
32ded45b3caba42c8a2315a20bbfabb513fa54c1vboxsync if (pszLastDot)
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync {
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync offSuff = pszLastDot - pszPath;
32ded45b3caba42c8a2315a20bbfabb513fa54c1vboxsync if (offSuff <= offName)
32ded45b3caba42c8a2315a20bbfabb513fa54c1vboxsync offSuff = -1;
32ded45b3caba42c8a2315a20bbfabb513fa54c1vboxsync }
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync *poffSuff = offSuff;
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync }
32ded45b3caba42c8a2315a20bbfabb513fa54c1vboxsync
0ff7152ccdcc57edca12c5f17b9699c66eeff975vboxsync if (pcchDir)
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync {
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync ssize_t off = offName - 1;
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync while (off >= offRoot && RTPATH_IS_SLASH(pszPath[off]))
32ded45b3caba42c8a2315a20bbfabb513fa54c1vboxsync off--;
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync *pcchDir = RT_MAX(off, offRoot) + 1;
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync }
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync return psz - pszPath;
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync }
32ded45b3caba42c8a2315a20bbfabb513fa54c1vboxsync }
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync }
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync
32ded45b3caba42c8a2315a20bbfabb513fa54c1vboxsync /* will never get here */
0ff7152ccdcc57edca12c5f17b9699c66eeff975vboxsync return 0;
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync}
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync
9148e7ca5f9ee1d467112d347bfb2de2b97254d2vboxsync/**
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync * Finds the filename in a path.
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync *
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.
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync */
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsyncRTDECL(char *) RTPathFilename(const char *pszPath)
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync{
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync const char *psz = pszPath;
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync const char *pszName = pszPath;
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync for (;; psz++)
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync {
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync switch (*psz)
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync {
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync /* handle separators. */
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync case ':':
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync pszName = psz + 1;
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync break;
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync case '\\':
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync#endif
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync case '/':
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync pszName = psz + 1;
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync break;
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync /* the end */
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync case '\0':
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync if (*pszName)
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync return (char *)(void *)pszName;
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync return NULL;
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync }
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync }
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync /* will never get here */
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync return NULL;
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync}
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync/**
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync * Strips the trailing slashes of a path name.
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync *
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync * @param pszPath Path to strip.
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync */
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsyncRTDECL(void) RTPathStripTrailingSlash(char *pszPath)
9148e7ca5f9ee1d467112d347bfb2de2b97254d2vboxsync{
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync char *pszEnd = strchr(pszPath, '\0');
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync while (pszEnd-- > pszPath)
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync {
9148e7ca5f9ee1d467112d347bfb2de2b97254d2vboxsync switch (*pszEnd)
9148e7ca5f9ee1d467112d347bfb2de2b97254d2vboxsync {
9148e7ca5f9ee1d467112d347bfb2de2b97254d2vboxsync case '/':
295a2ae5a4db2c930f7096c6cf55b5fa4ee5b018vboxsync#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
9148e7ca5f9ee1d467112d347bfb2de2b97254d2vboxsync case '\\':
9148e7ca5f9ee1d467112d347bfb2de2b97254d2vboxsync#endif
9148e7ca5f9ee1d467112d347bfb2de2b97254d2vboxsync *pszEnd = '\0';
9148e7ca5f9ee1d467112d347bfb2de2b97254d2vboxsync break;
9148e7ca5f9ee1d467112d347bfb2de2b97254d2vboxsync default:
9148e7ca5f9ee1d467112d347bfb2de2b97254d2vboxsync return;
9148e7ca5f9ee1d467112d347bfb2de2b97254d2vboxsync }
32ded45b3caba42c8a2315a20bbfabb513fa54c1vboxsync }
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync return;
32ded45b3caba42c8a2315a20bbfabb513fa54c1vboxsync}
32ded45b3caba42c8a2315a20bbfabb513fa54c1vboxsync
9148e7ca5f9ee1d467112d347bfb2de2b97254d2vboxsync
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync/**
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync * Finds the extension part of in a path.
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync *
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync * @returns Pointer to extension within pszPath.
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync * @returns NULL if no extension.
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync * @param pszPath Path to find extension in.
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync */
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsyncRTDECL(char *) RTPathExt(const char *pszPath)
32ded45b3caba42c8a2315a20bbfabb513fa54c1vboxsync{
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync const char *psz = pszPath;
32ded45b3caba42c8a2315a20bbfabb513fa54c1vboxsync const char *pszExt = NULL;
32ded45b3caba42c8a2315a20bbfabb513fa54c1vboxsync
32ded45b3caba42c8a2315a20bbfabb513fa54c1vboxsync for (;; psz++)
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync {
32ded45b3caba42c8a2315a20bbfabb513fa54c1vboxsync switch (*psz)
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync {
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync /* handle separators. */
0ff7152ccdcc57edca12c5f17b9699c66eeff975vboxsync#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync case ':':
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync pszExt = NULL;
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync break;
6a30d0e9d748ecb8a0c62b4233a97fed255a8a4cvboxsync
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync case '\\':
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync#endif
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync case '/':
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync pszExt = NULL;
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync break;
32ded45b3caba42c8a2315a20bbfabb513fa54c1vboxsync case '.':
32ded45b3caba42c8a2315a20bbfabb513fa54c1vboxsync pszExt = psz;
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync break;
32ded45b3caba42c8a2315a20bbfabb513fa54c1vboxsync
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync /* the end */
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync case '\0':
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync if (pszExt)
32ded45b3caba42c8a2315a20bbfabb513fa54c1vboxsync return (char *)(void *)pszExt;
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync return NULL;
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync }
0ff7152ccdcc57edca12c5f17b9699c66eeff975vboxsync }
32ded45b3caba42c8a2315a20bbfabb513fa54c1vboxsync
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync /* will never get here */
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync return NULL;
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync}
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync/**
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync * Checks if a path have an extension.
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync *
32ded45b3caba42c8a2315a20bbfabb513fa54c1vboxsync * @returns true if extension present.
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync * @returns false if no extension.
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync * @param pszPath Path to check.
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync */
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsyncRTDECL(bool) RTPathHaveExt(const char *pszPath)
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync{
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync return RTPathExt(pszPath) != NULL;
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync}
32ded45b3caba42c8a2315a20bbfabb513fa54c1vboxsync
32ded45b3caba42c8a2315a20bbfabb513fa54c1vboxsync
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync/**
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync * Checks if a path includes more than a filename.
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync *
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync * @returns true if path present.
9148e7ca5f9ee1d467112d347bfb2de2b97254d2vboxsync * @returns false if no path.
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync * @param pszPath Path to check.
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync */
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsyncRTDECL(bool) RTPathHavePath(const char *pszPath)
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync{
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync return strpbrk(pszPath, "/\\:") != NULL;
32ded45b3caba42c8a2315a20bbfabb513fa54c1vboxsync#else
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync return strpbrk(pszPath, "/") != NULL;
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync#endif
32ded45b3caba42c8a2315a20bbfabb513fa54c1vboxsync}
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync/**
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync * Helper for RTPathCompare() and RTPathStartsWith().
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync *
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
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync */
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsyncstatic int rtPathCompare(const char *pszPath1, const char *pszPath2, bool fLimit)
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync{
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync if (pszPath1 == pszPath2)
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync return 0;
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync if (!pszPath1)
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync return -1;
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync if (!pszPath2)
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync return 1;
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync PRTUNICP puszPath1;
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync int rc = RTStrToUni(pszPath1, &puszPath1);
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync if (RT_FAILURE(rc))
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync return -1;
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync PRTUNICP puszPath2;
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync rc = RTStrToUni(pszPath2, &puszPath2);
e6317c5fa1f3ac24b7d86f42d2b11bcb0733609evboxsync if (RT_FAILURE(rc))
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync {
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync RTUniFree(puszPath1);
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync return 1;
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync }
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync
dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2evboxsync int iDiff = 0;
7d9e0d289e799c0ee5b9307fe7358ddc53bd34ddvboxsync PRTUNICP puszTmpPath1 = puszPath1;
PRTUNICP puszTmpPath2 = puszPath2;
for (;;)
{
register RTUNICP uc1 = *puszTmpPath1;
register RTUNICP uc2 = *puszTmpPath2;
if (uc1 != uc2)
{
if (uc1 == '\\')
uc1 = '/';
else
uc1 = RTUniCpToUpper(uc1);
if (uc2 == '\\')
uc2 = '/';
else
uc2 = RTUniCpToUpper(uc2);
if (uc1 != uc2)
{
iDiff = uc1 > uc2 ? 1 : -1; /* (overflow/underflow paranoia) */
if (fLimit && uc2 == '\0')
iDiff = 0;
break;
}
}
if (!uc1)
break;
puszTmpPath1++;
puszTmpPath2++;
}
RTUniFree(puszPath2);
RTUniFree(puszPath1);
return iDiff;
#else
if (!fLimit)
return strcmp(pszPath1, pszPath2);
return strncmp(pszPath1, pszPath2, strlen(pszPath2));
#endif
}
/**
* Compares two paths.
*
* The comparison takes platform-dependent details into account,
* such as:
* <ul>
* <li>On DOS-like platforms, both |\| and |/| separator chars are considered
* to be equal.
* <li>On platforms with case-insensitive file systems, mismatching characters
* are uppercased and compared again.
* </ul>
*
* @remark
*
* File system details are currently ignored. This means that you won't get
* case-insentive compares on unix systems when a path goes into a case-insensitive
* filesystem like FAT, HPFS, HFS, NTFS, JFS, or similar. For NT, OS/2 and similar
* you'll won't get case-sensitve compares on a case-sensitive file system.
*
* @param pszPath1 Path to compare (must be an absolute path).
* @param pszPath2 Path to compare (must be an absolute path).
*
* @returns @< 0 if the first path less than the second path.
* @returns 0 if the first path identical to the second path.
* @returns @> 0 if the first path greater than the second path.
*/
RTDECL(int) RTPathCompare(const char *pszPath1, const char *pszPath2)
{
return rtPathCompare(pszPath1, pszPath2, false /* full path lengths */);
}
/**
* Checks if a path starts with the given parent path.
*
* This means that either the path and the parent path matches completely, or that
* the path is to some file or directory residing in the tree given by the parent
* directory.
*
* The path comparison takes platform-dependent details into account,
* see RTPathCompare() for details.
*
* @param pszPath Path to check, must be an absolute path.
* @param pszParentPath Parent path, must be an absolute path.
* No trailing directory slash!
*
* @returns |true| when \a pszPath starts with \a pszParentPath (or when they
* are identical), or |false| otherwise.
*
* @remark This API doesn't currently handle root directory compares in a manner
* consistant with the other APIs. RTPathStartsWith(pszSomePath, "/") will
* not work if pszSomePath isn't "/".
*/
RTDECL(bool) RTPathStartsWith(const char *pszPath, const char *pszParentPath)
{
if (pszPath == pszParentPath)
return true;
if (!pszPath || !pszParentPath)
return false;
if (rtPathCompare(pszPath, pszParentPath, true /* limited by path 2 */) != 0)
return false;
const size_t cchParentPath = strlen(pszParentPath);
return RTPATH_IS_SLASH(pszPath[cchParentPath])
|| pszPath[cchParentPath] == '\0';
}
/**
* Same as RTPathReal only the result is RTStrDup()'ed.
*
* @returns Pointer to real path. Use RTStrFree() to free this string.
* @returns NULL if RTPathReal() or RTStrDup() fails.
* @param pszPath
*/
RTDECL(char *) RTPathRealDup(const char *pszPath)
{
char szPath[RTPATH_MAX];
int rc = RTPathReal(pszPath, szPath, sizeof(szPath));
if (RT_SUCCESS(rc))
return RTStrDup(szPath);
return NULL;
}
/**
* Same as RTPathAbs only the result is RTStrDup()'ed.
*
* @returns Pointer to real path. Use RTStrFree() to free this string.
* @returns NULL if RTPathAbs() or RTStrDup() fails.
* @param pszPath The path to resolve.
*/
RTDECL(char *) RTPathAbsDup(const char *pszPath)
{
char szPath[RTPATH_MAX];
int rc = RTPathAbs(pszPath, szPath, sizeof(szPath));
if (RT_SUCCESS(rc))
return RTStrDup(szPath);
return NULL;
}
/**
* Returns the length of the volume name specifier of the given path.
* If no such specifier zero is returned.
*/
size_t rtPathVolumeSpecLen(const char *pszPath)
{
#if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
if (pszPath && *pszPath)
{
/* UTC path. */
if ( (pszPath[0] == '\\' || pszPath[0] == '/')
&& (pszPath[1] == '\\' || pszPath[1] == '/'))
return strcspn(pszPath + 2, "\\/") + 2;
/* Drive letter. */
if ( pszPath[1] == ':'
&& toupper(pszPath[0]) >= 'A' && toupper(pszPath[0]) <= 'Z')
return 2;
}
return 0;
#else
/* This isn't quite right when looking at the above stuff, but it works assuming that '//' does not mean UNC. */
/// @todo (dmik) well, it's better to consider there's no volume name
// at all on *nix systems
return 0;
// return pszPath && pszPath[0] == '/';
#endif
}
/**
* Get the absolute path (no symlinks, no . or .. components), assuming the
* given base path as the current directory. The resulting path doesn't have
* to exist.
*
* @returns iprt status code.
* @param pszBase The base path to act like a current directory.
* When NULL, the actual cwd is used (i.e. the call
* is equivalent to RTPathAbs(pszPath, ...).
* @param pszPath The path to resolve.
* @param pszAbsPath Where to store the absolute path.
* @param cchAbsPath Size of the buffer.
*/
RTDECL(int) RTPathAbsEx(const char *pszBase, const char *pszPath, char *pszAbsPath, unsigned cchAbsPath)
{
if (pszBase && pszPath && !rtPathVolumeSpecLen(pszPath))
{
#if defined(RT_OS_WINDOWS)
/* The format for very long paths is not supported. */
if ( (pszBase[0] == '/' || pszBase[0] == '\\')
&& (pszBase[1] == '/' || pszBase[1] == '\\')
&& pszBase[2] == '?'
&& (pszBase[3] == '/' || pszBase[3] == '\\'))
return VERR_INVALID_NAME;
#endif
/** @todo there are a couple of things which isn't 100% correct, although the
* current code will have to work for now - I don't have time to fix it right now.
*
* 1) On Windows & OS/2 we confuse '/' with an abspath spec and will
* not necessarily resolve it on the right drive.
* 2) A trailing slash in the base might cause UNC names to be created.
* 3) The lengths total doesn't have to be less than max length
* if the pszPath starts with a slash.
*/
size_t cchBase = strlen(pszBase);
size_t cchPath = strlen(pszPath);
if (cchBase + cchPath >= RTPATH_MAX)
return VERR_FILENAME_TOO_LONG;
bool fRootSpec = pszPath[0] == '/'
#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
|| pszPath[0] == '\\'
#endif
;
size_t cchVolSpec = rtPathVolumeSpecLen(pszBase);
char szPath[RTPATH_MAX];
if (fRootSpec)
{
/* join the disk name from base and the path */
memcpy(szPath, pszBase, cchVolSpec);
strcpy(&szPath[cchVolSpec], pszPath);
}
else
{
/* join the base path and the path */
strcpy(szPath, pszBase);
szPath[cchBase] = RTPATH_DELIMITER;
strcpy(&szPath[cchBase + 1], pszPath);
}
return RTPathAbs(szPath, pszAbsPath, cchAbsPath);
}
/* Fallback to the non *Ex version */
return RTPathAbs(pszPath, pszAbsPath, cchAbsPath);
}
/**
* Same as RTPathAbsEx only the result is RTStrDup()'ed.
*
* @returns Pointer to the absolute path. Use RTStrFree() to free this string.
* @returns NULL if RTPathAbsEx() or RTStrDup() fails.
* @param pszBase The base path to act like a current directory.
* When NULL, the actual cwd is used (i.e. the call
* is equivalent to RTPathAbs(pszPath, ...).
* @param pszPath The path to resolve.
*/
RTDECL(char *) RTPathAbsExDup(const char *pszBase, const char *pszPath)
{
char szPath[RTPATH_MAX];
int rc = RTPathAbsEx(pszBase, pszPath, szPath, sizeof(szPath));
if (RT_SUCCESS(rc))
return RTStrDup(szPath);
return NULL;
}
#ifndef RT_MINI
RTDECL(int) RTPathProgram(char *pszPath, size_t cchPath)
{
AssertReturn(g_szrtProcExePath[0], VERR_WRONG_ORDER);
/*
* Calc the length and check if there is space before copying.
*/
size_t cch = g_cchrtProcDir;
if (cch <= cchPath)
{
memcpy(pszPath, g_szrtProcExePath, cch);
pszPath[cch] = '\0';
return VINF_SUCCESS;
}
AssertMsgFailed(("Buffer too small (%zu <= %zu)\n", cchPath, cch));
return VERR_BUFFER_OVERFLOW;
}
/**
* Gets the directory for architecture-independent application data, for
* example NLS files, module sources, ...
*
* Linux: /usr/shared/@<application@>
* Windows: @<program files directory@>/@<application@>
* Old path: same as RTPathProgram()
*
*/
RTDECL(int) RTPathAppPrivateNoArch(char *pszPath, size_t cchPath)
{
#if !defined(RT_OS_WINDOWS) && defined(RTPATH_APP_PRIVATE)
char *pszUtf8Path;
int rc;
rc = rtPathFromNative(&pszUtf8Path, RTPATH_APP_PRIVATE);
if (RT_SUCCESS(rc))
{
size_t cchPathPrivateNoArch = strlen(pszUtf8Path);
if (cchPathPrivateNoArch < cchPath)
memcpy(pszPath, pszUtf8Path, cchPathPrivateNoArch + 1);
else
rc = VERR_BUFFER_OVERFLOW;
RTStrFree(pszUtf8Path);
}
return rc;
#else
return RTPathProgram(pszPath, cchPath);
#endif
}
/**
* Gets the directory for architecture-dependent application data, for
* example modules which can be loaded at runtime.
*
* Linux: /usr/lib/@<application@>
* Windows: @<program files directory@>/@<application@>
* Old path: same as RTPathProgram()
*
* @returns iprt status code.
* @param pszPath Buffer where to store the path.
* @param cchPath Buffer size in bytes.
*/
RTDECL(int) RTPathAppPrivateArch(char *pszPath, size_t cchPath)
{
#if !defined(RT_OS_WINDOWS) && defined(RTPATH_APP_PRIVATE_ARCH)
char *pszUtf8Path;
int rc;
rc = rtPathFromNative(&pszUtf8Path, RTPATH_APP_PRIVATE_ARCH);
if (RT_SUCCESS(rc))
{
size_t cchPathPrivateArch = strlen(pszUtf8Path);
if (cchPathPrivateArch < cchPath)
memcpy(pszPath, pszUtf8Path, cchPathPrivateArch + 1);
else
rc = VERR_BUFFER_OVERFLOW;
RTStrFree(pszUtf8Path);
}
return rc;
#else
return RTPathProgram(pszPath, cchPath);
#endif
}
/**
* Gets the directory of shared libraries. This is not the same as
* RTPathAppPrivateArch() as Linux depends all shared libraries in
* a common global directory where ld.so can found them.
*
* Linux: /usr/lib
* Windows: @<program files directory@>/@<application@>
* Old path: same as RTPathProgram()
*
* @returns iprt status code.
* @param pszPath Buffer where to store the path.
* @param cchPath Buffer size in bytes.
*/
RTDECL(int) RTPathSharedLibs(char *pszPath, size_t cchPath)
{
#if !defined(RT_OS_WINDOWS) && defined(RTPATH_SHARED_LIBS)
char *pszUtf8Path;
int rc;
rc = rtPathFromNative(&pszUtf8Path, RTPATH_SHARED_LIBS);
if (RT_SUCCESS(rc))
{
size_t cchPathSharedLibs = strlen(pszUtf8Path);
if (cchPathSharedLibs < cchPath)
memcpy(pszPath, pszUtf8Path, cchPathSharedLibs + 1);
else
rc = VERR_BUFFER_OVERFLOW;
RTStrFree(pszUtf8Path);
}
return rc;
#else
return RTPathProgram(pszPath, cchPath);
#endif
}
/**
* Gets the directory for documentation.
*
* Linux: /usr/share/doc/@<application@>
* Windows: @<program files directory@>/@<application@>
* Old path: same as RTPathProgram()
*
* @returns iprt status code.
* @param pszPath Buffer where to store the path.
* @param cchPath Buffer size in bytes.
*/
RTDECL(int) RTPathAppDocs(char *pszPath, size_t cchPath)
{
#if !defined(RT_OS_WINDOWS) && defined(RTPATH_APP_DOCS)
char *pszUtf8Path;
int rc;
rc = rtPathFromNative(&pszUtf8Path, RTPATH_APP_DOCS);
if (RT_SUCCESS(rc))
{
size_t cchPathAppDocs = strlen(pszUtf8Path);
if (cchPathAppDocs < cchPath)
memcpy(pszPath, pszUtf8Path, cchPathAppDocs + 1);
else
rc = VERR_BUFFER_OVERFLOW;
RTStrFree(pszUtf8Path);
}
return rc;
#else
return RTPathProgram(pszPath, cchPath);
#endif
}
#endif /* !RT_MINI */