glibc源码分析之普通文件读写

来源:互联网 发布:笔记本风扇除尘软件 编辑:程序博客网 时间:2024/05/10 19:27

glibc中关于普通文件读写的函数有open,close,read,write,lseek,lseek64。它们分别封装了open,close,read,write,lseek,_llseek系统调用。
lseek用于在32位长度的文件中跳转,_llseek用于在64位长度的文件中跳转。
open函数的封装在前文中已经介绍了。

close函数定义在sysdeps/unix/sysv/linux/close.c文件中。

int__close (int fd){  return SYSCALL_CANCEL (close, fd);}libc_hidden_def (__close)strong_alias (__close, __libc_close)weak_alias (__close, close)

__close 函数调用了close系统调用。

read函数定义在sysdeps/unix/sysv/linux/read.c文件中

ssize_t__libc_read (int fd, void *buf, size_t nbytes){  return SYSCALL_CANCEL (read, fd, buf, nbytes);}libc_hidden_def (__libc_read)libc_hidden_def (__read)weak_alias (__libc_read, __read)libc_hidden_def (read)weak_alias (__libc_read, read)

__libc_read函数调用了read系统调用。

write函数定义在sysdeps/unix/sysv/linux/write.c文件中

ssize_t__libc_write (int fd, const void *buf, size_t nbytes){  return SYSCALL_CANCEL (write, fd, buf, nbytes);}libc_hidden_def (__libc_write)weak_alias (__libc_write, __write)libc_hidden_weak (__write)weak_alias (__libc_write, write)libc_hidden_weak (write)

__libc_write 函数调用了write系统调用。

lseek函数定义在sysdeps/unix/sysv/linux/lseek.c文件中。

static inline off_t lseek_overflow (loff_t res){  off_t retval = (off_t) res;  if (retval == res)    return retval;  __set_errno (EOVERFLOW);  return (off_t) -1;}off_t__lseek (int fd, off_t offset, int whence){  loff_t res;  int rc = INLINE_SYSCALL_CALL (_llseek, fd,                (long) (((uint64_t) (offset)) >> 32),                (long) offset, &res, whence);  return rc ?: lseek_overflow (res);}libc_hidden_def (__lseek)weak_alias (__lseek, lseek)strong_alias (__lseek, __libc_lseek)

__lseek 函数调用了_llseek系统调用。

lseek64函数定义在sysdeps/unix/sysv/linux/lseek64.c文件中。

off64_t__lseek64 (int fd, off64_t offset, int whence){  loff_t res;  int rc = INLINE_SYSCALL_CALL (_llseek, fd,                (long) (((uint64_t) (offset)) >> 32),                (long) offset, &res, whence);  return rc ?: res;}

__lseek64 调用了_llseek系统调用。