当前位置: 首页 > news >正文

implement libwhich for Windows

因为windows没有类似unix的which命令

  • 现在实现尽量跨平台,且stb 风格的libwhich
// which.h
#ifndef LIBWHICH_H
#define LIBWHICH_H#ifdef __cplusplus
extern "C" {
#endif/** 查找可执行文件在系统中的路径* 参数:*   filename - 要查找的可执行文件名*   buffer   - 用于存储结果的缓冲区*   bufsize  - 缓冲区大小* 返回值:*   成功返回指向buffer的指针,失败返回NULL*/
char* libwhich(const char* filename, char* buffer, int bufsize);#ifdef __cplusplus
}
#endif#endif // LIBWHICH_H#ifdef LIBWHICH_IMPLEMENTATION#include <stdlib.h>
#include <string.h>
#include <stdio.h>#ifdef _WIN32
#include <windows.h>
#include <io.h>
#define access _access
#define F_OK 0
#else
#include <unistd.h>
#include <sys/stat.h>
#endif// 辅助函数:检查文件是否存在且可执行
static int is_executable(const char* path) {
#ifdef _WIN32// Windows系统检查文件是否存在return access(path, F_OK) == 0;
#else// Unix-like系统检查文件是否存在且可执行struct stat st;if (stat(path, &st) != 0)return 0;return (st.st_mode & S_IXUSR) || (st.st_mode & S_IXGRP) || (st.st_mode & S_IXOTH);
#endif
}// 辅助函数:获取路径分隔符(PATH环境变量中分隔目录的符号)
static char get_path_separator() {
#ifdef _WIN32return ';';
#elsereturn ':';
#endif
}// 辅助函数:获取目录分隔符(路径中分隔目录的符号)
static char get_dir_separator() {
#ifdef _WIN32return '\\';
#elsereturn '/';
#endif
}// 检查文件名是否包含扩展名
static int has_extension(const char* filename) {const char* dot = strrchr(filename, '.');return dot != NULL && dot != filename && *(dot + 1) != '\0';
}char* libwhich(const char* filename, char* buffer, int bufsize) {if (!filename || !buffer || bufsize <= 0)return NULL;// 获取PATH环境变量const char* path_env = getenv("PATH");if (!path_env)return NULL;// 复制PATH以便处理char* path = strdup(path_env);if (!path)return NULL;char* result = NULL;char sep[2] = {get_path_separator(), '\0'};char dir_sep = get_dir_separator();// 拆分PATH中的各个目录char* dir = strtok(path, sep);while (dir != NULL) {size_t dir_len = strlen(dir);size_t file_len = strlen(filename);int need_sep = (dir_len > 0 && dir[dir_len - 1] != dir_sep) ? 1 : 0;// 检查缓冲区是否足够if (dir_len + need_sep + file_len + 1 > (size_t)bufsize) {dir = strtok(NULL, sep);continue;}// 构建完整路径strcpy(buffer, dir);if (need_sep) {buffer[dir_len] = dir_sep;strcpy(buffer + dir_len + 1, filename);} else {strcpy(buffer + dir_len, filename);}// 检查文件是否存在且可执行if (is_executable(buffer)) {result = buffer;break;}#ifdef _WIN32// Windows系统需要检查常见的可执行文件扩展名if (!has_extension(filename)) {const char* exts[] = {".exe", ".com", ".bat", ".cmd", ".ps1", NULL};for (int i = 0; exts[i] != NULL; i++) {size_t ext_len = strlen(exts[i]);if (dir_len + need_sep + file_len + ext_len + 1 > (size_t)bufsize)continue;// 构建带扩展名的路径strcpy(buffer, dir);if (need_sep) {buffer[dir_len] = dir_sep;strcpy(buffer + dir_len + 1, filename);} else {strcpy(buffer + dir_len, filename);}strcat(buffer, exts[i]);if (is_executable(buffer)) {result = buffer;goto cleanup; // 找到后跳出所有循环}}}
#endifdir = strtok(NULL, sep);}cleanup:free(path);return result;
}#endif // LIBWHICH_IMPLEMENTATION// 示例主程序,定义LIBWHICH_MAIN可编译为独立工具
#ifdef LIBWHICH_MAIN
#include <stdio.h>int main(int argc, char* argv[]) {if (argc != 2) {fprintf(stderr, "用法: %s <命令名>\n", argv[0]);return 1;}char buffer[1024];char* path = libwhich(argv[1], buffer, sizeof(buffer));if (path) {printf("%s\n", path);return 0;} else {fprintf(stderr, "%s: 未找到命令\n", argv[1]);return 1;}
}
#endif

build as program (view .h file as .c file)

> gcc -x c which.h -DLIBWHICH_MAIN -DLIBWHICH_IMPLEMENTATION> cl /DLIBWHICH_IMPLEMENTATION /DLIBWHICH_MAIN /Tcwhich.h /Fe:which.exe

build as lib (msvc for example)

> cl /DLIBWHICH_IMPLEMENTATION /Tcwhich.h /Fo:libwhich.obj> lib libwhich.obj /OUT:libwhich.lib

as lib usage

// probe.c#define LIBWHICH_IMPLEMENTATION
#include "which.h"
#include <stdio.h>int main() {char buffer[1024];char* path = libwhich("python", buffer, sizeof(buffer));if (path) {printf("找到python: %s\n", path);} else {printf("未找到python\n");}return 0;
}
http://www.xdnf.cn/news/1314145.html

相关文章:

  • STM32标准库学习笔记
  • 40 C++ STL模板库9-容器2-vector
  • 深度剖析Redisson分布式锁项目实战
  • 8.从零开始写LINUX内核——初始化中断控制芯片
  • 三、memblock 内存分配器
  • 《软件工程导论》实验报告五 设计建模工具的使用(一)类图
  • 《AI 与数据质量的深度碰撞:颠覆传统治理模式的变革》文章提纲
  • Java -- 泛型-自定义泛型
  • 《从入门到精通:Kafka核心原理全解析》
  • 中级统计师-会计学基础知识-第三章 会计凭证与会计账簿
  • ZYNQ QSPI控制器说明
  • OpenStack Neutron中的L2 Agent与L3 Agent:新手友好指南
  • Java 9 新特性及具体应用
  • InfluxDB 数据迁移工具:跨数据库同步方案(二)
  • 【笔记】动手学Ollma 第一章 Ollama介绍
  • 各种读取csv文件的工具性能比较
  • GEEPython-demo1:利用Sentinel-2监测北京奥林匹克森林公园2024年NDVI变化(附Python版)
  • 从零开始大模型之实现GPT模型
  • Spring Framework:Java 开发的基石与 Spring 生态的起点
  • RecSys:多目标模型和MMOE
  • Oracle数据库文件管理与空间问题解决指南
  • Reduce系列
  • GaussDB 数据库架构师修炼(十三)安全管理(3)-行级访问控制
  • 测试18种RAG技术,找出最优方案(四)
  • 用Pygame开发桌面小游戏:从入门到发布
  • 衡石使用指南嵌入式场景实践之仪表盘嵌入
  • 【FreeRTOS】队列集
  • 聊聊智慧这个东西之三:从食物的毒性、偏性聊起
  • 布隆过滤器的原理及使用
  • ubuntu网络共享