BSD support

This commit is contained in:
Luxferre
2026-08-21 19:08:45 +03:00
parent f7d9a69952
commit ac479bf457
3 changed files with 93 additions and 7 deletions
+66 -1
View File
@@ -25,6 +25,71 @@
#include <sys/stat.h>
#include <unistd.h>
#include <sys/types.h>
/* sysctlbyname() is the portable way to query CPU count on every BSD family
* (FreeBSD, OpenBSD, NetBSD, DragonFly) and on macOS/Darwin. */
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || defined(__APPLE__)
#include <sys/sysctl.h>
#endif
/* On some libc implementations (e.g. FreeBSD) the d_type constants are only
* exposed when BSD visibility is enabled. Provide portable fallbacks matching
* <sys/dirent.h> so the directory-walk classification always compiles. */
#ifndef DT_UNKNOWN
#define DT_UNKNOWN 0
#define DT_FIFO 1
#define DT_CHR 2
#define DT_DIR 4
#define DT_BLK 6
#define DT_REG 8
#define DT_LNK 10
#define DT_SOCK 12
#define DT_WHT 14
#endif
/* memmem() is a BSD/GNU extension, not POSIX. glibc and the BSD/Darwin libcs
* all provide it, but only expose the prototype outside strict conformance
* mode, so declare it ourselves when the system has not already. Any libc that
* genuinely lacks it (rare) gets a small portable fallback. */
#if defined(__GLIBC__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || defined(__APPLE__)
#ifndef memmem
void *memmem(const void *haystack, size_t haystacklen,
const void *needle, size_t needlelen);
#endif
#else
static void *gg_memmem(const void *haystack, size_t haystacklen,
const void *needle, size_t needlelen) {
if (needlelen == 0) return (void *)haystack;
if (haystacklen < needlelen) return NULL;
const unsigned char *h = (const unsigned char *)haystack;
const unsigned char *n = (const unsigned char *)needle;
const unsigned char *end = h + haystacklen - needlelen;
for (; h <= end; h++)
if (memcmp(h, n, needlelen) == 0) return (void *)h;
return NULL;
}
#define memmem gg_memmem
#endif
/* Number of online processors, portable across Linux, the BSDs and macOS. */
static long gg_nproc(void) {
#if defined(_SC_NPROCESSORS_ONLN)
long n = sysconf(_SC_NPROCESSORS_ONLN);
if (n > 0) return n;
#elif defined(_SC_NPROCESSORS_CONF)
long n = sysconf(_SC_NPROCESSORS_CONF);
if (n > 0) return n;
#endif
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || defined(__APPLE__)
int ncpu = 1;
size_t len = sizeof(ncpu);
if (sysctlbyname("hw.ncpu", &ncpu, &len, NULL, 0) == 0 && ncpu > 0)
return (long)ncpu;
#endif
return 1;
}
#ifndef PATH_MAX
#define PATH_MAX 4096
#endif
@@ -588,7 +653,7 @@ int main(int argc, char **argv) {
push_dir(root, "", NULL);
}
long nproc = sysconf(_SC_NPROCESSORS_ONLN);
long nproc = gg_nproc();
if(nproc < 1)
nproc = 1;
int nth = (int)nproc;