@@ -113,44 +113,36 @@ const char **_CFGetProcessPath(void) {
113
113
return & __CFProcessPath ;
114
114
}
115
115
116
- #if TARGET_OS_WIN32
116
+ static inline void _CFSetProgramNameFromPath (const char * path ) {
117
+ __CFProcessPath = strdup (path );
118
+ __CFprogname = strrchr (__CFProcessPath , PATH_SEP );
119
+ __CFprogname = (__CFprogname ? __CFprogname + 1 : __CFProcessPath );
120
+ }
121
+
117
122
const char * _CFProcessPath (void ) {
118
123
if (__CFProcessPath ) return __CFProcessPath ;
124
+
125
+ #if TARGET_OS_WIN32
119
126
wchar_t buf [CFMaxPathSize ] = {0 };
120
127
DWORD rlen = GetModuleFileNameW (NULL , buf , sizeof (buf ) / sizeof (buf [0 ]));
121
128
if (0 < rlen ) {
122
129
char asciiBuf [CFMaxPathSize ] = {0 };
123
130
int res = WideCharToMultiByte (CP_UTF8 , 0 , buf , rlen , asciiBuf , sizeof (asciiBuf ) / sizeof (asciiBuf [0 ]), NULL , NULL );
124
131
if (0 < res ) {
125
- __CFProcessPath = strdup (asciiBuf );
126
- __CFprogname = strrchr (__CFProcessPath , PATH_SEP );
127
- __CFprogname = (__CFprogname ? __CFprogname + 1 : __CFProcessPath );
132
+ _CFSetProgramNameFromPath (asciiBuf );
128
133
}
129
134
}
130
135
if (!__CFProcessPath ) {
131
136
__CFProcessPath = "" ;
132
137
__CFprogname = __CFProcessPath ;
133
138
}
134
139
return __CFProcessPath ;
135
- }
136
- #endif
137
-
138
- #if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_BSD
139
- CF_CROSS_PLATFORM_EXPORT Boolean _CFIsMainThread (void ) {
140
- return pthread_main_np () == 1 ;
141
- }
142
- #endif
143
-
144
- #if TARGET_OS_MAC
145
- const char * _CFProcessPath (void ) {
146
- if (__CFProcessPath ) return __CFProcessPath ;
140
+ #elif TARGET_OS_MAC
147
141
#if TARGET_OS_OSX
148
142
if (!__CFProcessIsRestricted ()) {
149
143
const char * path = (char * )__CFgetenv ("CFProcessPath" );
150
144
if (path ) {
151
- __CFProcessPath = strdup (path );
152
- __CFprogname = strrchr (__CFProcessPath , PATH_SEP );
153
- __CFprogname = (__CFprogname ? __CFprogname + 1 : __CFProcessPath );
145
+ _CFSetProgramNameFromPath (path );
154
146
return __CFProcessPath ;
155
147
}
156
148
}
@@ -160,9 +152,7 @@ const char *_CFProcessPath(void) {
160
152
uint32_t size = CFMaxPathSize ;
161
153
char buffer [size ];
162
154
if (0 == _NSGetExecutablePath (buffer , & size )) {
163
- __CFProcessPath = strdup (buffer );
164
- __CFprogname = strrchr (__CFProcessPath , PATH_SEP );
165
- __CFprogname = (__CFprogname ? __CFprogname + 1 : __CFProcessPath );
155
+ _CFSetProgramNameFromPath (buffer );
166
156
}
167
157
}
168
158
@@ -171,6 +161,38 @@ const char *_CFProcessPath(void) {
171
161
__CFprogname = __CFProcessPath ;
172
162
}
173
163
return __CFProcessPath ;
164
+ #elif TARGET_OS_LINUX
165
+ char buf [CFMaxPathSize + 1 ];
166
+
167
+ ssize_t res = readlink ("/proc/self/exe" , buf , CFMaxPathSize );
168
+ if (res > 0 ) {
169
+ // null terminate, readlink does not
170
+ buf [res ] = 0 ;
171
+ _CFSetProgramNameFromPath (buf );
172
+ } else {
173
+ __CFProcessPath = "" ;
174
+ __CFprogname = __CFProcessPath ;
175
+ }
176
+ return __CFProcessPath ;
177
+ #else // TARGET_OS_BSD
178
+ if (!__CFProcessIsRestricted ()) {
179
+ char * path = getenv ("_" );
180
+ if (path != NULL ) {
181
+ _CFSetProgramNameFromPath (path );
182
+ return __CFProcessPath ;
183
+ }
184
+ }
185
+
186
+ // We don't yet have anything left to try.
187
+ __CFProcessPath = "" ;
188
+ __CFprogname = __CFProcessPath ;
189
+ return __CFProcessPath ;
190
+ #endif
191
+ }
192
+
193
+ #if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_BSD
194
+ CF_CROSS_PLATFORM_EXPORT Boolean _CFIsMainThread (void ) {
195
+ return pthread_main_np () == 1 ;
174
196
}
175
197
#endif
176
198
@@ -185,24 +207,6 @@ const char *_CFProcessPath(void) {
185
207
Boolean _CFIsMainThread (void ) {
186
208
return syscall (SYS_gettid ) == getpid ();
187
209
}
188
-
189
- const char * _CFProcessPath (void ) {
190
- if (__CFProcessPath ) return __CFProcessPath ;
191
- char buf [CFMaxPathSize + 1 ];
192
-
193
- ssize_t res = readlink ("/proc/self/exe" , buf , CFMaxPathSize );
194
- if (res > 0 ) {
195
- // null terminate, readlink does not
196
- buf [res ] = 0 ;
197
- __CFProcessPath = strdup (buf );
198
- __CFprogname = strrchr (__CFProcessPath , PATH_SEP );
199
- __CFprogname = (__CFprogname ? __CFprogname + 1 : __CFProcessPath );
200
- } else {
201
- __CFProcessPath = "" ;
202
- __CFprogname = __CFProcessPath ;
203
- }
204
- return __CFProcessPath ;
205
- }
206
210
#endif
207
211
208
212
CF_PRIVATE CFStringRef _CFProcessNameString (void ) {
0 commit comments