diff --git a/config-unix.mk b/config-unix.mk index 4c82ee24..b47b4b7d 100644 --- a/config-unix.mk +++ b/config-unix.mk @@ -39,6 +39,11 @@ LINKFLAGS+=-lrt UV_OS_FILE=uv-linux.c endif +ifeq (FreeBSD,$(uname_S)) +LINKFLAGS+= +UV_OS_FILE=uv-freebsd.c +endif + # Need _GNU_SOURCE for strdup? RUNNER_CFLAGS=$(CFLAGS) -D_GNU_SOURCE diff --git a/uv-darwin.c b/uv-darwin.c index 36730d29..6670a34a 100644 --- a/uv-darwin.c +++ b/uv-darwin.c @@ -25,6 +25,7 @@ #include #include + uint64_t uv_get_hrtime() { uint64_t time; Nanoseconds enano; @@ -32,3 +33,32 @@ uint64_t uv_get_hrtime() { enano = AbsoluteToNanoseconds(*(AbsoluteTime *)&time); return (*(uint64_t *)&enano); } + + +int uv_get_exepath(char* buffer, size_t* size) { + uint32_t usize; + int result; + char* path; + char* fullpath; + + if (!buffer || !size) { + return -1; + } + + usize = *size; + result = _NSGetExecutablePath(buffer, &usize); + if (result) return result; + + path = (char*)malloc(2 * PATH_MAX); + fullpath = realpath(buffer, path); + + if (fullpath == NULL) { + free(path); + return -1; + } + + strncpy(buffer, fullpath, *size); + free(fullpath); + *size = strlen(buffer); + return 0; +} diff --git a/uv-freebsd.c b/uv-freebsd.c new file mode 100644 index 00000000..8c68e24e --- /dev/null +++ b/uv-freebsd.c @@ -0,0 +1,49 @@ +/* Copyright Joyent, Inc. and other Node contributors. All rights reserved. + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#include "uv.h" + + +int uv_get_exepath(char* buffer, size_t* size) { + uint32_t usize; + int result; + char* path; + char* fullpath; + + if (!buffer || !size) { + return -1; + } + + int mib[4]; + + mib[0] = CTL_KERN; + mib[1] = KERN_PROC; + mib[2] = KERN_PROC_PATHNAME; + mib[3] = -1; + + size_t cb = *size; + if (sysctl(mib, 4, buffer, &cb, NULL, 0) < 0) { + *size = 0; + return -1; + } + *size = strlen(buffer); + + return 0; +} diff --git a/uv-linux.c b/uv-linux.c index 2e6bf45f..8eae6c17 100644 --- a/uv-linux.c +++ b/uv-linux.c @@ -33,3 +33,20 @@ uint64_t uv_get_hrtime() { clock_gettime(CLOCK_MONOTONIC, &ts); return (ts.tv_sec * NANOSEC + ts.tv_nsec); } + + +int uv_get_exepath(char* buffer, size_t* size) { + uint32_t usize; + int result; + char* path; + char* fullpath; + + if (!buffer || !size) { + return -1; + } + + *size = readlink("/proc/self/exe", buffer, *size - 1); + if (*size <= 0) return -1; + buffer[*size] = '\0'; + return 0; +} diff --git a/uv-sunos.c b/uv-sunos.c index 3b0dbb63..0f1fad3c 100644 --- a/uv-sunos.c +++ b/uv-sunos.c @@ -23,6 +23,14 @@ #include #include + uint64_t uv_get_hrtime() { return (gethrtime()); } + + +int uv_get_exepath(char* buffer, size_t* size) { + assert(0 && "implement me"); + /* Need to return argv[0] */ + return -1; +} diff --git a/uv-unix.c b/uv-unix.c index 4163683c..1d94388b 100644 --- a/uv-unix.c +++ b/uv-unix.c @@ -1244,58 +1244,3 @@ int64_t uv_timer_get_repeat(uv_timer_t* timer) { } -int uv_get_exepath(char* buffer, size_t* size) { - uint32_t usize; - int result; - char* path; - char* fullpath; - - if (!buffer || !size) { - return -1; - } - -#if defined(__APPLE__) - usize = *size; - result = _NSGetExecutablePath(buffer, &usize); - if (result) return result; - - path = (char*)malloc(2 * PATH_MAX); - fullpath = realpath(buffer, path); - - if (fullpath == NULL) { - free(path); - return -1; - } - - strncpy(buffer, fullpath, *size); - free(fullpath); - *size = strlen(buffer); - return 0; -#elif defined(__linux__) - *size = readlink("/proc/self/exe", buffer, *size - 1); - if (*size <= 0) return -1; - buffer[*size] = '\0'; - return 0; -#elif defined(__FreeBSD__) - int mib[4]; - - mib[0] = CTL_KERN; - mib[1] = KERN_PROC; - mib[2] = KERN_PROC_PATHNAME; - mib[3] = -1; - - size_t cb = *size; - if (sysctl(mib, 4, buffer, &cb, NULL, 0) < 0) { - *size = 0; - return -1; - } - *size = strlen(buffer); - - return 0; -#else - assert(0 && "implement me"); - /* Need to return argv[0] */ -#endif -} - -