VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/path.cpp@ 34079

Last change on this file since 34079 was 33454, checked in by vboxsync, 14 years ago

IPRT: off-by-one

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.8 KB
Line 
1/* $Id: path.cpp 33454 2010-10-26 09:51:53Z vboxsync $ */
2/** @file
3 * IPRT - Path Manipulation.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*******************************************************************************
29* Header Files *
30*******************************************************************************/
31#include "internal/iprt.h"
32#include <iprt/assert.h>
33#include <iprt/env.h>
34#include <iprt/err.h>
35#include <iprt/path.h>
36#include <iprt/string.h>
37#include "internal/path.h"
38#include "internal/process.h"
39
40
41RTDECL(int) RTPathExecDir(char *pszPath, size_t cchPath)
42{
43 AssertReturn(g_szrtProcExePath[0], VERR_WRONG_ORDER);
44
45 /*
46 * Calc the length and check if there is space before copying.
47 */
48 size_t cch = g_cchrtProcDir;
49 if (cch < cchPath)
50 {
51 memcpy(pszPath, g_szrtProcExePath, cch);
52 pszPath[cch] = '\0';
53 return VINF_SUCCESS;
54 }
55
56 AssertMsgFailed(("Buffer too small (%zu <= %zu)\n", cchPath, cch));
57 return VERR_BUFFER_OVERFLOW;
58}
59
60
61/**
62 * Gets the directory for architecture-independent application data, for
63 * example NLS files, module sources, ...
64 *
65 * Linux: /usr/shared/@<application@>
66 * Windows: @<program files directory@>/@<application@>
67 * Old path: same as RTPathExecDir()
68 *
69 */
70RTDECL(int) RTPathAppPrivateNoArch(char *pszPath, size_t cchPath)
71{
72#if !defined(RT_OS_WINDOWS) && defined(RTPATH_APP_PRIVATE)
73 return RTStrCopy(pszPath, cchPath, RTPATH_APP_PRIVATE);
74#else
75 return RTPathExecDir(pszPath, cchPath);
76#endif
77}
78
79
80/**
81 * Gets the directory for architecture-dependent application data, for
82 * example modules which can be loaded at runtime.
83 *
84 * Linux: /usr/lib/@<application@>
85 * Windows: @<program files directory@>/@<application@>
86 * Old path: same as RTPathExecDir()
87 *
88 * @returns iprt status code.
89 * @param pszPath Buffer where to store the path.
90 * @param cchPath Buffer size in bytes.
91 */
92RTDECL(int) RTPathAppPrivateArch(char *pszPath, size_t cchPath)
93{
94#if !defined(RT_OS_WINDOWS) && defined(RTPATH_APP_PRIVATE_ARCH)
95 return RTStrCopy(pszPath, cchPath, RTPATH_APP_PRIVATE_ARCH);
96#else
97 return RTPathExecDir(pszPath, cchPath);
98#endif
99}
100
101
102/**
103 * Gets the directory of shared libraries. This is not the same as
104 * RTPathAppPrivateArch() as Linux depends all shared libraries in
105 * a common global directory where ld.so can found them.
106 *
107 * Linux: /usr/lib
108 * Windows: @<program files directory@>/@<application@>
109 * Old path: same as RTPathExecDir()
110 *
111 * @returns iprt status code.
112 * @param pszPath Buffer where to store the path.
113 * @param cchPath Buffer size in bytes.
114 */
115RTDECL(int) RTPathSharedLibs(char *pszPath, size_t cchPath)
116{
117#if !defined(RT_OS_WINDOWS) && defined(RTPATH_SHARED_LIBS)
118 return RTStrCopy(pszPath, cchPath, RTPATH_SHARED_LIBS);
119#else
120 return RTPathExecDir(pszPath, cchPath);
121#endif
122}
123
124
125/**
126 * Gets the directory for documentation.
127 *
128 * Linux: /usr/share/doc/@<application@>
129 * Windows: @<program files directory@>/@<application@>
130 * Old path: same as RTPathExecDir()
131 *
132 * @returns iprt status code.
133 * @param pszPath Buffer where to store the path.
134 * @param cchPath Buffer size in bytes.
135 */
136RTDECL(int) RTPathAppDocs(char *pszPath, size_t cchPath)
137{
138#if !defined(RT_OS_WINDOWS) && defined(RTPATH_APP_DOCS)
139 return RTStrCopy(pszPath, cchPath, RTPATH_APP_DOCS);
140#else
141 return RTPathExecDir(pszPath, cchPath);
142#endif
143}
144
145
146/**
147 * Gets the temporary directory path.
148 *
149 * @returns iprt status code.
150 *
151 * @param pszPath Buffer where to store the path.
152 * @param cchPath Buffer size in bytes.
153 */
154RTDECL(int) RTPathTemp(char *pszPath, size_t cchPath)
155{
156 /*
157 * Try get it from the environment first.
158 */
159 static const char * const s_apszVars[] =
160 {
161 "IPRT_TMPDIR"
162#if defined(RT_OS_WINDOWS)
163 , "TMP", "TEMP", "USERPROFILE"
164#elif defined(RT_OS_OS2)
165 , "TMP", "TEMP", "TMPDIR"
166#else
167 , "TMPDIR"
168#endif
169 };
170 for (size_t iVar = 0; iVar < RT_ELEMENTS(s_apszVars); iVar++)
171 {
172 int rc = RTEnvGetEx(RTENV_DEFAULT, s_apszVars[iVar], pszPath, cchPath, NULL);
173 if (rc != VERR_ENV_VAR_NOT_FOUND)
174 return rc;
175 }
176
177 /*
178 * Here we should use some sane system default, instead we just use
179 * the typical unix temp dir for now.
180 */
181 /** @todo Windows should default to the windows directory, see GetTempPath.
182 * Some unixes has path.h and _PATH_TMP. There is also a question about
183 * whether /var/tmp wouldn't be a better place... */
184 if (cchPath < sizeof("/tmp") )
185 return VERR_BUFFER_OVERFLOW;
186 memcpy(pszPath, "/tmp", sizeof("/tmp"));
187 return VINF_SUCCESS;
188}
189
190
191RTR3DECL(int) RTPathGetMode(const char *pszPath, PRTFMODE pfMode)
192{
193 AssertPtrReturn(pfMode, VERR_INVALID_POINTER);
194
195 RTFSOBJINFO ObjInfo;
196 int rc = RTPathQueryInfoEx(pszPath, &ObjInfo, RTFSOBJATTRADD_NOTHING, RTPATH_F_FOLLOW_LINK);
197 if (RT_SUCCESS(rc))
198 *pfMode = ObjInfo.Attr.fMode;
199
200 return rc;
201}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette