VirtualBox

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

Last change on this file since 21673 was 21673, checked in by vboxsync, 15 years ago

IPRT: Split up r3/path.cpp.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.0 KB
Line 
1/* $Id: path.cpp 21673 2009-07-17 12:10:10Z vboxsync $ */
2/** @file
3 * IPRT - Path Manipulation.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31
32/*******************************************************************************
33* Header Files *
34*******************************************************************************/
35#include "internal/iprt.h"
36#include <iprt/assert.h>
37#include <iprt/env.h>
38#include <iprt/err.h>
39#include <iprt/path.h>
40#include <iprt/string.h>
41#include "internal/process.h"
42
43
44RTDECL(int) RTPathExecDir(char *pszPath, size_t cchPath)
45{
46 AssertReturn(g_szrtProcExePath[0], VERR_WRONG_ORDER);
47
48 /*
49 * Calc the length and check if there is space before copying.
50 */
51 size_t cch = g_cchrtProcDir;
52 if (cch <= cchPath)
53 {
54 memcpy(pszPath, g_szrtProcExePath, cch);
55 pszPath[cch] = '\0';
56 return VINF_SUCCESS;
57 }
58
59 AssertMsgFailed(("Buffer too small (%zu <= %zu)\n", cchPath, cch));
60 return VERR_BUFFER_OVERFLOW;
61}
62
63
64/**
65 * Gets the directory for architecture-independent application data, for
66 * example NLS files, module sources, ...
67 *
68 * Linux: /usr/shared/@<application@>
69 * Windows: @<program files directory@>/@<application@>
70 * Old path: same as RTPathExecDir()
71 *
72 */
73RTDECL(int) RTPathAppPrivateNoArch(char *pszPath, size_t cchPath)
74{
75#if !defined(RT_OS_WINDOWS) && defined(RTPATH_APP_PRIVATE)
76 char *pszUtf8Path;
77 int rc;
78 rc = rtPathFromNative(&pszUtf8Path, RTPATH_APP_PRIVATE);
79 if (RT_SUCCESS(rc))
80 {
81 size_t cchPathPrivateNoArch = strlen(pszUtf8Path);
82 if (cchPathPrivateNoArch < cchPath)
83 memcpy(pszPath, pszUtf8Path, cchPathPrivateNoArch + 1);
84 else
85 rc = VERR_BUFFER_OVERFLOW;
86 RTStrFree(pszUtf8Path);
87 }
88 return rc;
89#else
90 return RTPathExecDir(pszPath, cchPath);
91#endif
92}
93
94
95/**
96 * Gets the directory for architecture-dependent application data, for
97 * example modules which can be loaded at runtime.
98 *
99 * Linux: /usr/lib/@<application@>
100 * Windows: @<program files directory@>/@<application@>
101 * Old path: same as RTPathExecDir()
102 *
103 * @returns iprt status code.
104 * @param pszPath Buffer where to store the path.
105 * @param cchPath Buffer size in bytes.
106 */
107RTDECL(int) RTPathAppPrivateArch(char *pszPath, size_t cchPath)
108{
109#if !defined(RT_OS_WINDOWS) && defined(RTPATH_APP_PRIVATE_ARCH)
110 char *pszUtf8Path;
111 int rc;
112 rc = rtPathFromNative(&pszUtf8Path, RTPATH_APP_PRIVATE_ARCH);
113 if (RT_SUCCESS(rc))
114 {
115 size_t cchPathPrivateArch = strlen(pszUtf8Path);
116 if (cchPathPrivateArch < cchPath)
117 memcpy(pszPath, pszUtf8Path, cchPathPrivateArch + 1);
118 else
119 rc = VERR_BUFFER_OVERFLOW;
120 RTStrFree(pszUtf8Path);
121 }
122 return rc;
123#else
124 return RTPathExecDir(pszPath, cchPath);
125#endif
126}
127
128
129/**
130 * Gets the directory of shared libraries. This is not the same as
131 * RTPathAppPrivateArch() as Linux depends all shared libraries in
132 * a common global directory where ld.so can found them.
133 *
134 * Linux: /usr/lib
135 * Windows: @<program files directory@>/@<application@>
136 * Old path: same as RTPathExecDir()
137 *
138 * @returns iprt status code.
139 * @param pszPath Buffer where to store the path.
140 * @param cchPath Buffer size in bytes.
141 */
142RTDECL(int) RTPathSharedLibs(char *pszPath, size_t cchPath)
143{
144#if !defined(RT_OS_WINDOWS) && defined(RTPATH_SHARED_LIBS)
145 char *pszUtf8Path;
146 int rc;
147 rc = rtPathFromNative(&pszUtf8Path, RTPATH_SHARED_LIBS);
148 if (RT_SUCCESS(rc))
149 {
150 size_t cchPathSharedLibs = strlen(pszUtf8Path);
151 if (cchPathSharedLibs < cchPath)
152 memcpy(pszPath, pszUtf8Path, cchPathSharedLibs + 1);
153 else
154 rc = VERR_BUFFER_OVERFLOW;
155 RTStrFree(pszUtf8Path);
156 }
157 return rc;
158#else
159 return RTPathExecDir(pszPath, cchPath);
160#endif
161}
162
163
164/**
165 * Gets the directory for documentation.
166 *
167 * Linux: /usr/share/doc/@<application@>
168 * Windows: @<program files directory@>/@<application@>
169 * Old path: same as RTPathExecDir()
170 *
171 * @returns iprt status code.
172 * @param pszPath Buffer where to store the path.
173 * @param cchPath Buffer size in bytes.
174 */
175RTDECL(int) RTPathAppDocs(char *pszPath, size_t cchPath)
176{
177#if !defined(RT_OS_WINDOWS) && defined(RTPATH_APP_DOCS)
178 char *pszUtf8Path;
179 int rc;
180 rc = rtPathFromNative(&pszUtf8Path, RTPATH_APP_DOCS);
181 if (RT_SUCCESS(rc))
182 {
183 size_t cchPathAppDocs = strlen(pszUtf8Path);
184 if (cchPathAppDocs < cchPath)
185 memcpy(pszPath, pszUtf8Path, cchPathAppDocs + 1);
186 else
187 rc = VERR_BUFFER_OVERFLOW;
188 RTStrFree(pszUtf8Path);
189 }
190 return rc;
191#else
192 return RTPathExecDir(pszPath, cchPath);
193#endif
194}
195
196
197/**
198 * Gets the temporary directory path.
199 *
200 * @returns iprt status code.
201 *
202 * @param pszPath Buffer where to store the path.
203 * @param cchPath Buffer size in bytes.
204 */
205RTDECL(int) RTPathTemp(char *pszPath, size_t cchPath)
206{
207 /*
208 * Try get it from the environment first.
209 */
210 static const char * const s_apszVars[] =
211 {
212 "IPRT_TMPDIR"
213#if defined(RT_OS_WINDOWS)
214 , "TMP", "TEMP", "USERPROFILE"
215#elif defined(RT_OS_OS2)
216 , "TMP", "TEMP", "TMPDIR"
217#else
218 , "TMPDIR"
219#endif
220 };
221 for (size_t iVar = 0; iVar < RT_ELEMENTS(s_apszVars); iVar++)
222 {
223 int rc = RTEnvGetEx(RTENV_DEFAULT, s_apszVars[iVar], pszPath, cchPath, NULL);
224 if (rc != VERR_ENV_VAR_NOT_FOUND)
225 return rc;
226 }
227
228 /*
229 * Here we should use some sane system default, instead we just use
230 * the typical unix temp dir for now.
231 */
232 /** @todo Windows should default to the windows directory, see GetTempPath.
233 * Some unixes has path.h and _PATH_TMP. There is also a question about
234 * whether /var/tmp wouldn't be a better place... */
235 if (cchPath < sizeof("/tmp") )
236 return VERR_BUFFER_OVERFLOW;
237 memcpy(pszPath, "/tmp", sizeof("/tmp"));
238 return VINF_SUCCESS;
239}
240
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