Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 包装变量函数_C_Gcc_Variadic Functions - Fatal编程技术网

C 包装变量函数

C 包装变量函数,c,gcc,variadic-functions,C,Gcc,Variadic Functions,我正在尝试包装open函数。我不知道如何将可选的第三个参数传递给realopen。据我所知,无法验证va_列表,因此在下面的示例中,if(mode)不正确。有没有一种方法可以使用适当数量的参数调用open #include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <s

我正在尝试包装
open
函数。我不知道如何将可选的第三个参数传递给real
open
。据我所知,无法验证
va_列表
,因此在下面的示例中,
if(mode)
不正确。有没有一种方法可以使用适当数量的参数调用
open

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdarg.h>

extern "C" 
{

int shouldWrap = 0;

int willCallRealOpen(const char * path, int flags, va_list args);
int __real_open(const char * path, int flags, ...);

int __wrap_open(const char * path, int flags, ...) {
    if(shouldWrap != 0){
        printf("Fake called\n");
        return 0;
    }
    else {
        printf("Real called\n");
        va_list args;
            va_start(args, flags);
            int res = willCallRealOpen(path, flags, args);
        va_end(args);
        return res;
    }
}

int willCallRealOpen(const char * path, int flags, va_list args) {
    mode_t mode = va_arg(args, mode_t);
    if (mode) {
        printf("3 args\n");
        return __real_open(path, flags, mode);
    }
    else {
        printf("2 args\n");
        return __real_open(path, flags);
    }
}   

}

int main() {
    //int fd = open("temp.txt", O_CREAT | O_WRONLY, S_IRUSR);
    int fd = open("temp.txt", O_CREAT | O_WRONLY);
}
#包括
#包括
#包括
#包括
#包括
#包括
外部“C”
{
int shouldWrap=0;
int willCallRealOpen(常量字符*路径、int标志、va_列表参数);
int uu real u open(常量字符*路径,int标志,…);
整型(常量字符*路径,整型标志,…){
如果(shouldWrap!=0){
printf(“假称\n”);
返回0;
}
否则{
printf(“称为Real\n”);
va_列表参数;
va_开始(参数、标志);
int res=willCallRealOpen(路径、标志、参数);
va_端(args);
返回res;
}
}
int willCallRealOpen(常量字符*路径、int标志、va_列表参数){
mode\u t mode=va\u arg(args,mode\u t);
如果(模式){
printf(“3个参数\n”);
返回实际打开(路径、标志、模式);
}
否则{
printf(“2个参数\n”);
返回实际打开(路径、标志);
}
}   
}
int main(){
//int fd=打开(“temp.txt”,O|u CREAT | O|u WRONLY,S|u IRUSR);
int fd=打开(“temp.txt”,O|u CREAT | O|u WRONLY);
}

open的手册页上写着:

模式指定在创建新文件时使用的权限 创建。此参数必须在O_创建或O_TMPFILE时提供 在旗帜中指定;如果未指定O_create或O_TMPFILE, 然后模式被忽略

所以我认为你应该做一些类似的事情:

int willCallRealOpen(const char * path, int flags, va_list args) {
  if (flags & (O_CREAT | O_TMPFILE))
  {
    mode_t mode = va_arg(args, mode_t);
    printf("3 args\n");
    return __real_open(path, flags, mode);
  }
  else {
    printf("2 args\n");
    return __real_open(path, flags);
  }
}