small getself.c improvements for FreeBSD

All development conversation and discussion takes place here

Moderator: Moderator

Post Reply
Message
Author
lifanov
Cornac
Posts: 32
Joined: Sat Jan 14, 2012 12:53 am

small getself.c improvements for FreeBSD

#1 Post 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)

lifanov
Cornac
Posts: 32
Joined: Sat Jan 14, 2012 12:53 am

Re: small getself.c improvements for FreeBSD

#2 Post by lifanov »

I release copyright for this change to DarkGod to license as he wishes.

darkgod
Master of Eyal
Posts: 10750
Joined: Wed Jul 24, 2002 9:26 pm
Location: Angolwen
Contact:

Re: small getself.c improvements for FreeBSD

#3 Post by darkgod »

Thanks :)
[tome] joylove: You can't just release an expansion like one would release a Kraken XD
--
[tome] phantomfrettchen: your ability not to tease anyone is simply stunning ;)

Post Reply