@@ -119,6 +119,12 @@ static inline void _CFSetProgramNameFromPath(const char *path) {
119119 __CFprogname = (__CFprogname ? __CFprogname + 1 : __CFProcessPath );
120120}
121121
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+
122128const char * _CFProcessPath (void ) {
123129 if (__CFProcessPath ) return __CFProcessPath ;
124130
@@ -175,6 +181,53 @@ const char *_CFProcessPath(void) {
175181 }
176182 return __CFProcessPath ;
177183#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.
178231 if (!__CFProcessIsRestricted ()) {
179232 char * path = getenv ("_" );
180233 if (path != NULL ) {
@@ -1574,6 +1627,9 @@ CF_CROSS_PLATFORM_EXPORT int _CFThreadSetName(_CFThreadRef thread, const char *_
15741627 return 0 ;
15751628#elif TARGET_OS_LINUX
15761629 return pthread_setname_np (thread , name );
1630+ #elif TARGET_OS_BSD
1631+ pthread_set_name_np (thread , name );
1632+ return 0 ;
15771633#endif
15781634}
15791635
@@ -1593,6 +1649,9 @@ CF_CROSS_PLATFORM_EXPORT int _CFThreadGetName(char *buf, int length) {
15931649 return 0 ;
15941650#elif TARGET_OS_LINUX
15951651 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 ;
15961655#elif TARGET_OS_WIN32
15971656 * buf = '\0' ;
15981657
@@ -1630,6 +1689,9 @@ CF_EXPORT char **_CFEnviron(void) {
16301689#elif TARGET_OS_WIN32
16311690 return _environ ;
16321691#else
1692+ #if TARGET_OS_BSD
1693+ extern char * * environ ;
1694+ #endif
16331695 return environ ;
16341696#endif
16351697}
0 commit comments