Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ add_executable(main_toolbox
src/external_software.cpp
src/template_generator.cpp
src/conda_setup.cpp
src/common_utils.cpp
src/soft_manager/soft_manager.cpp
# src/soft_manager/clash_verge_installer.cpp
src/soft_manager/sogou.cpp
src/soft_manager/tmux.cpp
)

# 4. 关键配置:全静态链接设置
Expand All @@ -28,4 +23,4 @@ add_executable(main_toolbox
set(CMAKE_EXE_LINKER_FLAGS "-static -pthread")

# 如果你的代码中有用到 dlopen 等动态加载功能(通常 system() 不需要,但为了保险),
# 有时需要显式链接 dl 库,但在全静态模式下通常不适用,这里仅作提示。
# 有时需要显式链接 dl 库,但在全静态模式下通常不适用,这里仅作提示。
46 changes: 42 additions & 4 deletions include/common_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,50 @@
#include <iostream>

// 获取真实用户 (SUDO_USER 优先)
std::string get_real_user();
inline std::string get_real_user() {
const char* sudo_user = std::getenv("SUDO_USER");
if (sudo_user != nullptr) return std::string(sudo_user);

const char* user = std::getenv("USER");
if (user != nullptr) return std::string(user);

return "root";
}

// 获取真实用户的家目录
std::string get_real_home_dir();
inline std::string get_real_home_dir() {
std::string user = get_real_user();
if (user == "root") {
// 如果真的是 root 登录,尝试获取 HOME 环境变量
const char* home = std::getenv("HOME");
return home ? std::string(home) : "/root";
}
return "/home/" + user;
}

// 展开路径中的 ~ 符号
std::string expand_user_path(const std::string& path);
inline std::string expand_user_path(const std::string& path) {
if (path.empty()) return path;

// 如果路径以 ~ 开头
if (path[0] == '~') {
std::string home = get_real_home_dir();

// 情况 1: 只有 "~"
if (path.size() == 1) {
return home;
}
// 情况 2: 标准 "~/"
else if (path[1] == '/') {
return home + path.substr(1);
}
// 情况 3: 手误 "~~/" 或者是某些特殊情况
else if (path[1] == '~' && path.size() > 2 && path[2] == '/') {
return home + path.substr(2);
}
}

return path;
}

#endif // COMMON_UTILS_H
#endif // COMMON_UTILS_H
2 changes: 2 additions & 0 deletions include/unit_color.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#pragma once

#include <iostream>
#include <string>

Expand Down
47 changes: 0 additions & 47 deletions src/common_utils.cpp

This file was deleted.

Loading