path.cpp revision 207e97003c797079efc85d3df4dfe832d5919860
5b281ba489ca18f0380d7efc7a5108b606cce449vboxsync * IPRT - Path Manipulation.
c58f1213e628a545081c70e26c6b67a841cff880vboxsync * Copyright (C) 2006-2007 Oracle Corporation
804197ca1d56719343cd21af782ddc2c695a6760vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
804197ca1d56719343cd21af782ddc2c695a6760vboxsync * available from http://www.virtualbox.org. This file is free software;
804197ca1d56719343cd21af782ddc2c695a6760vboxsync * you can redistribute it and/or modify it under the terms of the GNU
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * General Public License (GPL) as published by the Free Software
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * The contents of this file may alternatively be used under the terms
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * of the Common Development and Distribution License Version 1.0
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * VirtualBox OSE distribution, in which case the provisions of the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * CDDL are applicable instead of those of the GPL.
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * You may elect to license modified versions of this file under the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * terms and conditions of either the GPL or the CDDL or both.
65a755c25b49d32c914e2d569fcdfb766e1ee879vboxsync/*******************************************************************************
65a755c25b49d32c914e2d569fcdfb766e1ee879vboxsync* Header Files *
65a755c25b49d32c914e2d569fcdfb766e1ee879vboxsync*******************************************************************************/
65a755c25b49d32c914e2d569fcdfb766e1ee879vboxsyncRTDECL(int) RTPathExecDir(char *pszPath, size_t cchPath)
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync AssertReturn(g_szrtProcExePath[0], VERR_WRONG_ORDER);
46fee3dbfe26f500824ef463ac239b6cb5d09522vboxsync * Calc the length and check if there is space before copying.
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync AssertMsgFailed(("Buffer too small (%zu <= %zu)\n", cchPath, cch));
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync * Gets the directory for architecture-independent application data, for
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync * example NLS files, module sources, ...
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync * Linux: /usr/shared/@<application@>
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync * Windows: @<program files directory@>/@<application@>
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync * Old path: same as RTPathExecDir()
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsyncRTDECL(int) RTPathAppPrivateNoArch(char *pszPath, size_t cchPath)
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync#if !defined(RT_OS_WINDOWS) && defined(RTPATH_APP_PRIVATE)
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync return RTStrCopy(pszPath, cchPath, RTPATH_APP_PRIVATE);
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync * Gets the directory for architecture-dependent application data, for
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync * example modules which can be loaded at runtime.
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync * Linux: /usr/lib/@<application@>
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync * Windows: @<program files directory@>/@<application@>
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync * Old path: same as RTPathExecDir()
65a755c25b49d32c914e2d569fcdfb766e1ee879vboxsync * @returns iprt status code.
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync * @param pszPath Buffer where to store the path.
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync * @param cchPath Buffer size in bytes.
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsyncRTDECL(int) RTPathAppPrivateArch(char *pszPath, size_t cchPath)
db55f7b1060a6a72704b5369a8e776c59e5e4f64vboxsync#if !defined(RT_OS_WINDOWS) && defined(RTPATH_APP_PRIVATE_ARCH)
db55f7b1060a6a72704b5369a8e776c59e5e4f64vboxsync return RTStrCopy(pszPath, cchPath, RTPATH_APP_PRIVATE_ARCH);
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync * Gets the directory of shared libraries. This is not the same as
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync * RTPathAppPrivateArch() as Linux depends all shared libraries in
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync * a common global directory where ld.so can found them.
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync * Windows: @<program files directory@>/@<application@>
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync * Old path: same as RTPathExecDir()
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync * @returns iprt status code.
db55f7b1060a6a72704b5369a8e776c59e5e4f64vboxsync * @param pszPath Buffer where to store the path.
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync * @param cchPath Buffer size in bytes.
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsyncRTDECL(int) RTPathSharedLibs(char *pszPath, size_t cchPath)
65a755c25b49d32c914e2d569fcdfb766e1ee879vboxsync#if !defined(RT_OS_WINDOWS) && defined(RTPATH_SHARED_LIBS)
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync return RTStrCopy(pszPath, cchPath, RTPATH_SHARED_LIBS);
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync * Gets the directory for documentation.
db55f7b1060a6a72704b5369a8e776c59e5e4f64vboxsync * Linux: /usr/share/doc/@<application@>
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync * Windows: @<program files directory@>/@<application@>
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync * Old path: same as RTPathExecDir()
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync * @returns iprt status code.
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync * @param pszPath Buffer where to store the path.
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync * @param cchPath Buffer size in bytes.
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsyncRTDECL(int) RTPathAppDocs(char *pszPath, size_t cchPath)
65a755c25b49d32c914e2d569fcdfb766e1ee879vboxsync#if !defined(RT_OS_WINDOWS) && defined(RTPATH_APP_DOCS)
65a755c25b49d32c914e2d569fcdfb766e1ee879vboxsync return RTStrCopy(pszPath, cchPath, RTPATH_APP_DOCS);
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync * Gets the temporary directory path.
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync * @returns iprt status code.
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync * @param pszPath Buffer where to store the path.
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync * @param cchPath Buffer size in bytes.
65a755c25b49d32c914e2d569fcdfb766e1ee879vboxsyncRTDECL(int) RTPathTemp(char *pszPath, size_t cchPath)
65a755c25b49d32c914e2d569fcdfb766e1ee879vboxsync * Try get it from the environment first.
db55f7b1060a6a72704b5369a8e776c59e5e4f64vboxsync static const char * const s_apszVars[] =
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync "IPRT_TMPDIR"
aa4bcf0a4b2db3ac352b56a291d49cb8d4b66d32vboxsync for (size_t iVar = 0; iVar < RT_ELEMENTS(s_apszVars); iVar++)
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync int rc = RTEnvGetEx(RTENV_DEFAULT, s_apszVars[iVar], pszPath, cchPath, NULL);
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync * Here we should use some sane system default, instead we just use
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync * the typical unix temp dir for now.
65a755c25b49d32c914e2d569fcdfb766e1ee879vboxsync /** @todo Windows should default to the windows directory, see GetTempPath.
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync * Some unixes has path.h and _PATH_TMP. There is also a question about
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync * whether /var/tmp wouldn't be a better place... */
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsyncRTR3DECL(int) RTPathGetMode(const char *pszPath, PRTFMODE pfMode)
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync int rc = RTPathReal(pszPath, szPathReal, sizeof(szPathReal));