@@ -119,6 +119,12 @@ static inline void _CFSetProgramNameFromPath(const char *path) {
119
119
__CFprogname = (__CFprogname ? __CFprogname + 1 : __CFProcessPath );
120
120
}
121
121
122
+ #if TARGET_OS_BSD && defined(__OpenBSD__ )
123
+ #include <sys/types.h>
124
+ #include <sys/sysctl.h>
125
+ #include <sys/exec.h>
126
+ #endif
127
+
122
128
const char * _CFProcessPath (void ) {
123
129
if (__CFProcessPath ) return __CFProcessPath ;
124
130
@@ -175,6 +181,53 @@ const char *_CFProcessPath(void) {
175
181
}
176
182
return __CFProcessPath ;
177
183
#else // TARGET_OS_BSD
184
+ char * argv0 = NULL ;
185
+
186
+ // Get argv[0].
187
+ #if defined(__OpenBSD__ )
188
+ int mib [2 ] = {CTL_VM , VM_PSSTRINGS };
189
+ struct _ps_strings _ps ;
190
+ size_t len = sizeof (_ps );
191
+
192
+ if (sysctl (mib , 2 , & _ps , & len , NULL , 0 ) != -1 ) {
193
+ struct ps_strings * ps = _ps .val ;
194
+ char * res = realpath (ps -> ps_argvstr [0 ], NULL );
195
+ argv0 = res ? res : strdup (ps -> ps_argvstr [0 ]);
196
+ }
197
+ #endif
198
+
199
+ if (!__CFProcessIsRestricted () && argv0 && argv0 [0 ] == '/' ) {
200
+ _CFSetProgramNameFromPath (argv0 );
201
+ free (argv0 );
202
+ return __CFProcessPath ;
203
+ }
204
+
205
+ // Search PATH.
206
+ if (argv0 ) {
207
+ char * paths = getenv ("PATH" );
208
+ char * p = NULL ;
209
+ while ((p = strsep (& paths , ":" )) != NULL ) {
210
+ char pp [PATH_MAX ];
211
+ int l = snprintf (pp , PATH_MAX , "%s/%s" , p , argv0 );
212
+ if (l >= PATH_MAX ) {
213
+ continue ;
214
+ }
215
+ char * res = realpath (pp , NULL );
216
+ if (!res ) {
217
+ continue ;
218
+ }
219
+ if (!__CFProcessIsRestricted () && access (res , X_OK ) == 0 ) {
220
+ _CFSetProgramNameFromPath (res );
221
+ free (argv0 );
222
+ free (res );
223
+ return __CFProcessPath ;
224
+ }
225
+ free (res );
226
+ }
227
+ free (argv0 );
228
+ }
229
+
230
+ // See if the shell will help.
178
231
if (!__CFProcessIsRestricted ()) {
179
232
char * path = getenv ("_" );
180
233
if (path != NULL ) {
@@ -1574,6 +1627,9 @@ CF_CROSS_PLATFORM_EXPORT int _CFThreadSetName(_CFThreadRef thread, const char *_
1574
1627
return 0 ;
1575
1628
#elif TARGET_OS_LINUX
1576
1629
return pthread_setname_np (thread , name );
1630
+ #elif TARGET_OS_BSD
1631
+ pthread_set_name_np (thread , name );
1632
+ return 0 ;
1577
1633
#endif
1578
1634
}
1579
1635
@@ -1593,6 +1649,9 @@ CF_CROSS_PLATFORM_EXPORT int _CFThreadGetName(char *buf, int length) {
1593
1649
return 0 ;
1594
1650
#elif TARGET_OS_LINUX
1595
1651
return pthread_getname_np (pthread_self (), buf , length );
1652
+ #elif TARGET_OS_BSD
1653
+ pthread_get_name_np (pthread_self (), buf , length );
1654
+ return 0 ;
1596
1655
#elif TARGET_OS_WIN32
1597
1656
* buf = '\0' ;
1598
1657
@@ -1630,6 +1689,9 @@ CF_EXPORT char **_CFEnviron(void) {
1630
1689
#elif TARGET_OS_WIN32
1631
1690
return _environ ;
1632
1691
#else
1692
+ #if TARGET_OS_BSD
1693
+ extern char * * environ ;
1694
+ #endif
1633
1695
return environ ;
1634
1696
#endif
1635
1697
}
0 commit comments