move uv_get_exepath() to platform files

This commit is contained in:
Ryan Dahl 2011-06-17 13:13:18 +02:00
parent d880491616
commit c4c022fb0c
6 changed files with 109 additions and 55 deletions

View File

@ -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

View File

@ -25,6 +25,7 @@
#include <mach/mach.h>
#include <mach/mach_time.h>
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;
}

49
uv-freebsd.c Normal file
View File

@ -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;
}

View File

@ -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;
}

View File

@ -23,6 +23,14 @@
#include <stdint.h>
#include <sys/time.h>
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;
}

View File

@ -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
}