hewei.it 5 éve
szülő
commit
d28d69b769
7 módosított fájl, 90 hozzáadás és 29 törlés
  1. 1 1
      Makefile.in
  2. 57 0
      base/hbase.c
  3. 7 0
      base/hbase.h
  4. 10 0
      base/htime.c
  5. 4 0
      base/htime.h
  6. 10 27
      utils/hmain.cpp
  7. 1 1
      utils/hmain.h

+ 1 - 1
Makefile.in

@@ -239,7 +239,7 @@ ifeq ($(OS), Windows)
 	$(CC) -shared $^ -o $(LIBDIR)/$@.dll $(LDFLAGS) -Wl,--output-def,$(LIBDIR)/$(@).def
 else
 ifeq ($(OS), Apple)
-	$(CC) -shared $^ -o $(LIBDIR)/$@.dylib $(LDFLAGS)
+	$(CC) -dynamiclib -install_name @rpath/$@.dylib $^ -o $(LIBDIR)/$@.dylib $(LDFLAGS)
 else
 	$(CC) -shared $^ -o $(LIBDIR)/$@.so $(LDFLAGS)
 endif

+ 57 - 0
base/hbase.c

@@ -4,6 +4,11 @@
 #include <stdio.h>
 #include <string.h>
 
+#ifdef OS_DARWIN
+#include <mach-o/dyld.h> // for _NSGetExecutablePath
+#define environ (*_NSGetEnviron())
+#endif
+
 unsigned int g_alloc_cnt = 0;
 unsigned int g_free_cnt = 0;
 
@@ -145,6 +150,21 @@ bool strcontains(const char* str, const char* sub) {
     return strstr(str, sub) != NULL;
 }
 
+char* strrchr_dir(const char* filepath) {
+    char* b = (char*)filepath;
+    char* e = b;
+    while (*e) ++e;
+    while (--e >= b) {
+#ifdef OS_WIN
+        if (*e == '/' || *e == '\\')
+#else
+        if (*e == '/')
+#endif
+            return e;
+    }
+    return b;
+}
+
 bool getboolean(const char* str) {
     if (str == NULL) return false;
     int len = strlen(str);
@@ -158,3 +178,40 @@ bool getboolean(const char* str) {
     default: return false;
     }
 }
+
+char* get_executable_path(char* buf, int size) {
+#ifdef OS_WIN
+    GetModuleFileName(NULL, buf, size);
+#elif defined(OS_LINUX)
+    readlink("/proc/self/exe", buf, size);
+#elif defined(OS_DARWIN)
+    _NSGetExecutablePath(buf, (uint32_t*)&size);
+#endif
+    return buf;
+}
+
+char* get_executable_dir(char* buf, int size) {
+    char filepath[MAX_PATH];
+    get_executable_path(filepath, sizeof(filepath));
+    char* pos = strrchr_dir(filepath);
+    if (pos) {
+        *pos = '\0';
+        strncpy(buf, filepath, size);
+    }
+    return buf;
+}
+
+char* get_executable_file(char* buf, int size) {
+    char filepath[MAX_PATH];
+    get_executable_path(filepath, sizeof(filepath));
+    char* pos = strrchr_dir(filepath);
+    if (pos) {
+        strncpy(buf, pos+1, size);
+    }
+    return buf;
+}
+
+char* get_run_dir(char* buf, int size) {
+    getcwd(buf, size);
+    return buf;
+}

+ 7 - 0
base/hbase.h

@@ -47,6 +47,8 @@ char* strreverse(char* str);
 bool strstartswith(const char* str, const char* start);
 bool strendswith(const char* str, const char* end);
 bool strcontains(const char* str, const char* sub);
+#define strrchr_dot(str) strrchr(str, '.')
+char* strrchr_dir(const char* filepath);
 
 // strncpy n = sizeof(dest_buf)-1
 // safe_strncpy n = sizeof(dest_buf)
@@ -67,6 +69,11 @@ char* safe_strncat(char* dest, const char* src, size_t n);
 // 1 y on yes true enable
 bool getboolean(const char* str);
 
+char* get_executable_path(char* buf, int size);
+char* get_executable_dir(char* buf, int size);
+char* get_executable_file(char* buf, int size);
+char* get_run_dir(char* buf, int size);
+
 END_EXTERN_C
 
 #endif // HV_BASE_H_

+ 10 - 0
base/htime.c

@@ -109,6 +109,16 @@ time_t datetime_mktime(datetime_t* dt) {
     return mktime(&tm);
 }
 
