Windows 中 S_IFREG 的等价物是什么?

问题描述 投票:0回答:1

我正在用 C 编写一些代码并遇到一个问题 - 即使我定义了

sys/stats.h
标头,当我使用
S_IFREG
时,它会给我错误“使用未声明的标识符
S_IFREG
”。谷歌搜索后,它告诉我这不是可以在 Windows 上使用的东西,这就是我正在使用的东西。我找不到 Windows 上的等效项的明确答案,所以我想知道是否有人可以帮助我这是我的代码:

int function( command_t* p_cmd ) {


char path[MAX_ARG_LEN];
    strcpy(path, getenv("PATH"));

    char *token = strtok(path, ":");

    while(token != NULL){
        char parameter[MAX_ARG_LEN];
        strcpy(parameter, token);
        strcat(parameter, "/");
        strcat(parameter, p_cmd->argv[0]);

        struct stat buffer;
        int exists;

        exists = stat(parameter, &buffer);
        if(exists == 0 && (S_IFREG & buffer.st_mode)){
            strcpy(p_cmd->argv[0] , parameter);
            return 1; 
        }

        token = strtok(NULL, ":");
    }

    return 0;
}

这些是我正在使用的标头,我无法更改它们以添加其他库。

#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

我也无法更改参数或使用任何扩展。

c
1个回答
0
投票

答案从编辑移至问题:


本质上,现代 POSIX 兼容系统需要 S_IFMT 和 S_IFREG 值,但 POSIX.1-1990 不需要并且实际上禁止您包含它们。在我的第一段代码中,我手动屏蔽以检查文件:

(S_IFREG & buffer.st_mode)

但是 POSIX 兼容系统提供的宏已经为你做到了这一点,所以代码应该是:

S_ISREG(buffer.st_mode)
© www.soinside.com 2019 - 2024. All rights reserved.