1 : /**********************************************************************
2 : * $Id: cpl_getexecpath.cpp 21915 2011-03-08 21:58:16Z warmerdam $
3 : *
4 : * Project: CPL - Common Portability Library
5 : * Purpose: Implement CPLGetExecPath().
6 : * Author: Frank Warmerdam, warmerdam@pobox.com
7 : *
8 : **********************************************************************
9 : * Copyright (c) 2005, Frank Warmerdam
10 : *
11 : * Permission is hereby granted, free of charge, to any person obtaining a
12 : * copy of this software and associated documentation files (the "Software"),
13 : * to deal in the Software without restriction, including without limitation
14 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 : * and/or sell copies of the Software, and to permit persons to whom the
16 : * Software is furnished to do so, subject to the following conditions:
17 : *
18 : * The above copyright notice and this permission notice shall be included
19 : * in all copies or substantial portions of the Software.
20 : *
21 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 : * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 : * DEALINGS IN THE SOFTWARE.
28 : ****************************************************************************/
29 :
30 : #include "cpl_conv.h"
31 : #include "cpl_string.h"
32 :
33 : CPL_CVSID("$Id: cpl_getexecpath.cpp 21915 2011-03-08 21:58:16Z warmerdam $");
34 :
35 : #if defined(WIN32) || defined(WIN32CE)
36 :
37 : #define HAVE_IMPLEMENTATION 1
38 :
39 : #if defined(WIN32CE)
40 : # include "cpl_win32ce_api.h"
41 : #else
42 : # include <windows.h>
43 : #endif
44 :
45 : /************************************************************************/
46 : /* CPLGetExecPath() */
47 : /************************************************************************/
48 :
49 : int CPLGetExecPath( char *pszPathBuf, int nMaxLength )
50 : {
51 : #ifndef WIN32CE
52 : if( CSLTestBoolean(
53 : CPLGetConfigOption( "GDAL_FILENAME_IS_UTF8", "YES" ) ) )
54 : {
55 : wchar_t *pwszPathBuf = (wchar_t*)
56 : CPLCalloc(nMaxLength+1,sizeof(wchar_t));
57 :
58 : if( GetModuleFileNameW( NULL, pwszPathBuf, nMaxLength ) == 0 )
59 : {
60 : CPLFree( pwszPathBuf );
61 : return FALSE;
62 : }
63 : else
64 : {
65 : char *pszDecoded =
66 : CPLRecodeFromWChar(pwszPathBuf,CPL_ENC_UCS2,CPL_ENC_UTF8);
67 :
68 : strncpy( pszPathBuf, pszDecoded, nMaxLength );
69 : CPLFree( pszDecoded );
70 : CPLFree( pwszPathBuf );
71 : return TRUE;
72 : }
73 : }
74 : else
75 : {
76 : if( GetModuleFileName( NULL, pszPathBuf, nMaxLength ) == 0 )
77 : return FALSE;
78 : else
79 : return TRUE;
80 : }
81 : #else
82 : if( CE_GetModuleFileNameA( NULL, pszPathBuf, nMaxLength ) == 0 )
83 : return FALSE;
84 : else
85 : return TRUE;
86 : #endif
87 : }
88 :
89 : #endif
90 :
91 : /************************************************************************/
92 : /* CPLGetExecPath() */
93 : /************************************************************************/
94 :
95 : #if !defined(HAVE_IMPLEMENTATION) && defined(__linux)
96 :
97 : #include "cpl_multiproc.h"
98 :
99 : #define HAVE_IMPLEMENTATION 1
100 :
101 0 : int CPLGetExecPath( char *pszPathBuf, int nMaxLength )
102 : {
103 0 : long nPID = getpid();
104 0 : CPLString osExeLink;
105 : ssize_t nResultLen;
106 :
107 0 : osExeLink.Printf( "/proc/%ld/exe", nPID );
108 0 : nResultLen = readlink( osExeLink, pszPathBuf, nMaxLength );
109 0 : if( nResultLen >= 0 )
110 0 : pszPathBuf[nResultLen] = '\0';
111 : else
112 0 : pszPathBuf[0] = '\0';
113 :
114 0 : return nResultLen > 0;
115 : }
116 :
117 : #endif
118 :
119 : /************************************************************************/
120 : /* CPLGetExecPath() */
121 : /************************************************************************/
122 :
123 : /**
124 : * Fetch path of executable.
125 : *
126 : * The path to the executable currently running is returned. This path
127 : * includes the name of the executable. Currently this only works on
128 : * win32 and linux platforms. The returned path is UTF-8 encoded.
129 : *
130 : * @param pszPathBuf the buffer into which the path is placed.
131 : * @param nMaxLength the buffer size, MAX_PATH+1 is suggested.
132 : *
133 : * @return FALSE on failure or TRUE on success.
134 : */
135 :
136 : #ifndef HAVE_IMPLEMENTATION
137 :
138 : int CPLGetExecPath( char *pszPathBuf, int nMaxLength )
139 :
140 : {
141 : return FALSE;
142 : }
143 :
144 : #endif
145 :
|