gpt4 book ai didi

c - read() 的包装函数无法编译

转载 作者:太空宇宙 更新时间:2023-11-04 07:34:26 25 4
gpt4 key购买 nike

我正尝试在 Linux 下为 read() 编写一个包装函数。

请放轻松,因为这是我第一次使用 Wrappers :)

给定 my_wrappers.c 文件的代码:

#include "my_wrappers.h"
#include <unistd.h>
#include <sys/types.h>

ssize_t my_read (int fd, void *buf, size_t count)
{
long ret;
extern int errno;

__asm__ __volatile__ ("pushl %%ebx\n\t"
"movl %%esi,%%ebx\n\t"
"int $0x80\n\t"
"popl %%ebx"
: "=a" (ret)
: "0" (SYS_read), "S" ((long) fd),
"c" ((long) buf) "d" ((long) count): "bx");
if (ret >= 0)
{
return (int) ret;
}
errno = -ret;
return -1;
}

my_wrappers.h文件:

#ifndef __MY_WRAPPERS_H_
#define __MY_WRAPPERS_H_

#include <unistd.h>
#include <sys/types.h>


int my_open(const char *pathname, int flags, mode_t mode);
ssize_t my_write(int fd, const void *buf, size_t count);
ssize_t my_read(int fd, void *buf, size_t count);
int my_close(int fd);

pid_t my_fork(void);

#endif

我只被允许使用调用 libc 包装器,即,我不允许调用 open()、read() 等。

这段代码有什么问题?也许是因为我没有使用系统调用表的编号?

来自 Eclipse 的错误:- ‘SYS_read’ undeclared (first use in this function)

问候

罗恩

最佳答案

您需要 #include <syscall.h>或者可能 #include <sys/syscall.h>

关于c - read() 的包装函数无法编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10240255/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com