1 | /* $Id: path.cpp 28800 2010-04-27 08:22:32Z 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.215389.xyz. 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 |
|
---|
41 | RTDECL(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 | */
|
---|
70 | RTDECL(int) RTPathAppPrivateNoArch(char *pszPath, size_t cchPath)
|
---|
71 | {
|
---|
72 | #if !defined(RT_OS_WINDOWS) && defined(RTPATH_APP_PRIVATE)
|
---|
73 | char *pszUtf8Path;
|
---|
74 | int rc;
|
---|
75 | rc = rtPathFromNative(&pszUtf8Path, RTPATH_APP_PRIVATE);
|
---|
76 | if (RT_SUCCESS(rc))
|
---|
77 | {
|
---|
78 | size_t cchPathPrivateNoArch = strlen(pszUtf8Path);
|
---|
79 | if (cchPathPrivateNoArch < cchPath)
|
---|
80 | memcpy(pszPath, pszUtf8Path, cchPathPrivateNoArch + 1);
|
---|
81 | else
|
---|
82 | rc = VERR_BUFFER_OVERFLOW;
|
---|
83 | RTStrFree(pszUtf8Path);
|
---|
84 | }
|
---|
85 | return rc;
|
---|
86 | #else
|
---|
87 | return RTPathExecDir(pszPath, cchPath);
|
---|
88 | #endif
|
---|
89 | }
|
---|
90 |
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Gets the directory for architecture-dependent application data, for
|
---|
94 | * example modules which can be loaded at runtime.
|
---|
95 | *
|
---|
96 | * Linux: /usr/lib/@<application@>
|
---|
97 | * Windows: @<program files directory@>/@<application@>
|
---|
98 | * Old path: same as RTPathExecDir()
|
---|
99 | *
|
---|
100 | * @returns iprt status code.
|
---|
101 | * @param pszPath Buffer where to store the path.
|
---|
102 | * @param cchPath Buffer size in bytes.
|
---|
103 | */
|
---|
104 | RTDECL(int) RTPathAppPrivateArch(char *pszPath, size_t cchPath)
|
---|
105 | {
|
---|
106 | #if !defined(RT_OS_WINDOWS) && defined(RTPATH_APP_PRIVATE_ARCH)
|
---|
107 | char *pszUtf8Path;
|
---|
108 | int rc;
|
---|
109 | rc = rtPathFromNative(&pszUtf8Path, RTPATH_APP_PRIVATE_ARCH);
|
---|
110 | if (RT_SUCCESS(rc))
|
---|
111 | {
|
---|
112 | size_t cchPathPrivateArch = strlen(pszUtf8Path);
|
---|
113 | if (cchPathPrivateArch < cchPath)
|
---|
114 | memcpy(pszPath, pszUtf8Path, cchPathPrivateArch + 1);
|
---|
115 | else
|
---|
116 | rc = VERR_BUFFER_OVERFLOW;
|
---|
117 | RTStrFree(pszUtf8Path);
|
---|
118 | }
|
---|
119 | return rc;
|
---|
120 | #else
|
---|
121 | return RTPathExecDir(pszPath, cchPath);
|
---|
122 | #endif
|
---|
123 | }
|
---|
124 |
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * Gets the directory of shared libraries. This is not the same as
|
---|
128 | * RTPathAppPrivateArch() as Linux depends all shared libraries in
|
---|
129 | * a common global directory where ld.so can found them.
|
---|
130 | *
|
---|
131 | * Linux: /usr/lib
|
---|
132 | * Windows: @<program files directory@>/@<application@>
|
---|
133 | * Old path: same as RTPathExecDir()
|
---|
134 | *
|
---|
135 | * @returns iprt status code.
|
---|
136 | * @param pszPath Buffer where to store the path.
|
---|
137 | * @param cchPath Buffer size in bytes.
|
---|
138 | */
|
---|
139 | RTDECL(int) RTPathSharedLibs(char *pszPath, size_t cchPath)
|
---|
140 | {
|
---|
141 | #if !defined(RT_OS_WINDOWS) && defined(RTPATH_SHARED_LIBS)
|
---|
142 | char *pszUtf8Path;
|
---|
143 | int rc;
|
---|
144 | rc = rtPathFromNative(&pszUtf8Path, RTPATH_SHARED_LIBS);
|
---|
145 | if (RT_SUCCESS(rc))
|
---|
146 | {
|
---|
147 | size_t cchPathSharedLibs = strlen(pszUtf8Path);
|
---|
148 | if (cchPathSharedLibs < cchPath)
|
---|
149 | memcpy(pszPath, pszUtf8Path, cchPathSharedLibs + 1);
|
---|
150 | else
|
---|
151 | rc = VERR_BUFFER_OVERFLOW;
|
---|
152 | RTStrFree(pszUtf8Path);
|
---|
153 | }
|
---|
154 | return rc;
|
---|
155 | #else
|
---|
156 | return RTPathExecDir(pszPath, cchPath);
|
---|
157 | #endif
|
---|
158 | }
|
---|
159 |
|
---|
160 |
|
---|
161 | /**
|
---|
162 | * Gets the directory for documentation.
|
---|
163 | *
|
---|
164 | * Linux: /usr/share/doc/@<application@>
|
---|
165 | * Windows: @<program files directory@>/@<application@>
|
---|
166 | * Old path: same as RTPathExecDir()
|
---|
167 | *
|
---|
168 | * @returns iprt status code.
|
---|
169 | * @param pszPath Buffer where to store the path.
|
---|
170 | * @param cchPath Buffer size in bytes.
|
---|
171 | */
|
---|
172 | RTDECL(int) RTPathAppDocs(char *pszPath, size_t cchPath)
|
---|
173 | {
|
---|
174 | #if !defined(RT_OS_WINDOWS) && defined(RTPATH_APP_DOCS)
|
---|
175 | char *pszUtf8Path;
|
---|
176 | int rc;
|
---|
177 | rc = rtPathFromNative(&pszUtf8Path, RTPATH_APP_DOCS);
|
---|
178 | if (RT_SUCCESS(rc))
|
---|
179 | {
|
---|
180 | size_t cchPathAppDocs = strlen(pszUtf8Path);
|
---|
181 | if (cchPathAppDocs < cchPath)
|
---|
182 | memcpy(pszPath, pszUtf8Path, cchPathAppDocs + 1);
|
---|
183 | else
|
---|
184 | rc = VERR_BUFFER_OVERFLOW;
|
---|
185 | RTStrFree(pszUtf8Path);
|
---|
186 | }
|
---|
187 | return rc;
|
---|
188 | #else
|
---|
189 | return RTPathExecDir(pszPath, cchPath);
|
---|
190 | #endif
|
---|
191 | }
|
---|
192 |
|
---|
193 |
|
---|
194 | /**
|
---|
195 | * Gets the temporary directory path.
|
---|
196 | *
|
---|
197 | * @returns iprt status code.
|
---|
198 | *
|
---|
199 | * @param pszPath Buffer where to store the path.
|
---|
200 | * @param cchPath Buffer size in bytes.
|
---|
201 | */
|
---|
202 | RTDECL(int) RTPathTemp(char *pszPath, size_t cchPath)
|
---|
203 | {
|
---|
204 | /*
|
---|
205 | * Try get it from the environment first.
|
---|
206 | */
|
---|
207 | static const char * const s_apszVars[] =
|
---|
208 | {
|
---|
209 | "IPRT_TMPDIR"
|
---|
210 | #if defined(RT_OS_WINDOWS)
|
---|
211 | , "TMP", "TEMP", "USERPROFILE"
|
---|
212 | #elif defined(RT_OS_OS2)
|
---|
213 | , "TMP", "TEMP", "TMPDIR"
|
---|
214 | #else
|
---|
215 | , "TMPDIR"
|
---|
216 | #endif
|
---|
217 | };
|
---|
218 | for (size_t iVar = 0; iVar < RT_ELEMENTS(s_apszVars); iVar++)
|
---|
219 | {
|
---|
220 | int rc = RTEnvGetEx(RTENV_DEFAULT, s_apszVars[iVar], pszPath, cchPath, NULL);
|
---|
221 | if (rc != VERR_ENV_VAR_NOT_FOUND)
|
---|
222 | return rc;
|
---|
223 | }
|
---|
224 |
|
---|
225 | /*
|
---|
226 | * Here we should use some sane system default, instead we just use
|
---|
227 | * the typical unix temp dir for now.
|
---|
228 | */
|
---|
229 | /** @todo Windows should default to the windows directory, see GetTempPath.
|
---|
230 | * Some unixes has path.h and _PATH_TMP. There is also a question about
|
---|
231 | * whether /var/tmp wouldn't be a better place... */
|
---|
232 | if (cchPath < sizeof("/tmp") )
|
---|
233 | return VERR_BUFFER_OVERFLOW;
|
---|
234 | memcpy(pszPath, "/tmp", sizeof("/tmp"));
|
---|
235 | return VINF_SUCCESS;
|
---|
236 | }
|
---|
237 |
|
---|