浏览代码

feat: 修改getExedir的机制

使用GetModuleFileNameA或readlink获取可执行程序的路径
SongZihuan 3 年之前
父节点
当前提交
6458823c50
共有 4 个文件被更改,包括 21 次插入6 次删除
  1. 1 1
      include/tool/file.h
  2. 1 1
      src/main.c
  3. 18 3
      src/tool/file.c
  4. 1 1
      test/src/run_code.c

+ 1 - 1
include/tool/file.h

@@ -12,7 +12,7 @@ AFUN_TOOL_EXPORT char *getFilePath(char *path_1, int dep);
 AFUN_TOOL_EXPORT char *getFileSurfix(char *path);
 AFUN_TOOL_EXPORT char *getFileSurfix(char *path);
 AFUN_TOOL_EXPORT char *fileNameToVar(char *name, bool need_free);
 AFUN_TOOL_EXPORT char *fileNameToVar(char *name, bool need_free);
 AFUN_TOOL_EXPORT char *findPath(char *path, char *env, bool need_free);
 AFUN_TOOL_EXPORT char *findPath(char *path, char *env, bool need_free);
-AFUN_TOOL_EXPORT char *getExedir(char *pgm, int dep);
+AFUN_TOOL_EXPORT char *getExedir(int dep);
 AFUN_TOOL_EXPORT uintmax_t getFileSize(char *path);
 AFUN_TOOL_EXPORT uintmax_t getFileSize(char *path);
 AFUN_TOOL_EXPORT bool isCharUTF8(char *str);
 AFUN_TOOL_EXPORT bool isCharUTF8(char *str);
 #endif //AFUN_FILE_H
 #endif //AFUN_FILE_H

+ 1 - 1
src/main.c

@@ -50,7 +50,7 @@ void freeBaseName(void) {
 
 
 int main(int argc, char **argv) {
 int main(int argc, char **argv) {
     jmp_buf main_buf;
     jmp_buf main_buf;
-    base_path = getExedir(*argv, 1);
+    base_path = getExedir(1);
     if (base_path == NULL)
     if (base_path == NULL)
         goto INIT_ERROR;
         goto INIT_ERROR;
     atexit(freeBaseName);
     atexit(freeBaseName);

+ 18 - 3
src/tool/file.c

@@ -10,6 +10,13 @@
 #include <stdio.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdlib.h>
 
 
+#ifdef aFunWIN32_NO_CYGWIN
+#pragma warning(disable : 5105)  // 关闭 5105 的警告输出 (Windows.h中使用)
+#include <Windows.h>
+#else
+#include <unistd.h>
+#endif
+
 #include "tool.h"
 #include "tool.h"
 
 
 #ifndef S_ISREG
 #ifndef S_ISREG
@@ -174,10 +181,18 @@ char *findPath(char *path, char *env, bool need_free){
  * 函数名: 获取可执行程序目录
  * 函数名: 获取可执行程序目录
  * dep表示从可执行程序往回跳出的层数
  * dep表示从可执行程序往回跳出的层数
  */
  */
-char *getExedir(char *pgm, int dep) {
-    if (pgm == NULL)
+char *getExedir(int dep) {
+    char exepath[218] = {0};
+#ifdef aFunWIN32_NO_CYGWIN
+    DWORD ret = GetModuleFileNameA(NULL, exepath, 217);  // 预留一位给NUL
+    if (ret == 0 || STR_LEN(exepath) == 0)
         return NULL;
         return NULL;
-    return getFilePath(pgm, dep + 1);
+#else
+    ssize_t ret =  readlink("/proc/self/exe", exepath, 217);  // 预留一位给NUL
+    if (ret == -1 || STR_LEN(exepath) == 0)
+        return NULL;
+#endif
+    return getFilePath(exepath, dep + 1);
 }
 }
 
 
 uintmax_t getFileSize(char *path) {
 uintmax_t getFileSize(char *path) {

+ 1 - 1
test/src/run_code.c

@@ -371,7 +371,7 @@ bool infixFunc(char *id, af_Object *obj) {
 
 
 int main(int argc, char **argv) {
 int main(int argc, char **argv) {
     jmp_buf main_buf;
     jmp_buf main_buf;
-    char *base_path = getExedir(*argv, 1);
+    char *base_path = getExedir(1);
     if (base_path == NULL)
     if (base_path == NULL)
         goto INIT_ERROR;
         goto INIT_ERROR;