+char* duration_fmt(int sec, char* buf) {
+    int h, m, s;
+    m = sec / 60;
+    s = sec % 60;
+    h = m / 60;
+    m = m % 60;
+    sprintf(buf, TIME_FMT, h, m, s);
+    return buf;
+}
+
 char* datetime_fmt(datetime_t* dt, char* buf) {
     sprintf(buf, DATETIME_FMT,
         dt->year, dt->month, dt->day,

+ 4 - 0
base/htime.h

@@ -79,6 +79,10 @@ unsigned long long gethrtime_us();
 datetime_t datetime_now();
 time_t     datetime_mktime(datetime_t* dt);
 
+#define TIME_FMT            "%02d:%02d:%02d"
+#define TIME_FMT_BUFLEN     12
+char* duration_fmt(int sec, char* buf);
+
 #define DATETIME_FMT        "%04d-%02d-%02d %02d:%02d:%02d.%03d"
 #define DATETIME_FMT_BUFLEN 24
 char* datetime_fmt(datetime_t* dt, char* buf);

+ 10 - 27
utils/hmain.cpp

@@ -7,6 +7,7 @@
 
 #ifdef OS_DARWIN
 #include <crt_externs.h>
+#include <mach-o/dyld.h> // for _NSGetExecutablePath
 #define environ (*_NSGetEnviron())
 #endif
 
@@ -23,31 +24,13 @@ int main_ctx_init(int argc, char** argv) {
         argv = (char**)malloc(2*sizeof(char*));
         argv[0] = (char*)malloc(MAX_PATH);
         argv[1] = NULL;
-#ifdef OS_WIN
-        GetModuleFileName(NULL, argv[0], MAX_PATH);
-#elif defined(OS_LINUX)
-        readlink("/proc/self/exe", argv[0], MAX_PATH);
-#else
-        strcpy(argv[0], "./unnamed");
-#endif
+        get_executable_path(argv[0], MAX_PATH);
     }
 
-    char* cwd = getcwd(g_main_ctx.run_path, sizeof(g_main_ctx.run_path));
-    if (cwd == NULL) {
-        printf("getcwd error\n");
-    }
-    //printf("run_path=%s\n", g_main_ctx.run_path);
-    const char* b = argv[0];
-    const char* e = b;
-    while (*e) ++e;
-    --e;
-    while (e >= b) {
-        if (*e == '/' || *e == '\\') {
-            break;
-        }
-        --e;
-    }
-    strncpy(g_main_ctx.program_name, e+1, sizeof(g_main_ctx.program_name));
+    get_run_dir(g_main_ctx.run_dir, sizeof(g_main_ctx.run_dir));
+    //printf("run_dir=%s\n", g_main_ctx.run_dir);
+    char* pos = strrchr_dir(argv[0]);
+    strncpy(g_main_ctx.program_name, pos+1, sizeof(g_main_ctx.program_name));
 #ifdef OS_WIN
     if (strcmp(g_main_ctx.program_name+strlen(g_main_ctx.program_name)-4, ".exe") == 0) {
         *(g_main_ctx.program_name+strlen(g_main_ctx.program_name)-4) = '\0';
@@ -55,11 +38,11 @@ int main_ctx_init(int argc, char** argv) {
 #endif
     //printf("program_name=%s\n", g_main_ctx.program_name);
     char logpath[MAX_PATH] = {0};
-    snprintf(logpath, sizeof(logpath), "%s/logs", g_main_ctx.run_path);
+    snprintf(logpath, sizeof(logpath), "%s/logs", g_main_ctx.run_dir);
     MKDIR(logpath);
-    snprintf(g_main_ctx.confile, sizeof(g_main_ctx.confile), "%s/etc/%s.conf", g_main_ctx.run_path, g_main_ctx.program_name);
-    snprintf(g_main_ctx.pidfile, sizeof(g_main_ctx.pidfile), "%s/logs/%s.pid", g_main_ctx.run_path, g_main_ctx.program_name);
-    snprintf(g_main_ctx.logfile, sizeof(g_main_ctx.confile), "%s/logs/%s.log", g_main_ctx.run_path, g_main_ctx.program_name);
+    snprintf(g_main_ctx.confile, sizeof(g_main_ctx.confile), "%s/etc/%s.conf", g_main_ctx.run_dir, g_main_ctx.program_name);
+    snprintf(g_main_ctx.pidfile, sizeof(g_main_ctx.pidfile), "%s/logs/%s.pid", g_main_ctx.run_dir, g_main_ctx.program_name);
+    snprintf(g_main_ctx.logfile, sizeof(g_main_ctx.confile), "%s/logs/%s.log", g_main_ctx.run_dir, g_main_ctx.program_name);
     hlog_set_file(g_main_ctx.logfile);
 
     g_main_ctx.pid = getpid();

+ 1 - 1
utils/hmain.h

@@ -7,7 +7,7 @@
 #include "hproc.h"
 
 typedef struct main_ctx_s {
-    char    run_path[MAX_PATH];
+    char    run_dir[MAX_PATH];
     char    program_name[MAX_PATH];
     char    confile[MAX_PATH]; // default etc/${program}.conf
     char    pidfile[MAX_PATH]; // default logs/${program}.pid