Page 1 of 1

small getself.c improvements for FreeBSD

Posted: Sat Feb 25, 2017 3:37 pm
by lifanov
Turns out FreeBSD can also use sysconf() :D
Also, don't rely on /proc being mounted: many users don't mount proc.

Code: Select all

diff --git a/src/getself.c b/src/getself.c
index 88e3ab4d5..a6933b7e0 100644
--- a/src/getself.c
+++ b/src/getself.c
@@ -42,24 +42,25 @@ int get_number_cpus()
 #elif defined(SELFEXE_BSD)
 #include <limits.h>
 #include <stdlib.h>
+#include <unistd.h>
+#include <sys/sysctl.h>
+
 const char *get_self_executable(int argc, char **argv)
 {
        static char res[PATH_MAX];
-       // Like linux, but /proc is not always mounted
-       //  return 0 if it's not
-       if (realpath("/proc/curproc/file", res)) return NULL;
+       int mib[4];
+       mib[0] = CTL_KERN;
+       mib[1] = KERN_PROC;
+       mib[2] = KERN_PROC_PATHNAME;
+       mib[3] = -1;
+       size_t cb = sizeof(res);
+       sysctl(mib,4,res,&cb,NULL,0);
        return res;
 }
 
-#import <sys/sysctl.h>
-
 int get_number_cpus()
 {
-       int count;
-       size_t size=sizeof(count);
-       
-       if (sysctlbyname("hw.ncpu",&count,&size,NULL,0)) return 1;
-       return count;
+       return sysconf(_SC_NPROCESSORS_ONLN);
 }
 
 #elif defined(SELFEXE_WINDOWS)

Re: small getself.c improvements for FreeBSD

Posted: Fri Mar 03, 2017 7:15 pm
by lifanov
I release copyright for this change to DarkGod to license as he wishes.

Re: small getself.c improvements for FreeBSD

Posted: Wed Mar 08, 2017 4:52 pm
by darkgod
Thanks :)